@dereekb/util 12.6.21 → 13.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/fetch/LICENSE +1 -1
- package/fetch/index.cjs.js +42 -2529
- package/fetch/index.esm.js +44 -2530
- package/fetch/package.json +21 -16
- package/fetch/src/lib/fetch.page.d.ts +0 -4
- package/fetch/src/lib/fetch.url.d.ts +0 -8
- package/fetch/src/lib/provider.d.ts +0 -4
- package/index.cjs.js +36 -2354
- package/index.esm.js +39 -2344
- package/package.json +18 -7
- package/src/lib/array/array.boolean.d.ts +0 -11
- package/src/lib/array/array.value.d.ts +0 -18
- package/src/lib/boolean.d.ts +1 -1
- package/src/lib/date/date.d.ts +0 -8
- package/src/lib/date/date.unix.d.ts +6 -30
- package/src/lib/date/expires.d.ts +4 -4
- package/src/lib/date/hour.d.ts +1 -1
- package/src/lib/date/time.d.ts +0 -4
- package/src/lib/key.d.ts +4 -0
- package/src/lib/object/object.map.d.ts +0 -9
- package/src/lib/page/index.d.ts +0 -1
- package/src/lib/promise/promise.d.ts +2 -3
- package/src/lib/set/set.hashset.d.ts +4 -4
- package/src/lib/storage/storage.d.ts +2 -2
- package/src/lib/string/mimetype.d.ts +0 -8
- package/src/lib/string/replace.d.ts +11 -1
- package/src/lib/tree/tree.flatten.d.ts +0 -10
- package/src/lib/value/indexed.d.ts +1 -1
- package/src/lib/value/maybe.type.d.ts +3 -3
- package/src/lib/value/point.d.ts +2 -2
- package/test/LICENSE +21 -0
- package/test/index.cjs.js +707 -0
- package/test/index.esm.js +664 -0
- package/test/package.json +20 -6
- package/test/src/lib/index.d.ts +1 -3
- package/test/src/lib/jest/index.d.ts +4 -0
- package/test/src/lib/jest/jest.d.ts +55 -0
- package/test/src/lib/jest/jest.fail.d.ts +72 -0
- package/test/src/lib/jest/jest.function.d.ts +41 -0
- package/test/src/lib/jest/jest.wrap.d.ts +24 -0
- package/test/src/lib/shared/index.d.ts +4 -0
- package/test/src/lib/shared/shared.d.ts +154 -0
- package/test/src/lib/shared/shared.fail.d.ts +83 -0
- package/test/src/lib/shared/shared.function.d.ts +25 -0
- package/test/src/lib/{jest.wrap.d.ts → shared/shared.wrap.d.ts} +9 -9
- package/index.esm.d.ts +0 -1
- package/src/lib/page/page.calculator.d.ts +0 -29
- package/test/CHANGELOG.md +0 -1956
- package/test/src/index.js +0 -5
- package/test/src/index.js.map +0 -1
- package/test/src/lib/index.js +0 -8
- package/test/src/lib/index.js.map +0 -1
- package/test/src/lib/jest.d.ts +0 -100
- package/test/src/lib/jest.fail.d.ts +0 -104
- package/test/src/lib/jest.fail.js +0 -224
- package/test/src/lib/jest.fail.js.map +0 -1
- package/test/src/lib/jest.function.d.ts +0 -25
- package/test/src/lib/jest.function.js +0 -21
- package/test/src/lib/jest.function.js.map +0 -1
- package/test/src/lib/jest.js +0 -113
- package/test/src/lib/jest.js.map +0 -1
- package/test/src/lib/jest.wrap.js +0 -70
- package/test/src/lib/jest.wrap.js.map +0 -1
- /package/fetch/{index.cjs.d.ts → index.d.ts} +0 -0
- /package/{fetch/index.esm.d.ts → index.d.ts} +0 -0
- /package/{index.cjs.d.ts → test/index.d.ts} +0 -0
|
@@ -0,0 +1,664 @@
|
|
|
1
|
+
import { promiseReference, forwardFunction, mapObjectMap, isPromise, build } from '@dereekb/util';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Passes the error to the TestDoneCallback.
|
|
5
|
+
* @param done
|
|
6
|
+
* @param e
|
|
7
|
+
*/
|
|
8
|
+
function failWithTestDoneCallback(done, e = new Error('failed test')) {
|
|
9
|
+
if (done.fail != null) {
|
|
10
|
+
done.fail(e);
|
|
11
|
+
} else {
|
|
12
|
+
done(e);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new TestDoneCallbackRef.
|
|
17
|
+
*
|
|
18
|
+
* Used to create a promise reference that can be used to assert that a test function was called.
|
|
19
|
+
*/
|
|
20
|
+
function testDoneCallbackRef() {
|
|
21
|
+
const _promise = promiseReference();
|
|
22
|
+
const done = e => {
|
|
23
|
+
if (e) {
|
|
24
|
+
_promise.reject(e);
|
|
25
|
+
} else {
|
|
26
|
+
_promise.resolve();
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
done.fail = done;
|
|
30
|
+
return {
|
|
31
|
+
_promise,
|
|
32
|
+
done
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Wraps a callback-based test (using done) for Vitest compatibility.
|
|
37
|
+
* Converts the callback pattern to a Promise-based pattern.
|
|
38
|
+
*
|
|
39
|
+
* This also supports calling the input test with async, but still only returns when done is called.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
*
|
|
43
|
+
* // Before (Jasmine/Jest style):
|
|
44
|
+
* it('test name', (done) => {
|
|
45
|
+
* // async test code
|
|
46
|
+
* done();
|
|
47
|
+
* });
|
|
48
|
+
*
|
|
49
|
+
* // After (Vitest compatible):
|
|
50
|
+
* it('test name', callbackTest((done) => {
|
|
51
|
+
* // async test code
|
|
52
|
+
* done();
|
|
53
|
+
* }));
|
|
54
|
+
*/
|
|
55
|
+
function callbackTest(testFn) {
|
|
56
|
+
return async () => {
|
|
57
|
+
const done = testDoneCallbackRef();
|
|
58
|
+
await testFn(done.done);
|
|
59
|
+
return done._promise.promise;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Abstract TestContextFixture instance.
|
|
64
|
+
*/
|
|
65
|
+
class AbstractTestContextFixture {
|
|
66
|
+
constructor() {
|
|
67
|
+
this._instance = void 0;
|
|
68
|
+
}
|
|
69
|
+
get instance() {
|
|
70
|
+
return this._instance;
|
|
71
|
+
}
|
|
72
|
+
setInstance(instance) {
|
|
73
|
+
if (this._instance != null) {
|
|
74
|
+
throw new Error(`The testing fixture is locked. Don't call setInstance() directly.`);
|
|
75
|
+
}
|
|
76
|
+
this._instance = instance;
|
|
77
|
+
return () => {
|
|
78
|
+
delete this._instance;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Abstract TestContextFixture instance with a parent.
|
|
84
|
+
*/
|
|
85
|
+
class AbstractChildTestContextFixture extends AbstractTestContextFixture {
|
|
86
|
+
constructor(parent) {
|
|
87
|
+
super();
|
|
88
|
+
this.parent = void 0;
|
|
89
|
+
this.parent = parent;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Creates a TestContextBuilderFunction given the input builder.
|
|
94
|
+
*
|
|
95
|
+
* @param builder
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
function testContextBuilder(builder) {
|
|
99
|
+
return inputConfig => {
|
|
100
|
+
const config = builder.buildConfig(inputConfig);
|
|
101
|
+
return buildTests => {
|
|
102
|
+
const fixture = builder.buildFixture(config);
|
|
103
|
+
// add before each
|
|
104
|
+
if (builder.beforeEach != null) {
|
|
105
|
+
beforeEach(builder.beforeEach);
|
|
106
|
+
}
|
|
107
|
+
// add tests
|
|
108
|
+
useTestContextFixture({
|
|
109
|
+
fixture,
|
|
110
|
+
/**
|
|
111
|
+
* Build tests by passing the fixture to the testing functions.
|
|
112
|
+
*
|
|
113
|
+
* This will inject all tests and sub lifecycle items.
|
|
114
|
+
*/
|
|
115
|
+
buildTests,
|
|
116
|
+
initInstance: () => builder.setupInstance(config),
|
|
117
|
+
destroyInstance: instance => builder.teardownInstance(instance, config)
|
|
118
|
+
});
|
|
119
|
+
// add after each
|
|
120
|
+
if (builder.afterEach != null) {
|
|
121
|
+
afterEach(builder.afterEach);
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Creates a test context and configurations that will initialize an instance
|
|
128
|
+
*/
|
|
129
|
+
function useTestContextFixture(config) {
|
|
130
|
+
const {
|
|
131
|
+
buildTests,
|
|
132
|
+
fixture,
|
|
133
|
+
initInstance,
|
|
134
|
+
destroyInstance
|
|
135
|
+
} = config;
|
|
136
|
+
let clearInstance;
|
|
137
|
+
let instance;
|
|
138
|
+
// Create an instance
|
|
139
|
+
beforeEach(async () => {
|
|
140
|
+
try {
|
|
141
|
+
instance = await initInstance();
|
|
142
|
+
clearInstance = fixture.setInstance(instance);
|
|
143
|
+
} catch (e) {
|
|
144
|
+
console.error('Failed building a test instance due to an error in buildInstance(). Error: ', e);
|
|
145
|
+
if (clearInstance) {
|
|
146
|
+
clearInstance();
|
|
147
|
+
}
|
|
148
|
+
throw e;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
// Declare tests
|
|
152
|
+
buildTests(fixture);
|
|
153
|
+
// Cleanup
|
|
154
|
+
afterEach(async () => {
|
|
155
|
+
if (clearInstance) {
|
|
156
|
+
clearInstance();
|
|
157
|
+
}
|
|
158
|
+
if (fixture.instance != null) {
|
|
159
|
+
console.warn('Expected instance to be set on fixture for cleanup but was set to something else.');
|
|
160
|
+
}
|
|
161
|
+
if (destroyInstance) {
|
|
162
|
+
try {
|
|
163
|
+
await destroyInstance(instance);
|
|
164
|
+
instance = undefined;
|
|
165
|
+
} catch (e) {
|
|
166
|
+
console.error('Failed due to error in destroyInstance()');
|
|
167
|
+
throw e;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
class AbstractWrappedFixture {
|
|
174
|
+
constructor(fixture) {
|
|
175
|
+
this.fixture = void 0;
|
|
176
|
+
this.fixture = fixture;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
class AbstractWrappedFixtureWithInstance extends AbstractTestContextFixture {
|
|
180
|
+
constructor(parent) {
|
|
181
|
+
super();
|
|
182
|
+
this.parent = void 0;
|
|
183
|
+
this.parent = parent;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Wraps the input TestContextFactory to emit another type of Fixture for tests.
|
|
188
|
+
*
|
|
189
|
+
* @returns
|
|
190
|
+
*/
|
|
191
|
+
function wrapTestContextFactory(config) {
|
|
192
|
+
return factory => {
|
|
193
|
+
return buildTests => {
|
|
194
|
+
factory(inputFixture => {
|
|
195
|
+
const wrap = config.wrapFixture(inputFixture);
|
|
196
|
+
let effect;
|
|
197
|
+
// add before each
|
|
198
|
+
if (config.setupWrap != null) {
|
|
199
|
+
beforeEach(async () => {
|
|
200
|
+
effect = await config.setupWrap(wrap);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
// add tests
|
|
204
|
+
buildTests(wrap);
|
|
205
|
+
// add after each
|
|
206
|
+
if (config.teardownWrap != null) {
|
|
207
|
+
afterEach(async () => {
|
|
208
|
+
await config.teardownWrap(wrap, effect);
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function instanceWrapTestContextFactory(config) {
|
|
216
|
+
return wrapTestContextFactory({
|
|
217
|
+
wrapFixture: config.wrapFixture,
|
|
218
|
+
setupWrap: async wrap => {
|
|
219
|
+
const instance = await config.makeInstance(wrap);
|
|
220
|
+
const effect = wrap.setInstance(instance);
|
|
221
|
+
if (config.setupInstance) {
|
|
222
|
+
await config.setupInstance(instance, wrap);
|
|
223
|
+
}
|
|
224
|
+
return effect;
|
|
225
|
+
},
|
|
226
|
+
teardownWrap: async (wrap, deleteInstanceEffect) => {
|
|
227
|
+
deleteInstanceEffect?.();
|
|
228
|
+
if (config.teardownInstance) {
|
|
229
|
+
await config.teardownInstance(wrap.instance);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Creates a test context and configurations that provides a function to build tests based on the configuration.
|
|
237
|
+
*/
|
|
238
|
+
function useTestFunctionFixture(config, buildTests) {
|
|
239
|
+
const {
|
|
240
|
+
fn
|
|
241
|
+
} = config;
|
|
242
|
+
const forward = forwardFunction(fn);
|
|
243
|
+
buildTests(forward);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Creates a test context and configurations that provides a function to build tests based on the configuration.
|
|
247
|
+
*/
|
|
248
|
+
function useTestFunctionMapFixture(config, buildTests) {
|
|
249
|
+
const forwardedFunctions = mapObjectMap(config.fns, fn => forwardFunction(fn));
|
|
250
|
+
buildTests(forwardedFunctions);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
var makeError = {exports: {}};
|
|
254
|
+
|
|
255
|
+
(function (module, exports$1) {
|
|
256
|
+
|
|
257
|
+
// ===================================================================
|
|
258
|
+
|
|
259
|
+
var construct = typeof Reflect !== "undefined" ? Reflect.construct : undefined;
|
|
260
|
+
var defineProperty = Object.defineProperty;
|
|
261
|
+
|
|
262
|
+
// -------------------------------------------------------------------
|
|
263
|
+
|
|
264
|
+
var captureStackTrace = Error.captureStackTrace;
|
|
265
|
+
if (captureStackTrace === undefined) {
|
|
266
|
+
captureStackTrace = function captureStackTrace(error) {
|
|
267
|
+
var container = new Error();
|
|
268
|
+
|
|
269
|
+
defineProperty(error, "stack", {
|
|
270
|
+
configurable: true,
|
|
271
|
+
get: function getStack() {
|
|
272
|
+
var stack = container.stack;
|
|
273
|
+
|
|
274
|
+
// Replace property with value for faster future accesses.
|
|
275
|
+
defineProperty(this, "stack", {
|
|
276
|
+
configurable: true,
|
|
277
|
+
value: stack,
|
|
278
|
+
writable: true,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
return stack;
|
|
282
|
+
},
|
|
283
|
+
set: function setStack(stack) {
|
|
284
|
+
defineProperty(error, "stack", {
|
|
285
|
+
configurable: true,
|
|
286
|
+
value: stack,
|
|
287
|
+
writable: true,
|
|
288
|
+
});
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// -------------------------------------------------------------------
|
|
295
|
+
|
|
296
|
+
function BaseError(message) {
|
|
297
|
+
if (message !== undefined) {
|
|
298
|
+
defineProperty(this, "message", {
|
|
299
|
+
configurable: true,
|
|
300
|
+
value: message,
|
|
301
|
+
writable: true,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
var cname = this.constructor.name;
|
|
306
|
+
if (cname !== undefined && cname !== this.name) {
|
|
307
|
+
defineProperty(this, "name", {
|
|
308
|
+
configurable: true,
|
|
309
|
+
value: cname,
|
|
310
|
+
writable: true,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
captureStackTrace(this, this.constructor);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
BaseError.prototype = Object.create(Error.prototype, {
|
|
318
|
+
// See: https://github.com/JsCommunity/make-error/issues/4
|
|
319
|
+
constructor: {
|
|
320
|
+
configurable: true,
|
|
321
|
+
value: BaseError,
|
|
322
|
+
writable: true,
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
// -------------------------------------------------------------------
|
|
327
|
+
|
|
328
|
+
// Sets the name of a function if possible (depends of the JS engine).
|
|
329
|
+
var setFunctionName = (function() {
|
|
330
|
+
function setFunctionName(fn, name) {
|
|
331
|
+
return defineProperty(fn, "name", {
|
|
332
|
+
configurable: true,
|
|
333
|
+
value: name,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
try {
|
|
337
|
+
var f = function() {};
|
|
338
|
+
setFunctionName(f, "foo");
|
|
339
|
+
if (f.name === "foo") {
|
|
340
|
+
return setFunctionName;
|
|
341
|
+
}
|
|
342
|
+
} catch (_) {}
|
|
343
|
+
})();
|
|
344
|
+
|
|
345
|
+
// -------------------------------------------------------------------
|
|
346
|
+
|
|
347
|
+
function makeError(constructor, super_) {
|
|
348
|
+
if (super_ == null || super_ === Error) {
|
|
349
|
+
super_ = BaseError;
|
|
350
|
+
} else if (typeof super_ !== "function") {
|
|
351
|
+
throw new TypeError("super_ should be a function");
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
var name;
|
|
355
|
+
if (typeof constructor === "string") {
|
|
356
|
+
name = constructor;
|
|
357
|
+
constructor =
|
|
358
|
+
construct !== undefined
|
|
359
|
+
? function() {
|
|
360
|
+
return construct(super_, arguments, this.constructor);
|
|
361
|
+
}
|
|
362
|
+
: function() {
|
|
363
|
+
super_.apply(this, arguments);
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
// If the name can be set, do it once and for all.
|
|
367
|
+
if (setFunctionName !== undefined) {
|
|
368
|
+
setFunctionName(constructor, name);
|
|
369
|
+
name = undefined;
|
|
370
|
+
}
|
|
371
|
+
} else if (typeof constructor !== "function") {
|
|
372
|
+
throw new TypeError("constructor should be either a string or a function");
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// Also register the super constructor also as `constructor.super_` just
|
|
376
|
+
// like Node's `util.inherits()`.
|
|
377
|
+
//
|
|
378
|
+
// eslint-disable-next-line dot-notation
|
|
379
|
+
constructor.super_ = constructor["super"] = super_;
|
|
380
|
+
|
|
381
|
+
var properties = {
|
|
382
|
+
constructor: {
|
|
383
|
+
configurable: true,
|
|
384
|
+
value: constructor,
|
|
385
|
+
writable: true,
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
// If the name could not be set on the constructor, set it on the
|
|
390
|
+
// prototype.
|
|
391
|
+
if (name !== undefined) {
|
|
392
|
+
properties.name = {
|
|
393
|
+
configurable: true,
|
|
394
|
+
value: name,
|
|
395
|
+
writable: true,
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
constructor.prototype = Object.create(super_.prototype, properties);
|
|
399
|
+
|
|
400
|
+
return constructor;
|
|
401
|
+
}
|
|
402
|
+
exports$1 = module.exports = makeError;
|
|
403
|
+
exports$1.BaseError = BaseError;
|
|
404
|
+
} (makeError, makeError.exports));
|
|
405
|
+
|
|
406
|
+
var makeErrorExports = makeError.exports;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Testing utilities for handling expected failures and errors.
|
|
410
|
+
*/
|
|
411
|
+
// MARK: Errors
|
|
412
|
+
/**
|
|
413
|
+
* Error thrown by fail() and used by expectError()
|
|
414
|
+
*/
|
|
415
|
+
class ExpectedFailError extends makeErrorExports.BaseError {}
|
|
416
|
+
function failSuccessfullyError(message) {
|
|
417
|
+
return new ExpectedFailError(message);
|
|
418
|
+
}
|
|
419
|
+
function failSuccessfully(message) {
|
|
420
|
+
throw failSuccessfullyError(message);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Error thrown when success occurs when it should not have.
|
|
424
|
+
*/
|
|
425
|
+
class UnexpectedSuccessFailureError extends makeErrorExports.BaseError {}
|
|
426
|
+
function failDueToSuccessError(message) {
|
|
427
|
+
return new UnexpectedSuccessFailureError(message ?? 'expected an error to occur but was successful instead');
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Error thrown when the error type was different than the expected type.
|
|
431
|
+
*/
|
|
432
|
+
class ExpectedErrorOfSpecificTypeError extends makeErrorExports.BaseError {
|
|
433
|
+
constructor(encounteredType, expectedType) {
|
|
434
|
+
super(`The error encountered was not of the expected type. Expected: ${expectedType ?? 'n/a'}, but encountered: ${encounteredType} `);
|
|
435
|
+
this.encounteredType = void 0;
|
|
436
|
+
this.expectedType = void 0;
|
|
437
|
+
this.encounteredType = encounteredType;
|
|
438
|
+
this.expectedType = expectedType;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function failTest(message) {
|
|
442
|
+
throw failDueToSuccessError(message);
|
|
443
|
+
}
|
|
444
|
+
function failDueToSuccess() {
|
|
445
|
+
throw failDueToSuccessError();
|
|
446
|
+
}
|
|
447
|
+
function EXPECT_ERROR_DEFAULT_HANDLER(e) {
|
|
448
|
+
if (e instanceof ExpectedFailError) ; else {
|
|
449
|
+
throw e;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Creates an ExpectFailAssertionFunction that asserts the encountered error is of the expected type using the instanceof keyword.
|
|
454
|
+
*
|
|
455
|
+
* Throws an ExpectedErrorOfSpecificTypeError on failures.
|
|
456
|
+
*
|
|
457
|
+
* @param expectedType
|
|
458
|
+
* @returns
|
|
459
|
+
*/
|
|
460
|
+
function expectFailAssertErrorType(expectedType) {
|
|
461
|
+
return error => {
|
|
462
|
+
if (!(error instanceof expectedType)) {
|
|
463
|
+
throw new ExpectedErrorOfSpecificTypeError(error, expectedType);
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
function expectFail(errorFn, assertFailType) {
|
|
468
|
+
function handleError(e) {
|
|
469
|
+
if (e instanceof UnexpectedSuccessFailureError) {
|
|
470
|
+
throw e;
|
|
471
|
+
} else {
|
|
472
|
+
const assertionResult = assertFailType?.(e);
|
|
473
|
+
if (assertionResult === false) {
|
|
474
|
+
throw new ExpectedErrorOfSpecificTypeError(e);
|
|
475
|
+
}
|
|
476
|
+
failSuccessfully();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
try {
|
|
480
|
+
const result = errorFn();
|
|
481
|
+
if (isPromise(result)) {
|
|
482
|
+
return result.then(failDueToSuccess).catch(handleError);
|
|
483
|
+
} else {
|
|
484
|
+
failDueToSuccess();
|
|
485
|
+
}
|
|
486
|
+
} catch (e) {
|
|
487
|
+
handleError(e);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
function expectSuccessfulFail(errorFn, handleError = EXPECT_ERROR_DEFAULT_HANDLER) {
|
|
491
|
+
try {
|
|
492
|
+
const result = errorFn();
|
|
493
|
+
if (isPromise(result)) {
|
|
494
|
+
return result.then(failDueToSuccess).catch(handleError);
|
|
495
|
+
} else {
|
|
496
|
+
failDueToSuccess();
|
|
497
|
+
}
|
|
498
|
+
} catch (e) {
|
|
499
|
+
handleError(e);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Used to wrap a testing function and watch for ExpectedFailError errors in order to pass the test. Other exceptions are treated normally as failures.
|
|
504
|
+
*
|
|
505
|
+
* This is typically used in conjunction with failSuccessfully(), expectSuccessfulFail(), or expectFail().
|
|
506
|
+
*
|
|
507
|
+
* @param fn
|
|
508
|
+
* @param strict
|
|
509
|
+
* @returns
|
|
510
|
+
*/
|
|
511
|
+
function shouldFail(fn) {
|
|
512
|
+
const usesDoneCallback = fn.length > 0;
|
|
513
|
+
// Return a function that checks arguments at runtime to avoid done callback deprecation warning
|
|
514
|
+
return async function () {
|
|
515
|
+
const {
|
|
516
|
+
done,
|
|
517
|
+
_promise
|
|
518
|
+
} = testDoneCallbackRef();
|
|
519
|
+
function handleError(e) {
|
|
520
|
+
if (!(e instanceof ExpectedFailError)) {
|
|
521
|
+
failWithTestDoneCallback(done, e);
|
|
522
|
+
} else {
|
|
523
|
+
done();
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
try {
|
|
527
|
+
const result = expectSuccessfulFail(() => {
|
|
528
|
+
let result;
|
|
529
|
+
if (usesDoneCallback) {
|
|
530
|
+
const fakeDone = build({
|
|
531
|
+
base: fakeDoneHandler(),
|
|
532
|
+
build: x => {
|
|
533
|
+
x.failSuccessfully = () => {
|
|
534
|
+
fakeDone(failSuccessfullyError());
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
const callbackWithDoneResult = fn(fakeDone);
|
|
539
|
+
if (isPromise(callbackWithDoneResult)) {
|
|
540
|
+
fakeDone.reject(new Error('Configured to use "done" value while returning a promise. Configure your test to use one or the other.'));
|
|
541
|
+
}
|
|
542
|
+
// return the fake done promise. Done/fail will resolve as a promise.
|
|
543
|
+
result = fakeDone._ref.promise;
|
|
544
|
+
} else {
|
|
545
|
+
result = fn();
|
|
546
|
+
}
|
|
547
|
+
return result;
|
|
548
|
+
}, handleError);
|
|
549
|
+
// If expectSuccessfulFail returns a promise, handle it
|
|
550
|
+
if (isPromise(result)) {
|
|
551
|
+
result.catch(handleError);
|
|
552
|
+
}
|
|
553
|
+
} catch (e) {
|
|
554
|
+
// Handle synchronous errors
|
|
555
|
+
handleError(e);
|
|
556
|
+
}
|
|
557
|
+
return _promise.promise;
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
function itShouldFail(describeOrFn, fn) {
|
|
561
|
+
let description;
|
|
562
|
+
if (typeof describeOrFn === 'string') {
|
|
563
|
+
description = `should fail ${describeOrFn}`;
|
|
564
|
+
} else {
|
|
565
|
+
fn = describeOrFn;
|
|
566
|
+
description = 'should fail';
|
|
567
|
+
}
|
|
568
|
+
it(description, shouldFail(fn));
|
|
569
|
+
}
|
|
570
|
+
function fakeDoneHandler() {
|
|
571
|
+
const promiseRef = promiseReference();
|
|
572
|
+
const doneHandler = promiseRef.resolve;
|
|
573
|
+
const failHandler = e => {
|
|
574
|
+
promiseRef.reject(e);
|
|
575
|
+
};
|
|
576
|
+
const fakeDone = error => {
|
|
577
|
+
if (error) {
|
|
578
|
+
failHandler(error);
|
|
579
|
+
} else {
|
|
580
|
+
doneHandler(0);
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
fakeDone.fail = error => {
|
|
584
|
+
failHandler(error);
|
|
585
|
+
};
|
|
586
|
+
fakeDone._ref = promiseRef;
|
|
587
|
+
fakeDone.promise = promiseRef.promise;
|
|
588
|
+
fakeDone.resolve = promiseRef.resolve;
|
|
589
|
+
fakeDone.reject = promiseRef.reject;
|
|
590
|
+
return fakeDone;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/**
|
|
594
|
+
* @deprecated Use AbstractTestContextFixture from shared instead. This is kept for backwards compatibility.
|
|
595
|
+
*/
|
|
596
|
+
class AbstractJestTestContextFixture extends AbstractTestContextFixture {}
|
|
597
|
+
/**
|
|
598
|
+
* @deprecated Use AbstractChildTestContextFixture from shared instead. This is kept for backwards compatibility.
|
|
599
|
+
*/
|
|
600
|
+
class AbstractChildJestTestContextFixture extends AbstractChildTestContextFixture {}
|
|
601
|
+
/**
|
|
602
|
+
* @deprecated Use testContextBuilder from shared instead. This is kept for backwards compatibility.
|
|
603
|
+
*/
|
|
604
|
+
const jestTestContextBuilder = testContextBuilder;
|
|
605
|
+
/**
|
|
606
|
+
* @deprecated Use useContextFixture from shared instead. This is kept for backwards compatibility.
|
|
607
|
+
*/
|
|
608
|
+
const useJestContextFixture = useTestContextFixture;
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* https://github.com/facebook/jest/issues/11698
|
|
612
|
+
*
|
|
613
|
+
* Since fail() was silently removed, we redefine it.
|
|
614
|
+
*/
|
|
615
|
+
/**
|
|
616
|
+
* @deprecated Use failWithTestDoneCallback from shared instead. This is kept for backwards compatibility.
|
|
617
|
+
*/
|
|
618
|
+
const failWithJestDoneCallback = failWithTestDoneCallback;
|
|
619
|
+
// MARK: Errors
|
|
620
|
+
/**
|
|
621
|
+
* @deprecated Use ExpectedFailError from shared instead. This is kept for backwards compatibility.
|
|
622
|
+
*/
|
|
623
|
+
class JestExpectedFailError extends ExpectedFailError {}
|
|
624
|
+
/**
|
|
625
|
+
* @deprecated Use UnexpectedSuccessFailureError from shared instead. This is kept for backwards compatibility.
|
|
626
|
+
*/
|
|
627
|
+
class JestUnexpectedSuccessFailureError extends UnexpectedSuccessFailureError {}
|
|
628
|
+
/**
|
|
629
|
+
* @deprecated Use ExpectedErrorOfSpecificTypeError from shared instead. This is kept for backwards compatibility.
|
|
630
|
+
*/
|
|
631
|
+
class JestExpectedErrorOfSpecificTypeError extends ExpectedErrorOfSpecificTypeError {}
|
|
632
|
+
/**
|
|
633
|
+
* @deprecated Use failWithTestDoneCallback with failDueToSuccessError from shared instead. This is kept for backwards compatibility.
|
|
634
|
+
*/
|
|
635
|
+
function failWithDoneDueToSuccess(done) {
|
|
636
|
+
failWithTestDoneCallback(done, failDueToSuccessError());
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* @deprecated Use expectFailAssertErrorType from shared instead. This is kept for backwards compatibility.
|
|
640
|
+
*/
|
|
641
|
+
const jestExpectFailAssertErrorType = expectFailAssertErrorType;
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Wraps the input JestTestContextFactory to emit another type of Fixture for tests.
|
|
645
|
+
*
|
|
646
|
+
* @deprecated Use wrapTestContextFactory from shared instead. This is kept for backwards compatibility.
|
|
647
|
+
* @returns
|
|
648
|
+
*/
|
|
649
|
+
const wrapJestTestContextFactory = wrapTestContextFactory;
|
|
650
|
+
/**
|
|
651
|
+
* @deprecated Use instanceWrapTestContextFactory from shared instead. This is kept for backwards compatibility.
|
|
652
|
+
*/
|
|
653
|
+
const instanceWrapJestTestContextFactory = instanceWrapTestContextFactory;
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* @deprecated Use useTestFunctionFixture from shared instead. This is kept for backwards compatibility.
|
|
657
|
+
*/
|
|
658
|
+
const useJestFunctionFixture = useTestFunctionFixture;
|
|
659
|
+
/**
|
|
660
|
+
* @deprecated Use useTestFunctionMapFixture from shared instead. This is kept for backwards compatibility.
|
|
661
|
+
*/
|
|
662
|
+
const useJestFunctionMapFixture = useTestFunctionMapFixture;
|
|
663
|
+
|
|
664
|
+
export { AbstractChildJestTestContextFixture, AbstractChildTestContextFixture, AbstractJestTestContextFixture, AbstractTestContextFixture, AbstractWrappedFixture, AbstractWrappedFixtureWithInstance, EXPECT_ERROR_DEFAULT_HANDLER, ExpectedErrorOfSpecificTypeError, ExpectedFailError, JestExpectedErrorOfSpecificTypeError, JestExpectedFailError, JestUnexpectedSuccessFailureError, UnexpectedSuccessFailureError, callbackTest, expectFail, expectFailAssertErrorType, expectSuccessfulFail, failDueToSuccess, failDueToSuccessError, failSuccessfully, failSuccessfullyError, failTest, failWithDoneDueToSuccess, failWithJestDoneCallback, failWithTestDoneCallback, fakeDoneHandler, instanceWrapJestTestContextFactory, instanceWrapTestContextFactory, itShouldFail, jestExpectFailAssertErrorType, jestTestContextBuilder, shouldFail, testContextBuilder, testDoneCallbackRef, useJestContextFixture, useJestFunctionFixture, useJestFunctionMapFixture, useTestContextFixture, useTestFunctionFixture, useTestFunctionMapFixture, wrapJestTestContextFactory, wrapTestContextFactory };
|
package/test/package.json
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dereekb/util/test",
|
|
3
|
-
"version": "
|
|
4
|
-
"type": "commonjs",
|
|
5
|
-
"peerDependencies": {
|
|
6
|
-
"@dereekb/util": "*"
|
|
7
|
-
},
|
|
3
|
+
"version": "13.0.0",
|
|
8
4
|
"types": "./src/index.d.ts",
|
|
9
|
-
"
|
|
5
|
+
"module": "./index.esm.js",
|
|
6
|
+
"main": "./index.cjs.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./src/index.d.ts",
|
|
10
|
+
"node": {
|
|
11
|
+
"require": "./index.cjs.js"
|
|
12
|
+
},
|
|
13
|
+
"browser": {
|
|
14
|
+
"require": "./index.cjs.js",
|
|
15
|
+
"import": "./index.esm.js"
|
|
16
|
+
},
|
|
17
|
+
"default": "./index.cjs.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"@dereekb/util": "13.0.0",
|
|
22
|
+
"core-js": "^3.0.0"
|
|
23
|
+
}
|
|
10
24
|
}
|
package/test/src/lib/index.d.ts
CHANGED