@dereekb/util 8.0.0 → 8.1.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/CHANGELOG.md +18 -0
- package/package.json +1 -1
- package/src/lib/promise/is.d.ts +2 -1
- package/src/lib/promise/is.js +6 -1
- package/src/lib/promise/is.js.map +1 -1
- package/src/lib/promise/promise.ref.d.ts +14 -3
- package/src/lib/promise/promise.ref.js +8 -4
- package/src/lib/promise/promise.ref.js.map +1 -1
- package/test/CHANGELOG.md +13 -0
- package/test/package.json +2 -2
- package/test/src/lib/index.d.ts +1 -0
- package/test/src/lib/index.js +1 -0
- package/test/src/lib/index.js.map +1 -1
- package/test/src/lib/jest.fail.d.ts +81 -0
- package/test/src/lib/jest.fail.js +191 -0
- package/test/src/lib/jest.fail.js.map +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
# [8.1.0](https://github.com/dereekb/dbx-components/compare/v8.0.1-dev...v8.1.0) (2022-06-18)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* fixed issue with snapshot falsy default values being ignored ([b433bc4](https://github.com/dereekb/dbx-components/commit/b433bc4a63b04d5aab99e1cf67b058cf20e7cc6a))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Features
|
|
14
|
+
|
|
15
|
+
* added jest fail test utilities ([#13](https://github.com/dereekb/dbx-components/issues/13)) ([5891777](https://github.com/dereekb/dbx-components/commit/5891777470a339892c8e7045c24b5dea174b1736))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
## [8.0.1](https://github.com/dereekb/dbx-components/compare/v8.0.0-dev...v8.0.1) (2022-06-17)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
5
23
|
# [8.0.0](https://github.com/dereekb/dbx-components/compare/v7.16.0-dev...v8.0.0) (2022-06-17)
|
|
6
24
|
|
|
7
25
|
|
package/package.json
CHANGED
package/src/lib/promise/is.d.ts
CHANGED
|
@@ -4,4 +4,5 @@
|
|
|
4
4
|
* @param obj
|
|
5
5
|
* @returns
|
|
6
6
|
*/
|
|
7
|
-
export declare function isPromise<T, S>(obj:
|
|
7
|
+
export declare function isPromise<T, S>(obj: Promise<T> | S): obj is Promise<T>;
|
|
8
|
+
export declare function isPromiseLike<T, S>(obj: PromiseLike<T> | S): obj is PromiseLike<T>;
|
package/src/lib/promise/is.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isPromise = void 0;
|
|
3
|
+
exports.isPromiseLike = exports.isPromise = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Whether or not the input is function-like.
|
|
6
6
|
*
|
|
@@ -12,4 +12,9 @@ function isPromise(obj) {
|
|
|
12
12
|
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
|
13
13
|
}
|
|
14
14
|
exports.isPromise = isPromise;
|
|
15
|
+
function isPromiseLike(obj) {
|
|
16
|
+
// https://github.com/then/is-promise
|
|
17
|
+
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
|
18
|
+
}
|
|
19
|
+
exports.isPromiseLike = isPromiseLike;
|
|
15
20
|
//# sourceMappingURL=is.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"is.js","sourceRoot":"","sources":["../../../../../../packages/util/src/lib/promise/is.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,SAAS,CAAO,GAAuB;
|
|
1
|
+
{"version":3,"file":"is.js","sourceRoot":"","sources":["../../../../../../packages/util/src/lib/promise/is.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,SAAgB,SAAS,CAAO,GAAmB;IACjD,qCAAqC;IACrC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,UAAU,CAAC,IAAI,OAAQ,GAAsB,CAAC,IAAI,KAAK,UAAU,CAAC;AAC/H,CAAC;AAHD,8BAGC;AAED,SAAgB,aAAa,CAAO,GAAuB;IACzD,qCAAqC;IACrC,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,GAAG,KAAK,UAAU,CAAC,IAAI,OAAQ,GAAsB,CAAC,IAAI,KAAK,UAAU,CAAC;AAC/H,CAAC;AAHD,sCAGC"}
|
|
@@ -1,10 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* A reference to a Promise and its resolve/reject functions.
|
|
3
|
+
*/
|
|
4
|
+
export declare type PromiseReference<O = unknown> = {
|
|
2
5
|
readonly promise: Promise<O>;
|
|
3
6
|
readonly resolve: (value: O | PromiseLike<O>) => void;
|
|
4
7
|
readonly reject: (reason?: unknown) => void;
|
|
5
|
-
}
|
|
8
|
+
};
|
|
6
9
|
export declare type PromiseExecutor<O> = (resolve: (value: O | PromiseLike<O>) => void, reject: (reason?: unknown) => void) => void;
|
|
7
10
|
/**
|
|
8
11
|
* Creates a new promise and returns the full ref for it.
|
|
9
12
|
*/
|
|
10
|
-
export declare function
|
|
13
|
+
export declare function promiseReference<O>(executor?: PromiseExecutor<O>): PromiseReference<O>;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated use PromiseReference instead.
|
|
16
|
+
*/
|
|
17
|
+
export declare type PromiseFullRef<O = unknown> = PromiseReference<O>;
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated use promiseReference() instead.
|
|
20
|
+
*/
|
|
21
|
+
export declare const makePromiseFullRef: typeof promiseReference;
|
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.makePromiseFullRef = void 0;
|
|
3
|
+
exports.makePromiseFullRef = exports.promiseReference = void 0;
|
|
4
4
|
let PROMISE_REF_NUMBER = 0;
|
|
5
5
|
/**
|
|
6
6
|
* Creates a new promise and returns the full ref for it.
|
|
7
7
|
*/
|
|
8
|
-
function
|
|
8
|
+
function promiseReference(executor) {
|
|
9
9
|
const ref = {};
|
|
10
10
|
ref.promise = new Promise((resolve, reject) => {
|
|
11
11
|
ref.resolve = resolve;
|
|
12
12
|
ref.reject = reject;
|
|
13
|
-
executor(resolve, reject);
|
|
13
|
+
executor === null || executor === void 0 ? void 0 : executor(resolve, reject);
|
|
14
14
|
});
|
|
15
15
|
ref.number = PROMISE_REF_NUMBER += 1; // added for debugging
|
|
16
16
|
return ref;
|
|
17
17
|
}
|
|
18
|
-
exports.
|
|
18
|
+
exports.promiseReference = promiseReference;
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated use promiseReference() instead.
|
|
21
|
+
*/
|
|
22
|
+
exports.makePromiseFullRef = promiseReference;
|
|
19
23
|
//# sourceMappingURL=promise.ref.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise.ref.js","sourceRoot":"","sources":["../../../../../../packages/util/src/lib/promise/promise.ref.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"promise.ref.js","sourceRoot":"","sources":["../../../../../../packages/util/src/lib/promise/promise.ref.ts"],"names":[],"mappings":";;;AAaA,IAAI,kBAAkB,GAAG,CAAC,CAAC;AAE3B;;GAEG;AACH,SAAgB,gBAAgB,CAAI,QAA6B;IAC/D,MAAM,GAAG,GAAG,EAA4D,CAAC;IAEzE,GAAG,CAAC,OAAO,GAAG,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC5C,GAAG,CAAC,OAAO,GAAG,OAAO,CAAC;QACtB,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;QACpB,QAAQ,aAAR,QAAQ,uBAAR,QAAQ,CAAG,OAAO,EAAE,MAAM,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,GAAG,CAAC,MAAM,GAAG,kBAAkB,IAAI,CAAC,CAAC,CAAC,sBAAsB;IAE5D,OAAO,GAAG,CAAC;AACb,CAAC;AAZD,4CAYC;AAQD;;GAEG;AACU,QAAA,kBAAkB,GAAG,gBAAgB,CAAC"}
|
package/test/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
4
|
|
|
5
|
+
# [8.1.0](https://github.com/dereekb/dbx-components/compare/v8.0.1-dev...v8.1.0) (2022-06-18)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* added jest fail test utilities ([#13](https://github.com/dereekb/dbx-components/issues/13)) ([5891777](https://github.com/dereekb/dbx-components/commit/5891777470a339892c8e7045c24b5dea174b1736))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
## [8.0.1](https://github.com/dereekb/dbx-components/compare/v8.0.0-dev...v8.0.1) (2022-06-17)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
5
18
|
# [8.0.0](https://github.com/dereekb/dbx-components/compare/v7.16.0-dev...v8.0.0) (2022-06-17)
|
|
6
19
|
|
|
7
20
|
|
package/test/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dereekb/util/test",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.1.0",
|
|
4
4
|
"main": "./src/index.js",
|
|
5
5
|
"typings": "./src/index.d.ts",
|
|
6
6
|
"dependencies": {},
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@dereekb/util": "8.
|
|
8
|
+
"@dereekb/util": "8.1.0",
|
|
9
9
|
"make-error": "^1.3.0",
|
|
10
10
|
"ts-essentials": "^9.1.2",
|
|
11
11
|
"extra-set": "^2.2.11",
|
package/test/src/lib/index.d.ts
CHANGED
package/test/src/lib/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./jest"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./jest.fail"), exports);
|
|
5
6
|
tslib_1.__exportStar(require("./jest.wrap"), exports);
|
|
6
7
|
tslib_1.__exportStar(require("./jest.function"), exports);
|
|
7
8
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/util/test/src/lib/index.ts"],"names":[],"mappings":";;;AAAA,iDAAuB;AACvB,sDAA4B;AAC5B,0DAAgC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/util/test/src/lib/index.ts"],"names":[],"mappings":";;;AAAA,iDAAuB;AACvB,sDAA4B;AAC5B,sDAA4B;AAC5B,0DAAgC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* https://github.com/facebook/jest/issues/11698
|
|
3
|
+
*
|
|
4
|
+
* Since fail() was silently removed, we redefine it.
|
|
5
|
+
*/
|
|
6
|
+
import { PromiseReference, PromiseOrValue } from '@dereekb/util';
|
|
7
|
+
import { BaseError } from 'make-error';
|
|
8
|
+
export interface JestDoneCallback {
|
|
9
|
+
(...args: any[]): any;
|
|
10
|
+
/**
|
|
11
|
+
* NOTE: Not typically available in Jest, but here for legacy purposes.
|
|
12
|
+
*
|
|
13
|
+
* @param error
|
|
14
|
+
*/
|
|
15
|
+
fail(error?: string | {
|
|
16
|
+
message: string;
|
|
17
|
+
}): any;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Passes the error to the JestDoneCallback.
|
|
21
|
+
* @param done
|
|
22
|
+
* @param e
|
|
23
|
+
*/
|
|
24
|
+
export declare function failWithJestDoneCallback(done: JestDoneCallback, e?: unknown): void;
|
|
25
|
+
export declare type JestProvidesCallbackWithDone = (cb: JestDoneCallback) => void | undefined;
|
|
26
|
+
export declare type JestProvidesCallback = JestProvidesCallbackWithDone | (() => Promise<unknown>);
|
|
27
|
+
/**
|
|
28
|
+
* Error thrown by fail() and used by expectError()
|
|
29
|
+
*/
|
|
30
|
+
export declare class JestExpectedFailError extends BaseError {
|
|
31
|
+
}
|
|
32
|
+
export declare function failSuccessfullyError(message?: string): JestExpectedFailError;
|
|
33
|
+
export declare function failSuccessfully(message?: string): never;
|
|
34
|
+
/**
|
|
35
|
+
* Error thrown when success occurs when it should not have.
|
|
36
|
+
*/
|
|
37
|
+
export declare class JestUnexpectedSuccessFailureError extends BaseError {
|
|
38
|
+
}
|
|
39
|
+
export declare function failDueToSuccessError(message?: string): JestUnexpectedSuccessFailureError;
|
|
40
|
+
export declare function failTest(message?: string): never;
|
|
41
|
+
export declare function failDueToSuccess(): never;
|
|
42
|
+
export declare function failWithDoneDueToSuccess(done: JestDoneCallback): void;
|
|
43
|
+
export declare function EXPECT_ERROR_DEFAULT_HANDLER(e: unknown): void;
|
|
44
|
+
/**
|
|
45
|
+
* Function that expects any failure to be thrown, then throws a JestExpectedFailError.
|
|
46
|
+
*
|
|
47
|
+
* @param errorFn
|
|
48
|
+
* @param handleError
|
|
49
|
+
*/
|
|
50
|
+
export declare function expectFail(errorFn: () => void): void;
|
|
51
|
+
export declare function expectFail(errorFn: () => Promise<void>): Promise<void>;
|
|
52
|
+
/**
|
|
53
|
+
* Function that expects a JestExpectedFailError to be thrown.
|
|
54
|
+
*
|
|
55
|
+
* @param errorFn
|
|
56
|
+
* @param handleError
|
|
57
|
+
*/
|
|
58
|
+
export declare function expectSuccessfulFail(errorFn: () => void, handleError?: (e: unknown) => void): void;
|
|
59
|
+
export declare function expectSuccessfulFail(errorFn: () => Promise<void>, handleError?: (e: unknown) => void): Promise<void>;
|
|
60
|
+
export interface JestShouldFailDoneCallback extends JestDoneCallback {
|
|
61
|
+
failSuccessfully(): void;
|
|
62
|
+
}
|
|
63
|
+
export declare type JestShouldFailProvidesCallbackWithDone = (cb: JestShouldFailDoneCallback) => void | undefined;
|
|
64
|
+
export declare type JestShouldFailProvidesCallbackWithResult = () => PromiseOrValue<unknown>;
|
|
65
|
+
export declare type JestShouldFailProvidesCallback = JestShouldFailProvidesCallbackWithDone | JestShouldFailProvidesCallbackWithResult;
|
|
66
|
+
/**
|
|
67
|
+
* 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.
|
|
68
|
+
*
|
|
69
|
+
* This is typically used in conjunction with failSuccessfully(), expectSuccessfulFail(), or expectFail().
|
|
70
|
+
*
|
|
71
|
+
* @param fn
|
|
72
|
+
* @param strict
|
|
73
|
+
* @returns
|
|
74
|
+
*/
|
|
75
|
+
export declare function shouldFail(fn: JestShouldFailProvidesCallback): JestProvidesCallback;
|
|
76
|
+
export declare function itShouldFail(fn: JestShouldFailProvidesCallback): void;
|
|
77
|
+
export declare function itShouldFail(describe: string, fn: JestShouldFailProvidesCallback): void;
|
|
78
|
+
export interface JestFakeDoneHandler extends JestDoneCallback, PromiseReference {
|
|
79
|
+
_ref: PromiseReference;
|
|
80
|
+
}
|
|
81
|
+
export declare function fakeDoneHandler(): JestFakeDoneHandler;
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* https://github.com/facebook/jest/issues/11698
|
|
4
|
+
*
|
|
5
|
+
* Since fail() was silently removed, we redefine it.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.fakeDoneHandler = exports.itShouldFail = exports.shouldFail = exports.expectSuccessfulFail = exports.expectFail = exports.EXPECT_ERROR_DEFAULT_HANDLER = exports.failWithDoneDueToSuccess = exports.failDueToSuccess = exports.failTest = exports.failDueToSuccessError = exports.JestUnexpectedSuccessFailureError = exports.failSuccessfully = exports.failSuccessfullyError = exports.JestExpectedFailError = exports.failWithJestDoneCallback = void 0;
|
|
9
|
+
const util_1 = require("@dereekb/util");
|
|
10
|
+
const make_error_1 = require("make-error");
|
|
11
|
+
/**
|
|
12
|
+
* Passes the error to the JestDoneCallback.
|
|
13
|
+
* @param done
|
|
14
|
+
* @param e
|
|
15
|
+
*/
|
|
16
|
+
function failWithJestDoneCallback(done, e = new Error('failed test')) {
|
|
17
|
+
if (done.fail != null) {
|
|
18
|
+
done.fail(e);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
done(e);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.failWithJestDoneCallback = failWithJestDoneCallback;
|
|
25
|
+
// MARK: Errors
|
|
26
|
+
/**
|
|
27
|
+
* Error thrown by fail() and used by expectError()
|
|
28
|
+
*/
|
|
29
|
+
class JestExpectedFailError extends make_error_1.BaseError {
|
|
30
|
+
}
|
|
31
|
+
exports.JestExpectedFailError = JestExpectedFailError;
|
|
32
|
+
function failSuccessfullyError(message) {
|
|
33
|
+
return new JestExpectedFailError(message);
|
|
34
|
+
}
|
|
35
|
+
exports.failSuccessfullyError = failSuccessfullyError;
|
|
36
|
+
function failSuccessfully(message) {
|
|
37
|
+
throw failSuccessfullyError(message);
|
|
38
|
+
}
|
|
39
|
+
exports.failSuccessfully = failSuccessfully;
|
|
40
|
+
/**
|
|
41
|
+
* Error thrown when success occurs when it should not have.
|
|
42
|
+
*/
|
|
43
|
+
class JestUnexpectedSuccessFailureError extends make_error_1.BaseError {
|
|
44
|
+
}
|
|
45
|
+
exports.JestUnexpectedSuccessFailureError = JestUnexpectedSuccessFailureError;
|
|
46
|
+
function failDueToSuccessError(message) {
|
|
47
|
+
return new JestUnexpectedSuccessFailureError(message !== null && message !== void 0 ? message : 'expected an error to occur but was successful instead');
|
|
48
|
+
}
|
|
49
|
+
exports.failDueToSuccessError = failDueToSuccessError;
|
|
50
|
+
function failTest(message) {
|
|
51
|
+
throw failDueToSuccessError(message);
|
|
52
|
+
}
|
|
53
|
+
exports.failTest = failTest;
|
|
54
|
+
function failDueToSuccess() {
|
|
55
|
+
throw failDueToSuccessError();
|
|
56
|
+
}
|
|
57
|
+
exports.failDueToSuccess = failDueToSuccess;
|
|
58
|
+
function failWithDoneDueToSuccess(done) {
|
|
59
|
+
failWithJestDoneCallback(done, failDueToSuccessError());
|
|
60
|
+
}
|
|
61
|
+
exports.failWithDoneDueToSuccess = failWithDoneDueToSuccess;
|
|
62
|
+
function EXPECT_ERROR_DEFAULT_HANDLER(e) {
|
|
63
|
+
if (e instanceof JestExpectedFailError) {
|
|
64
|
+
// success
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
throw e;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.EXPECT_ERROR_DEFAULT_HANDLER = EXPECT_ERROR_DEFAULT_HANDLER;
|
|
71
|
+
function expectFail(errorFn) {
|
|
72
|
+
function handleError(e) {
|
|
73
|
+
if (e instanceof JestUnexpectedSuccessFailureError) {
|
|
74
|
+
throw e;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
failSuccessfully();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
try {
|
|
81
|
+
const result = errorFn();
|
|
82
|
+
if ((0, util_1.isPromise)(result)) {
|
|
83
|
+
return result.then(failDueToSuccess).catch(handleError);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
failDueToSuccess();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
handleError(e);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
exports.expectFail = expectFail;
|
|
94
|
+
function expectSuccessfulFail(errorFn, handleError = EXPECT_ERROR_DEFAULT_HANDLER) {
|
|
95
|
+
try {
|
|
96
|
+
const result = errorFn();
|
|
97
|
+
if ((0, util_1.isPromise)(result)) {
|
|
98
|
+
return result.then(failDueToSuccess).catch(handleError);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
failDueToSuccess();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch (e) {
|
|
105
|
+
handleError(e);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.expectSuccessfulFail = expectSuccessfulFail;
|
|
109
|
+
/**
|
|
110
|
+
* 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.
|
|
111
|
+
*
|
|
112
|
+
* This is typically used in conjunction with failSuccessfully(), expectSuccessfulFail(), or expectFail().
|
|
113
|
+
*
|
|
114
|
+
* @param fn
|
|
115
|
+
* @param strict
|
|
116
|
+
* @returns
|
|
117
|
+
*/
|
|
118
|
+
function shouldFail(fn) {
|
|
119
|
+
const usesDoneCallback = fn.length > 0;
|
|
120
|
+
return (done) => {
|
|
121
|
+
function handleError(e) {
|
|
122
|
+
if (!(e instanceof JestExpectedFailError)) {
|
|
123
|
+
failWithJestDoneCallback(done, e);
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
done();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
expectSuccessfulFail(() => {
|
|
130
|
+
let result;
|
|
131
|
+
if (usesDoneCallback) {
|
|
132
|
+
const fakeDone = (0, util_1.build)({
|
|
133
|
+
base: fakeDoneHandler(),
|
|
134
|
+
build: (x) => {
|
|
135
|
+
x.failSuccessfully = () => {
|
|
136
|
+
fakeDone(failSuccessfullyError());
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
const callbackWithDoneResult = fn(fakeDone);
|
|
141
|
+
if ((0, util_1.isPromise)(callbackWithDoneResult)) {
|
|
142
|
+
fakeDone.reject(new Error('Configured to use "done" value while returning a promise. Configure your test to use one or the other.'));
|
|
143
|
+
}
|
|
144
|
+
// return the fake done promise. Done/fail will resolve as a promise.
|
|
145
|
+
result = fakeDone._ref.promise;
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
result = fn();
|
|
149
|
+
}
|
|
150
|
+
return result;
|
|
151
|
+
}, handleError);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
exports.shouldFail = shouldFail;
|
|
155
|
+
function itShouldFail(describeOrFn, fn) {
|
|
156
|
+
let description;
|
|
157
|
+
if (typeof describeOrFn === 'string') {
|
|
158
|
+
description = `should fail ${describeOrFn}`;
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
fn = describeOrFn;
|
|
162
|
+
description = 'should fail';
|
|
163
|
+
}
|
|
164
|
+
it(description, shouldFail(fn));
|
|
165
|
+
}
|
|
166
|
+
exports.itShouldFail = itShouldFail;
|
|
167
|
+
function fakeDoneHandler() {
|
|
168
|
+
const promiseRef = (0, util_1.promiseReference)();
|
|
169
|
+
const doneHandler = promiseRef.resolve;
|
|
170
|
+
const failHandler = (e) => {
|
|
171
|
+
promiseRef.reject(e);
|
|
172
|
+
};
|
|
173
|
+
const fakeDone = (error) => {
|
|
174
|
+
if (error) {
|
|
175
|
+
failHandler(error);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
doneHandler(0);
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
fakeDone.fail = (error) => {
|
|
182
|
+
failHandler(error);
|
|
183
|
+
};
|
|
184
|
+
fakeDone._ref = promiseRef;
|
|
185
|
+
fakeDone.promise = promiseRef.promise;
|
|
186
|
+
fakeDone.resolve = promiseRef.resolve;
|
|
187
|
+
fakeDone.reject = promiseRef.reject;
|
|
188
|
+
return fakeDone;
|
|
189
|
+
}
|
|
190
|
+
exports.fakeDoneHandler = fakeDoneHandler;
|
|
191
|
+
//# sourceMappingURL=jest.fail.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jest.fail.js","sourceRoot":"","sources":["../../../../../../packages/util/test/src/lib/jest.fail.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AA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