@dereekb/util 10.0.6 → 10.0.8

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/test/index.cjs.js DELETED
@@ -1,555 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var util = require('@dereekb/util');
6
-
7
- /******************************************************************************
8
- Copyright (c) Microsoft Corporation.
9
-
10
- Permission to use, copy, modify, and/or distribute this software for any
11
- purpose with or without fee is hereby granted.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
- PERFORMANCE OF THIS SOFTWARE.
20
- ***************************************************************************** */
21
-
22
- function __awaiter(thisArg, _arguments, P, generator) {
23
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
- return new (P || (P = Promise))(function (resolve, reject) {
25
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
- step((generator = generator.apply(thisArg, _arguments || [])).next());
29
- });
30
- }
31
-
32
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
33
- var e = new Error(message);
34
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
35
- };
36
-
37
- /**
38
- * Abstract JestTestContextFixture instance.
39
- */
40
- class AbstractJestTestContextFixture {
41
- get instance() {
42
- return this._instance;
43
- }
44
- setInstance(instance) {
45
- if (this._instance != null) {
46
- throw new Error(`The testing fixture is locked. Don't call setInstance() directly.`);
47
- }
48
- this._instance = instance;
49
- return () => {
50
- delete this._instance;
51
- };
52
- }
53
- }
54
- /**
55
- * Abstract JestTestContextFixture instance with a parent.
56
- */
57
- class AbstractChildJestTestContextFixture extends AbstractJestTestContextFixture {
58
- constructor(parent) {
59
- super();
60
- this.parent = parent;
61
- }
62
- }
63
- /**
64
- * Creates a JestTestContextBuilderFunction given the input builder.
65
- *
66
- * @param builder
67
- * @returns
68
- */
69
- function jestTestContextBuilder(builder) {
70
- return inputConfig => {
71
- const config = builder.buildConfig(inputConfig);
72
- return buildTests => {
73
- const fixture = builder.buildFixture(config);
74
- // add before each
75
- if (builder.beforeEach != null) {
76
- beforeEach(builder.beforeEach);
77
- }
78
- // add tests
79
- useJestContextFixture({
80
- fixture,
81
- /**
82
- * Build tests by passing the fixture to the testing functions.
83
- *
84
- * This will inject all tests and sub Jest lifecycle items.
85
- */
86
- buildTests,
87
- initInstance: () => builder.setupInstance(config),
88
- destroyInstance: instance => builder.teardownInstance(instance, config)
89
- });
90
- // add after each
91
- if (builder.afterEach != null) {
92
- afterEach(builder.afterEach);
93
- }
94
- };
95
- };
96
- }
97
- /**
98
- * Creates a test context and jest configurations that will initialize an instance
99
- */
100
- function useJestContextFixture(config) {
101
- const {
102
- buildTests,
103
- fixture,
104
- initInstance,
105
- destroyInstance
106
- } = config;
107
- let clearInstance;
108
- let instance;
109
- // Create an instance
110
- beforeEach(() => __awaiter(this, void 0, void 0, function* () {
111
- try {
112
- instance = yield initInstance();
113
- clearInstance = fixture.setInstance(instance);
114
- } catch (e) {
115
- console.error('Failed building a test instance due to an error in buildInstance(). Error: ', e);
116
- if (clearInstance) {
117
- clearInstance();
118
- }
119
- throw e;
120
- }
121
- }));
122
- // Declare tests
123
- buildTests(fixture);
124
- // Cleanup
125
- afterEach(() => __awaiter(this, void 0, void 0, function* () {
126
- if (clearInstance) {
127
- clearInstance();
128
- }
129
- if (fixture.instance != null) {
130
- console.warn('Expected instance to be set on fixture for cleanup but was set to something else.');
131
- }
132
- if (destroyInstance) {
133
- try {
134
- yield destroyInstance(instance);
135
- instance = undefined;
136
- } catch (e) {
137
- console.error('Failed due to error in destroyInstance()');
138
- throw e;
139
- }
140
- }
141
- }));
142
- }
143
-
144
- var makeError = {exports: {}};
145
-
146
- (function (module, exports) {
147
-
148
- // ===================================================================
149
-
150
- var construct = typeof Reflect !== "undefined" ? Reflect.construct : undefined;
151
- var defineProperty = Object.defineProperty;
152
-
153
- // -------------------------------------------------------------------
154
-
155
- var captureStackTrace = Error.captureStackTrace;
156
- if (captureStackTrace === undefined) {
157
- captureStackTrace = function captureStackTrace(error) {
158
- var container = new Error();
159
-
160
- defineProperty(error, "stack", {
161
- configurable: true,
162
- get: function getStack() {
163
- var stack = container.stack;
164
-
165
- // Replace property with value for faster future accesses.
166
- defineProperty(this, "stack", {
167
- configurable: true,
168
- value: stack,
169
- writable: true,
170
- });
171
-
172
- return stack;
173
- },
174
- set: function setStack(stack) {
175
- defineProperty(error, "stack", {
176
- configurable: true,
177
- value: stack,
178
- writable: true,
179
- });
180
- },
181
- });
182
- };
183
- }
184
-
185
- // -------------------------------------------------------------------
186
-
187
- function BaseError(message) {
188
- if (message !== undefined) {
189
- defineProperty(this, "message", {
190
- configurable: true,
191
- value: message,
192
- writable: true,
193
- });
194
- }
195
-
196
- var cname = this.constructor.name;
197
- if (cname !== undefined && cname !== this.name) {
198
- defineProperty(this, "name", {
199
- configurable: true,
200
- value: cname,
201
- writable: true,
202
- });
203
- }
204
-
205
- captureStackTrace(this, this.constructor);
206
- }
207
-
208
- BaseError.prototype = Object.create(Error.prototype, {
209
- // See: https://github.com/JsCommunity/make-error/issues/4
210
- constructor: {
211
- configurable: true,
212
- value: BaseError,
213
- writable: true,
214
- },
215
- });
216
-
217
- // -------------------------------------------------------------------
218
-
219
- // Sets the name of a function if possible (depends of the JS engine).
220
- var setFunctionName = (function() {
221
- function setFunctionName(fn, name) {
222
- return defineProperty(fn, "name", {
223
- configurable: true,
224
- value: name,
225
- });
226
- }
227
- try {
228
- var f = function() {};
229
- setFunctionName(f, "foo");
230
- if (f.name === "foo") {
231
- return setFunctionName;
232
- }
233
- } catch (_) {}
234
- })();
235
-
236
- // -------------------------------------------------------------------
237
-
238
- function makeError(constructor, super_) {
239
- if (super_ == null || super_ === Error) {
240
- super_ = BaseError;
241
- } else if (typeof super_ !== "function") {
242
- throw new TypeError("super_ should be a function");
243
- }
244
-
245
- var name;
246
- if (typeof constructor === "string") {
247
- name = constructor;
248
- constructor =
249
- construct !== undefined
250
- ? function() {
251
- return construct(super_, arguments, this.constructor);
252
- }
253
- : function() {
254
- super_.apply(this, arguments);
255
- };
256
-
257
- // If the name can be set, do it once and for all.
258
- if (setFunctionName !== undefined) {
259
- setFunctionName(constructor, name);
260
- name = undefined;
261
- }
262
- } else if (typeof constructor !== "function") {
263
- throw new TypeError("constructor should be either a string or a function");
264
- }
265
-
266
- // Also register the super constructor also as `constructor.super_` just
267
- // like Node's `util.inherits()`.
268
- //
269
- // eslint-disable-next-line dot-notation
270
- constructor.super_ = constructor["super"] = super_;
271
-
272
- var properties = {
273
- constructor: {
274
- configurable: true,
275
- value: constructor,
276
- writable: true,
277
- },
278
- };
279
-
280
- // If the name could not be set on the constructor, set it on the
281
- // prototype.
282
- if (name !== undefined) {
283
- properties.name = {
284
- configurable: true,
285
- value: name,
286
- writable: true,
287
- };
288
- }
289
- constructor.prototype = Object.create(super_.prototype, properties);
290
-
291
- return constructor;
292
- }
293
- exports = module.exports = makeError;
294
- exports.BaseError = BaseError;
295
- }(makeError, makeError.exports));
296
-
297
- /**
298
- * https://github.com/facebook/jest/issues/11698
299
- *
300
- * Since fail() was silently removed, we redefine it.
301
- */
302
- /**
303
- * Passes the error to the JestDoneCallback.
304
- * @param done
305
- * @param e
306
- */
307
- function failWithJestDoneCallback(done, e = new Error('failed test')) {
308
- if (done.fail != null) {
309
- done.fail(e);
310
- } else {
311
- done(e);
312
- }
313
- }
314
- // MARK: Errors
315
- /**
316
- * Error thrown by fail() and used by expectError()
317
- */
318
- class JestExpectedFailError extends makeError.exports.BaseError {}
319
- function failSuccessfullyError(message) {
320
- return new JestExpectedFailError(message);
321
- }
322
- function failSuccessfully(message) {
323
- throw failSuccessfullyError(message);
324
- }
325
- /**
326
- * Error thrown when success occurs when it should not have.
327
- */
328
- class JestUnexpectedSuccessFailureError extends makeError.exports.BaseError {}
329
- function failDueToSuccessError(message) {
330
- return new JestUnexpectedSuccessFailureError(message !== null && message !== void 0 ? message : 'expected an error to occur but was successful instead');
331
- }
332
- function failTest(message) {
333
- throw failDueToSuccessError(message);
334
- }
335
- function failDueToSuccess() {
336
- throw failDueToSuccessError();
337
- }
338
- function failWithDoneDueToSuccess(done) {
339
- failWithJestDoneCallback(done, failDueToSuccessError());
340
- }
341
- function EXPECT_ERROR_DEFAULT_HANDLER(e) {
342
- if (e instanceof JestExpectedFailError) ; else {
343
- throw e;
344
- }
345
- }
346
- function expectFail(errorFn) {
347
- function handleError(e) {
348
- if (e instanceof JestUnexpectedSuccessFailureError) {
349
- throw e;
350
- } else {
351
- failSuccessfully();
352
- }
353
- }
354
- try {
355
- const result = errorFn();
356
- if (util.isPromise(result)) {
357
- return result.then(failDueToSuccess).catch(handleError);
358
- } else {
359
- failDueToSuccess();
360
- }
361
- } catch (e) {
362
- handleError(e);
363
- }
364
- }
365
- function expectSuccessfulFail(errorFn, handleError = EXPECT_ERROR_DEFAULT_HANDLER) {
366
- try {
367
- const result = errorFn();
368
- if (util.isPromise(result)) {
369
- return result.then(failDueToSuccess).catch(handleError);
370
- } else {
371
- failDueToSuccess();
372
- }
373
- } catch (e) {
374
- handleError(e);
375
- }
376
- }
377
- /**
378
- * Used to wrap a Jest testing function and watch for JestExpectedFailError errors in order to pass the test. Other exceptions are treated normally as failures.
379
- *
380
- * This is typically used in conjunction with failSuccessfully(), expectSuccessfulFail(), or expectFail().
381
- *
382
- * @param fn
383
- * @param strict
384
- * @returns
385
- */
386
- function shouldFail(fn) {
387
- const usesDoneCallback = fn.length > 0;
388
- return done => {
389
- function handleError(e) {
390
- if (!(e instanceof JestExpectedFailError)) {
391
- failWithJestDoneCallback(done, e);
392
- } else {
393
- done();
394
- }
395
- }
396
- expectSuccessfulFail(() => {
397
- let result;
398
- if (usesDoneCallback) {
399
- const fakeDone = util.build({
400
- base: fakeDoneHandler(),
401
- build: x => {
402
- x.failSuccessfully = () => {
403
- fakeDone(failSuccessfullyError());
404
- };
405
- }
406
- });
407
- const callbackWithDoneResult = fn(fakeDone);
408
- if (util.isPromise(callbackWithDoneResult)) {
409
- fakeDone.reject(new Error('Configured to use "done" value while returning a promise. Configure your test to use one or the other.'));
410
- }
411
- // return the fake done promise. Done/fail will resolve as a promise.
412
- result = fakeDone._ref.promise;
413
- } else {
414
- result = fn();
415
- }
416
- return result;
417
- }, handleError);
418
- };
419
- }
420
- function itShouldFail(describeOrFn, fn) {
421
- let description;
422
- if (typeof describeOrFn === 'string') {
423
- description = `should fail ${describeOrFn}`;
424
- } else {
425
- fn = describeOrFn;
426
- description = 'should fail';
427
- }
428
- it(description, shouldFail(fn));
429
- }
430
- function fakeDoneHandler() {
431
- const promiseRef = util.promiseReference();
432
- const doneHandler = promiseRef.resolve;
433
- const failHandler = e => {
434
- promiseRef.reject(e);
435
- };
436
- const fakeDone = error => {
437
- if (error) {
438
- failHandler(error);
439
- } else {
440
- doneHandler(0);
441
- }
442
- };
443
- fakeDone.fail = error => {
444
- failHandler(error);
445
- };
446
- fakeDone._ref = promiseRef;
447
- fakeDone.promise = promiseRef.promise;
448
- fakeDone.resolve = promiseRef.resolve;
449
- fakeDone.reject = promiseRef.reject;
450
- return fakeDone;
451
- }
452
-
453
- class AbstractWrappedFixture {
454
- constructor(fixture) {
455
- this.fixture = fixture;
456
- }
457
- }
458
- class AbstractWrappedFixtureWithInstance extends AbstractJestTestContextFixture {
459
- constructor(parent) {
460
- super();
461
- this.parent = parent;
462
- }
463
- }
464
- /**
465
- * Wraps the input JestTestContextFactory to emit another type of Fixture for tests.
466
- *
467
- * @returns
468
- */
469
- function wrapJestTestContextFactory(config) {
470
- return factory => {
471
- return buildTests => {
472
- factory(inputFixture => {
473
- const wrap = config.wrapFixture(inputFixture);
474
- let effect;
475
- // add before each
476
- if (config.setupWrap != null) {
477
- beforeEach(() => __awaiter(this, void 0, void 0, function* () {
478
- effect = yield config.setupWrap(wrap);
479
- }));
480
- }
481
- // add tests
482
- buildTests(wrap);
483
- // add after each
484
- if (config.teardownWrap != null) {
485
- afterEach(() => __awaiter(this, void 0, void 0, function* () {
486
- yield config.teardownWrap(wrap, effect);
487
- }));
488
- }
489
- });
490
- };
491
- };
492
- }
493
- function instanceWrapJestTestContextFactory(config) {
494
- return wrapJestTestContextFactory({
495
- wrapFixture: config.wrapFixture,
496
- setupWrap: wrap => __awaiter(this, void 0, void 0, function* () {
497
- const instance = yield config.makeInstance(wrap);
498
- const effect = wrap.setInstance(instance);
499
- if (config.setupInstance) {
500
- yield config.setupInstance(instance, wrap);
501
- }
502
- return effect;
503
- }),
504
- teardownWrap: (wrap, deleteInstanceEffect) => __awaiter(this, void 0, void 0, function* () {
505
- deleteInstanceEffect === null || deleteInstanceEffect === void 0 ? void 0 : deleteInstanceEffect();
506
- if (config.teardownInstance) {
507
- yield config.teardownInstance(wrap.instance);
508
- }
509
- })
510
- });
511
- }
512
-
513
- /**
514
- * Creates a test context and jest configurations that provides a function to build tests based on the configuration.
515
- */
516
- function useJestFunctionFixture(config, buildTests) {
517
- const {
518
- fn
519
- } = config;
520
- const forward = util.forwardFunction(fn);
521
- buildTests(forward);
522
- }
523
- /**
524
- * Creates a test context and jest configurations that provides a function to build tests based on the configuration.
525
- */
526
- function useJestFunctionMapFixture(config, buildTests) {
527
- const forwardedFunctions = util.mapObjectMap(config.fns, fn => util.forwardFunction(fn));
528
- buildTests(forwardedFunctions);
529
- }
530
-
531
- exports.AbstractChildJestTestContextFixture = AbstractChildJestTestContextFixture;
532
- exports.AbstractJestTestContextFixture = AbstractJestTestContextFixture;
533
- exports.AbstractWrappedFixture = AbstractWrappedFixture;
534
- exports.AbstractWrappedFixtureWithInstance = AbstractWrappedFixtureWithInstance;
535
- exports.EXPECT_ERROR_DEFAULT_HANDLER = EXPECT_ERROR_DEFAULT_HANDLER;
536
- exports.JestExpectedFailError = JestExpectedFailError;
537
- exports.JestUnexpectedSuccessFailureError = JestUnexpectedSuccessFailureError;
538
- exports.expectFail = expectFail;
539
- exports.expectSuccessfulFail = expectSuccessfulFail;
540
- exports.failDueToSuccess = failDueToSuccess;
541
- exports.failDueToSuccessError = failDueToSuccessError;
542
- exports.failSuccessfully = failSuccessfully;
543
- exports.failSuccessfullyError = failSuccessfullyError;
544
- exports.failTest = failTest;
545
- exports.failWithDoneDueToSuccess = failWithDoneDueToSuccess;
546
- exports.failWithJestDoneCallback = failWithJestDoneCallback;
547
- exports.fakeDoneHandler = fakeDoneHandler;
548
- exports.instanceWrapJestTestContextFactory = instanceWrapJestTestContextFactory;
549
- exports.itShouldFail = itShouldFail;
550
- exports.jestTestContextBuilder = jestTestContextBuilder;
551
- exports.shouldFail = shouldFail;
552
- exports.useJestContextFixture = useJestContextFixture;
553
- exports.useJestFunctionFixture = useJestFunctionFixture;
554
- exports.useJestFunctionMapFixture = useJestFunctionMapFixture;
555
- exports.wrapJestTestContextFactory = wrapJestTestContextFactory;
@@ -1,2 +0,0 @@
1
- export * from './index.cjs.js';
2
- export { _default as default } from './index.cjs.default.js';