@manifest-network/manifest-mcp-core 0.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/dist/__test-utils__/callTool.d.ts +29 -0
- package/dist/__test-utils__/callTool.d.ts.map +1 -0
- package/dist/__test-utils__/callTool.js +45 -0
- package/dist/__test-utils__/callTool.js.map +1 -0
- package/dist/__test-utils__/mocks.d.ts +125 -0
- package/dist/__test-utils__/mocks.d.ts.map +1 -0
- package/dist/__test-utils__/mocks.js +146 -0
- package/dist/__test-utils__/mocks.js.map +1 -0
- package/dist/client.d.ts +67 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +209 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +30 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +127 -0
- package/dist/config.js.map +1 -0
- package/dist/cosmos.d.ts +24 -0
- package/dist/cosmos.d.ts.map +1 -0
- package/dist/cosmos.js +85 -0
- package/dist/cosmos.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +20 -0
- package/dist/lcd-adapter.d.ts +15 -0
- package/dist/lcd-adapter.d.ts.map +1 -0
- package/dist/lcd-adapter.js +98 -0
- package/dist/lcd-adapter.js.map +1 -0
- package/dist/logger.d.ts +20 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +43 -0
- package/dist/logger.js.map +1 -0
- package/dist/modules.d.ts +63 -0
- package/dist/modules.d.ts.map +1 -0
- package/dist/modules.js +759 -0
- package/dist/modules.js.map +1 -0
- package/dist/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js +78 -0
- package/dist/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.js.map +1 -0
- package/dist/node_modules/@vitest/expect/dist/index.d.ts +802 -0
- package/dist/node_modules/@vitest/expect/dist/index.d.ts.map +1 -0
- package/dist/node_modules/@vitest/expect/dist/index.js +1457 -0
- package/dist/node_modules/@vitest/expect/dist/index.js.map +1 -0
- package/dist/node_modules/@vitest/pretty-format/dist/index.d.ts +95 -0
- package/dist/node_modules/@vitest/pretty-format/dist/index.d.ts.map +1 -0
- package/dist/node_modules/@vitest/pretty-format/dist/index.js +877 -0
- package/dist/node_modules/@vitest/pretty-format/dist/index.js.map +1 -0
- package/dist/node_modules/@vitest/runner/dist/chunk-tasks.js +91 -0
- package/dist/node_modules/@vitest/runner/dist/chunk-tasks.js.map +1 -0
- package/dist/node_modules/@vitest/runner/dist/index.js +1381 -0
- package/dist/node_modules/@vitest/runner/dist/index.js.map +1 -0
- package/dist/node_modules/@vitest/runner/dist/tasks.d-D2GKpdwQ.d.ts +540 -0
- package/dist/node_modules/@vitest/runner/dist/tasks.d-D2GKpdwQ.d.ts.map +1 -0
- package/dist/node_modules/@vitest/runner/dist/utils.js +1 -0
- package/dist/node_modules/@vitest/snapshot/dist/environment.d-DOJxxZV9.d.ts +16 -0
- package/dist/node_modules/@vitest/snapshot/dist/environment.d-DOJxxZV9.d.ts.map +1 -0
- package/dist/node_modules/@vitest/snapshot/dist/index.d.ts +89 -0
- package/dist/node_modules/@vitest/snapshot/dist/index.d.ts.map +1 -0
- package/dist/node_modules/@vitest/snapshot/dist/index.js +649 -0
- package/dist/node_modules/@vitest/snapshot/dist/index.js.map +1 -0
- package/dist/node_modules/@vitest/snapshot/dist/rawSnapshot.d-U2kJUxDr.d.ts +40 -0
- package/dist/node_modules/@vitest/snapshot/dist/rawSnapshot.d-U2kJUxDr.d.ts.map +1 -0
- package/dist/node_modules/@vitest/spy/dist/index.d.ts +343 -0
- package/dist/node_modules/@vitest/spy/dist/index.d.ts.map +1 -0
- package/dist/node_modules/@vitest/spy/dist/index.js +386 -0
- package/dist/node_modules/@vitest/spy/dist/index.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js +82 -0
- package/dist/node_modules/@vitest/utils/dist/chunk-pathe.M-eThtNZ.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/diff.d.ts +14 -0
- package/dist/node_modules/@vitest/utils/dist/diff.d.ts.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/diff.js +1297 -0
- package/dist/node_modules/@vitest/utils/dist/diff.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/display.d.ts +15 -0
- package/dist/node_modules/@vitest/utils/dist/display.d.ts.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/display.js +558 -0
- package/dist/node_modules/@vitest/utils/dist/display.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/error.js +30 -0
- package/dist/node_modules/@vitest/utils/dist/error.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/helpers.js +181 -0
- package/dist/node_modules/@vitest/utils/dist/helpers.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/offset.js +27 -0
- package/dist/node_modules/@vitest/utils/dist/offset.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/serialize.js +77 -0
- package/dist/node_modules/@vitest/utils/dist/serialize.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/source-map.js +367 -0
- package/dist/node_modules/@vitest/utils/dist/source-map.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/timers.js +37 -0
- package/dist/node_modules/@vitest/utils/dist/timers.js.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/types.d-BCElaP-c.d.ts +38 -0
- package/dist/node_modules/@vitest/utils/dist/types.d-BCElaP-c.d.ts.map +1 -0
- package/dist/node_modules/@vitest/utils/dist/types.d.ts +25 -0
- package/dist/node_modules/@vitest/utils/dist/types.d.ts.map +1 -0
- package/dist/node_modules/chai/index.js +2875 -0
- package/dist/node_modules/chai/index.js.map +1 -0
- package/dist/node_modules/magic-string/dist/magic-string.es.js +939 -0
- package/dist/node_modules/magic-string/dist/magic-string.es.js.map +1 -0
- package/dist/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js +85 -0
- package/dist/node_modules/pathe/dist/shared/pathe.M-eThtNZ.js.map +1 -0
- package/dist/node_modules/tinybench/dist/index.d.ts +91 -0
- package/dist/node_modules/tinybench/dist/index.d.ts.map +1 -0
- package/dist/node_modules/tinyrainbow/dist/index.d.ts +9 -0
- package/dist/node_modules/tinyrainbow/dist/index.d.ts.map +1 -0
- package/dist/node_modules/tinyrainbow/dist/index.js +86 -0
- package/dist/node_modules/tinyrainbow/dist/index.js.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +6 -0
- package/dist/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/benchmark.D0SlKNbZ.js +41 -0
- package/dist/node_modules/vitest/dist/chunks/benchmark.D0SlKNbZ.js.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/benchmark.d.DAaHLpsq.d.ts +12 -0
- package/dist/node_modules/vitest/dist/chunks/benchmark.d.DAaHLpsq.d.ts.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/global.d.x-ILCfAE.d.ts +100 -0
- package/dist/node_modules/vitest/dist/chunks/global.d.x-ILCfAE.d.ts.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/rpc.MzXet3jl.js +57 -0
- package/dist/node_modules/vitest/dist/chunks/rpc.MzXet3jl.js.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/rpc.d.BFMWpdph.d.ts +13 -0
- package/dist/node_modules/vitest/dist/chunks/rpc.d.BFMWpdph.d.ts.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/test.CTcmp4Su.js +2791 -0
- package/dist/node_modules/vitest/dist/chunks/test.CTcmp4Su.js.map +1 -0
- package/dist/node_modules/vitest/dist/chunks/utils.BX5Fg8C4.js +44 -0
- package/dist/node_modules/vitest/dist/chunks/utils.BX5Fg8C4.js.map +1 -0
- package/dist/node_modules/vitest/dist/index.d.ts +9 -0
- package/dist/queries/auth.d.ts +15 -0
- package/dist/queries/auth.d.ts.map +1 -0
- package/dist/queries/auth.js +58 -0
- package/dist/queries/auth.js.map +1 -0
- package/dist/queries/bank.d.ts +15 -0
- package/dist/queries/bank.d.ts.map +1 -0
- package/dist/queries/bank.js +93 -0
- package/dist/queries/bank.js.map +1 -0
- package/dist/queries/billing.d.ts +15 -0
- package/dist/queries/billing.d.ts.map +1 -0
- package/dist/queries/billing.js +114 -0
- package/dist/queries/billing.js.map +1 -0
- package/dist/queries/distribution.d.ts +15 -0
- package/dist/queries/distribution.d.ts.map +1 -0
- package/dist/queries/distribution.js +73 -0
- package/dist/queries/distribution.js.map +1 -0
- package/dist/queries/gov.d.ts +15 -0
- package/dist/queries/gov.d.ts.map +1 -0
- package/dist/queries/gov.js +98 -0
- package/dist/queries/gov.js.map +1 -0
- package/dist/queries/group.d.ts +15 -0
- package/dist/queries/group.d.ts.map +1 -0
- package/dist/queries/group.js +159 -0
- package/dist/queries/group.js.map +1 -0
- package/dist/queries/index.d.ts +10 -0
- package/dist/queries/index.js +10 -0
- package/dist/queries/sku.d.ts +16 -0
- package/dist/queries/sku.d.ts.map +1 -0
- package/dist/queries/sku.js +85 -0
- package/dist/queries/sku.js.map +1 -0
- package/dist/queries/staking.d.ts +15 -0
- package/dist/queries/staking.d.ts.map +1 -0
- package/dist/queries/staking.js +127 -0
- package/dist/queries/staking.js.map +1 -0
- package/dist/queries/utils.d.ts +54 -0
- package/dist/queries/utils.d.ts.map +1 -0
- package/dist/queries/utils.js +74 -0
- package/dist/queries/utils.js.map +1 -0
- package/dist/retry.d.ts +48 -0
- package/dist/retry.d.ts.map +1 -0
- package/dist/retry.js +106 -0
- package/dist/retry.js.map +1 -0
- package/dist/server-utils.d.ts +61 -0
- package/dist/server-utils.d.ts.map +1 -0
- package/dist/server-utils.js +156 -0
- package/dist/server-utils.js.map +1 -0
- package/dist/tools/fundCredits.d.ts +8 -0
- package/dist/tools/fundCredits.d.ts.map +1 -0
- package/dist/tools/fundCredits.js +9 -0
- package/dist/tools/fundCredits.js.map +1 -0
- package/dist/tools/getBalance.d.ts +26 -0
- package/dist/tools/getBalance.d.ts.map +1 -0
- package/dist/tools/getBalance.js +59 -0
- package/dist/tools/getBalance.js.map +1 -0
- package/dist/tools/stopApp.d.ts +13 -0
- package/dist/tools/stopApp.d.ts.map +1 -0
- package/dist/tools/stopApp.js +15 -0
- package/dist/tools/stopApp.js.map +1 -0
- package/dist/transactions/bank.d.ts +11 -0
- package/dist/transactions/bank.d.ts.map +1 -0
- package/dist/transactions/bank.js +75 -0
- package/dist/transactions/bank.js.map +1 -0
- package/dist/transactions/billing.d.ts +11 -0
- package/dist/transactions/billing.d.ts.map +1 -0
- package/dist/transactions/billing.js +189 -0
- package/dist/transactions/billing.js.map +1 -0
- package/dist/transactions/distribution.d.ts +11 -0
- package/dist/transactions/distribution.d.ts.map +1 -0
- package/dist/transactions/distribution.js +60 -0
- package/dist/transactions/distribution.js.map +1 -0
- package/dist/transactions/gov.d.ts +11 -0
- package/dist/transactions/gov.d.ts.map +1 -0
- package/dist/transactions/gov.js +108 -0
- package/dist/transactions/gov.js.map +1 -0
- package/dist/transactions/group.d.ts +11 -0
- package/dist/transactions/group.d.ts.map +1 -0
- package/dist/transactions/group.js +347 -0
- package/dist/transactions/group.js.map +1 -0
- package/dist/transactions/index.d.ts +10 -0
- package/dist/transactions/index.js +10 -0
- package/dist/transactions/manifest.d.ts +11 -0
- package/dist/transactions/manifest.d.ts.map +1 -0
- package/dist/transactions/manifest.js +59 -0
- package/dist/transactions/manifest.js.map +1 -0
- package/dist/transactions/sku.d.ts +11 -0
- package/dist/transactions/sku.d.ts.map +1 -0
- package/dist/transactions/sku.js +191 -0
- package/dist/transactions/sku.js.map +1 -0
- package/dist/transactions/staking.d.ts +11 -0
- package/dist/transactions/staking.d.ts.map +1 -0
- package/dist/transactions/staking.js +79 -0
- package/dist/transactions/staking.js.map +1 -0
- package/dist/transactions/utils.d.ts +161 -0
- package/dist/transactions/utils.d.ts.map +1 -0
- package/dist/transactions/utils.js +272 -0
- package/dist/transactions/utils.js.map +1 -0
- package/dist/types.d.ts +390 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +45 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +30 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +53 -0
- package/dist/validation.js.map +1 -0
- package/dist/version.d.ts +5 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +6 -0
- package/dist/version.js.map +1 -0
- package/dist/wallet/index.d.ts +4 -0
- package/dist/wallet/index.js +3 -0
- package/dist/wallet/mnemonic.d.ts +47 -0
- package/dist/wallet/mnemonic.d.ts.map +1 -0
- package/dist/wallet/mnemonic.js +97 -0
- package/dist/wallet/mnemonic.js.map +1 -0
- package/dist/wallet/sign-arbitrary.d.ts +12 -0
- package/dist/wallet/sign-arbitrary.d.ts.map +1 -0
- package/dist/wallet/sign-arbitrary.js +36 -0
- package/dist/wallet/sign-arbitrary.js.map +1 -0
- package/package.json +72 -0
|
@@ -0,0 +1,1457 @@
|
|
|
1
|
+
import { y } from "../../../tinyrainbow/dist/index.js";
|
|
2
|
+
import { stringify } from "../../utils/dist/display.js";
|
|
3
|
+
import { assertTypes, getType, isObject, noop, ordinal } from "../../utils/dist/helpers.js";
|
|
4
|
+
import { diff, printDiffOrStringify } from "../../utils/dist/diff.js";
|
|
5
|
+
import { processError } from "../../utils/dist/error.js";
|
|
6
|
+
import { isMockFunction } from "../../spy/dist/index.js";
|
|
7
|
+
import { use, utils_exports } from "../../../chai/index.js";
|
|
8
|
+
//#region ../../node_modules/@vitest/expect/dist/index.js
|
|
9
|
+
const ChaiStyleAssertions = (chai, utils) => {
|
|
10
|
+
function defProperty(name, delegateTo) {
|
|
11
|
+
utils.addProperty(chai.Assertion.prototype, name, function() {
|
|
12
|
+
const jestMethod = chai.Assertion.prototype[delegateTo];
|
|
13
|
+
if (!jestMethod) throw new Error(`Cannot delegate to ${String(delegateTo)}: method not found. Ensure JestChaiExpect plugin is loaded first.`);
|
|
14
|
+
return jestMethod.call(this);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function defPropertyWithArgs(name, delegateTo, ...args) {
|
|
18
|
+
utils.addProperty(chai.Assertion.prototype, name, function() {
|
|
19
|
+
const jestMethod = chai.Assertion.prototype[delegateTo];
|
|
20
|
+
if (!jestMethod) throw new Error(`Cannot delegate to ${String(delegateTo)}: method not found. Ensure JestChaiExpect plugin is loaded first.`);
|
|
21
|
+
return jestMethod.call(this, ...args);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
function defMethod(name, delegateTo) {
|
|
25
|
+
utils.addChainableMethod(chai.Assertion.prototype, name, function(...args) {
|
|
26
|
+
const jestMethod = chai.Assertion.prototype[delegateTo];
|
|
27
|
+
if (!jestMethod) throw new Error(`Cannot delegate to ${String(delegateTo)}: method not found. Ensure JestChaiExpect plugin is loaded first.`);
|
|
28
|
+
return jestMethod.call(this, ...args);
|
|
29
|
+
}, () => {});
|
|
30
|
+
}
|
|
31
|
+
defProperty("called", "toHaveBeenCalled");
|
|
32
|
+
defProperty("calledOnce", "toHaveBeenCalledOnce");
|
|
33
|
+
defProperty("returned", "toHaveReturned");
|
|
34
|
+
defPropertyWithArgs("calledTwice", "toHaveBeenCalledTimes", 2);
|
|
35
|
+
defPropertyWithArgs("calledThrice", "toHaveBeenCalledTimes", 3);
|
|
36
|
+
defMethod("callCount", "toHaveBeenCalledTimes");
|
|
37
|
+
defMethod("calledWith", "toHaveBeenCalledWith");
|
|
38
|
+
defMethod("calledOnceWith", "toHaveBeenCalledExactlyOnceWith");
|
|
39
|
+
defMethod("lastCalledWith", "toHaveBeenLastCalledWith");
|
|
40
|
+
defMethod("nthCalledWith", "toHaveBeenNthCalledWith");
|
|
41
|
+
defMethod("returnedWith", "toHaveReturnedWith");
|
|
42
|
+
defMethod("returnedTimes", "toHaveReturnedTimes");
|
|
43
|
+
defMethod("lastReturnedWith", "toHaveLastReturnedWith");
|
|
44
|
+
defMethod("nthReturnedWith", "toHaveNthReturnedWith");
|
|
45
|
+
defMethod("calledBefore", "toHaveBeenCalledBefore");
|
|
46
|
+
defMethod("calledAfter", "toHaveBeenCalledAfter");
|
|
47
|
+
};
|
|
48
|
+
const MATCHERS_OBJECT = Symbol.for("matchers-object");
|
|
49
|
+
const JEST_MATCHERS_OBJECT = Symbol.for("$$jest-matchers-object");
|
|
50
|
+
const GLOBAL_EXPECT = Symbol.for("expect-global");
|
|
51
|
+
const ASYMMETRIC_MATCHERS_OBJECT = Symbol.for("asymmetric-matchers-object");
|
|
52
|
+
const customMatchers = {
|
|
53
|
+
toSatisfy(actual, expected, message) {
|
|
54
|
+
const { printReceived, printExpected, matcherHint } = this.utils;
|
|
55
|
+
const pass = expected(actual);
|
|
56
|
+
return {
|
|
57
|
+
pass,
|
|
58
|
+
message: () => pass ? `\
|
|
59
|
+
${matcherHint(".not.toSatisfy", "received", "")}
|
|
60
|
+
|
|
61
|
+
Expected value to not satisfy:
|
|
62
|
+
${message || printExpected(expected)}
|
|
63
|
+
Received:
|
|
64
|
+
${printReceived(actual)}` : `\
|
|
65
|
+
${matcherHint(".toSatisfy", "received", "")}
|
|
66
|
+
|
|
67
|
+
Expected value to satisfy:
|
|
68
|
+
${message || printExpected(expected)}
|
|
69
|
+
|
|
70
|
+
Received:
|
|
71
|
+
${printReceived(actual)}`
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
toBeOneOf(actual, expected) {
|
|
75
|
+
const { equals, customTesters } = this;
|
|
76
|
+
const { printReceived, printExpected, matcherHint } = this.utils;
|
|
77
|
+
let pass;
|
|
78
|
+
if (Array.isArray(expected)) pass = expected.length === 0 || expected.some((item) => equals(item, actual, customTesters));
|
|
79
|
+
else if (expected instanceof Set) pass = expected.size === 0 || expected.has(actual) || [...expected].some((item) => equals(item, actual, customTesters));
|
|
80
|
+
else throw new TypeError(`You must provide an array or set to ${matcherHint(".toBeOneOf")}, not '${typeof expected}'.`);
|
|
81
|
+
return {
|
|
82
|
+
pass,
|
|
83
|
+
message: () => pass ? `\
|
|
84
|
+
${matcherHint(".not.toBeOneOf", "received", "")}
|
|
85
|
+
|
|
86
|
+
Expected value to not be one of:
|
|
87
|
+
${printExpected(expected)}
|
|
88
|
+
Received:
|
|
89
|
+
${printReceived(actual)}` : `\
|
|
90
|
+
${matcherHint(".toBeOneOf", "received", "")}
|
|
91
|
+
|
|
92
|
+
Expected value to be one of:
|
|
93
|
+
${printExpected(expected)}
|
|
94
|
+
|
|
95
|
+
Received:
|
|
96
|
+
${printReceived(actual)}`
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
const EXPECTED_COLOR = y.green;
|
|
101
|
+
const RECEIVED_COLOR = y.red;
|
|
102
|
+
const INVERTED_COLOR = y.inverse;
|
|
103
|
+
const BOLD_WEIGHT = y.bold;
|
|
104
|
+
const DIM_COLOR = y.dim;
|
|
105
|
+
function matcherHint(matcherName, received = "received", expected = "expected", options = {}) {
|
|
106
|
+
const { comment = "", isDirectExpectCall = false, isNot = false, promise = "", secondArgument = "", expectedColor = EXPECTED_COLOR, receivedColor = RECEIVED_COLOR, secondArgumentColor = EXPECTED_COLOR } = options;
|
|
107
|
+
let hint = "";
|
|
108
|
+
let dimString = "expect";
|
|
109
|
+
if (!isDirectExpectCall && received !== "") {
|
|
110
|
+
hint += DIM_COLOR(`${dimString}(`) + receivedColor(received);
|
|
111
|
+
dimString = ")";
|
|
112
|
+
}
|
|
113
|
+
if (promise !== "") {
|
|
114
|
+
hint += DIM_COLOR(`${dimString}.`) + promise;
|
|
115
|
+
dimString = "";
|
|
116
|
+
}
|
|
117
|
+
if (isNot) {
|
|
118
|
+
hint += `${DIM_COLOR(`${dimString}.`)}not`;
|
|
119
|
+
dimString = "";
|
|
120
|
+
}
|
|
121
|
+
if (matcherName.includes(".")) dimString += matcherName;
|
|
122
|
+
else {
|
|
123
|
+
hint += DIM_COLOR(`${dimString}.`) + matcherName;
|
|
124
|
+
dimString = "";
|
|
125
|
+
}
|
|
126
|
+
if (expected === "") dimString += "()";
|
|
127
|
+
else {
|
|
128
|
+
hint += DIM_COLOR(`${dimString}(`) + expectedColor(expected);
|
|
129
|
+
if (secondArgument) hint += DIM_COLOR(", ") + secondArgumentColor(secondArgument);
|
|
130
|
+
dimString = ")";
|
|
131
|
+
}
|
|
132
|
+
if (comment !== "") dimString += ` // ${comment}`;
|
|
133
|
+
if (dimString !== "") hint += DIM_COLOR(dimString);
|
|
134
|
+
return hint;
|
|
135
|
+
}
|
|
136
|
+
const SPACE_SYMBOL = "·";
|
|
137
|
+
function replaceTrailingSpaces(text) {
|
|
138
|
+
return text.replace(/\s+$/gm, (spaces) => SPACE_SYMBOL.repeat(spaces.length));
|
|
139
|
+
}
|
|
140
|
+
function printReceived(object) {
|
|
141
|
+
return RECEIVED_COLOR(replaceTrailingSpaces(stringify(object)));
|
|
142
|
+
}
|
|
143
|
+
function printExpected(value) {
|
|
144
|
+
return EXPECTED_COLOR(replaceTrailingSpaces(stringify(value)));
|
|
145
|
+
}
|
|
146
|
+
function getMatcherUtils() {
|
|
147
|
+
return {
|
|
148
|
+
EXPECTED_COLOR,
|
|
149
|
+
RECEIVED_COLOR,
|
|
150
|
+
INVERTED_COLOR,
|
|
151
|
+
BOLD_WEIGHT,
|
|
152
|
+
DIM_COLOR,
|
|
153
|
+
diff,
|
|
154
|
+
matcherHint,
|
|
155
|
+
printReceived,
|
|
156
|
+
printExpected,
|
|
157
|
+
printDiffOrStringify,
|
|
158
|
+
printWithType
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function printWithType(name, value, print) {
|
|
162
|
+
const type = getType(value);
|
|
163
|
+
return (type !== "null" && type !== "undefined" ? `${name} has type: ${type}\n` : "") + `${name} has value: ${print(value)}`;
|
|
164
|
+
}
|
|
165
|
+
function addCustomEqualityTesters(newTesters) {
|
|
166
|
+
if (!Array.isArray(newTesters)) throw new TypeError(`expect.customEqualityTesters: Must be set to an array of Testers. Was given "${getType(newTesters)}"`);
|
|
167
|
+
globalThis[JEST_MATCHERS_OBJECT].customEqualityTesters.push(...newTesters);
|
|
168
|
+
}
|
|
169
|
+
function getCustomEqualityTesters() {
|
|
170
|
+
return globalThis[JEST_MATCHERS_OBJECT].customEqualityTesters;
|
|
171
|
+
}
|
|
172
|
+
function equals(a, b, customTesters, strictCheck) {
|
|
173
|
+
customTesters = customTesters || [];
|
|
174
|
+
return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey);
|
|
175
|
+
}
|
|
176
|
+
Function.prototype.toString;
|
|
177
|
+
function isAsymmetric(obj) {
|
|
178
|
+
return !!obj && typeof obj === "object" && "asymmetricMatch" in obj && isA("Function", obj.asymmetricMatch);
|
|
179
|
+
}
|
|
180
|
+
function asymmetricMatch(a, b, customTesters) {
|
|
181
|
+
const asymmetricA = isAsymmetric(a);
|
|
182
|
+
const asymmetricB = isAsymmetric(b);
|
|
183
|
+
if (asymmetricA && asymmetricB) return;
|
|
184
|
+
if (asymmetricA) return a.asymmetricMatch(b, customTesters);
|
|
185
|
+
if (asymmetricB) return b.asymmetricMatch(a, customTesters);
|
|
186
|
+
}
|
|
187
|
+
function isError(value) {
|
|
188
|
+
if (typeof Error.isError === "function") return Error.isError(value);
|
|
189
|
+
switch (Object.prototype.toString.call(value)) {
|
|
190
|
+
case "[object Error]":
|
|
191
|
+
case "[object Exception]":
|
|
192
|
+
case "[object DOMException]": return true;
|
|
193
|
+
default: return value instanceof Error;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
function eq(a, b, aStack, bStack, customTesters, hasKey) {
|
|
197
|
+
let result = true;
|
|
198
|
+
const asymmetricResult = asymmetricMatch(a, b, customTesters);
|
|
199
|
+
if (asymmetricResult !== void 0) return asymmetricResult;
|
|
200
|
+
const testerContext = { equals };
|
|
201
|
+
for (let i = 0; i < customTesters.length; i++) {
|
|
202
|
+
const customTesterResult = customTesters[i].call(testerContext, a, b, customTesters);
|
|
203
|
+
if (customTesterResult !== void 0) return customTesterResult;
|
|
204
|
+
}
|
|
205
|
+
if (typeof URL === "function" && a instanceof URL && b instanceof URL) return a.href === b.href;
|
|
206
|
+
if (Object.is(a, b)) return true;
|
|
207
|
+
if (a === null || b === null) return a === b;
|
|
208
|
+
const className = Object.prototype.toString.call(a);
|
|
209
|
+
if (className !== Object.prototype.toString.call(b)) return false;
|
|
210
|
+
switch (className) {
|
|
211
|
+
case "[object Boolean]":
|
|
212
|
+
case "[object String]":
|
|
213
|
+
case "[object Number]": if (typeof a !== typeof b) return false;
|
|
214
|
+
else if (typeof a !== "object" && typeof b !== "object") return Object.is(a, b);
|
|
215
|
+
else return Object.is(a.valueOf(), b.valueOf());
|
|
216
|
+
case "[object Date]": {
|
|
217
|
+
const numA = +a;
|
|
218
|
+
const numB = +b;
|
|
219
|
+
return numA === numB || Number.isNaN(numA) && Number.isNaN(numB);
|
|
220
|
+
}
|
|
221
|
+
case "[object RegExp]": return a.source === b.source && a.flags === b.flags;
|
|
222
|
+
case "[object Temporal.Instant]":
|
|
223
|
+
case "[object Temporal.ZonedDateTime]":
|
|
224
|
+
case "[object Temporal.PlainDateTime]":
|
|
225
|
+
case "[object Temporal.PlainDate]":
|
|
226
|
+
case "[object Temporal.PlainTime]":
|
|
227
|
+
case "[object Temporal.PlainYearMonth]":
|
|
228
|
+
case "[object Temporal.PlainMonthDay]": return a.equals(b);
|
|
229
|
+
case "[object Temporal.Duration]": return a.toString() === b.toString();
|
|
230
|
+
}
|
|
231
|
+
if (typeof a !== "object" || typeof b !== "object") return false;
|
|
232
|
+
if (isDomNode(a) && isDomNode(b)) return a.isEqualNode(b);
|
|
233
|
+
let length = aStack.length;
|
|
234
|
+
while (length--) if (aStack[length] === a) return bStack[length] === b;
|
|
235
|
+
else if (bStack[length] === b) return false;
|
|
236
|
+
aStack.push(a);
|
|
237
|
+
bStack.push(b);
|
|
238
|
+
if (className === "[object Array]" && a.length !== b.length) return false;
|
|
239
|
+
if (isError(a) && isError(b)) try {
|
|
240
|
+
return isErrorEqual(a, b, aStack, bStack, customTesters, hasKey);
|
|
241
|
+
} finally {
|
|
242
|
+
aStack.pop();
|
|
243
|
+
bStack.pop();
|
|
244
|
+
}
|
|
245
|
+
const aKeys = keys(a, hasKey);
|
|
246
|
+
let key;
|
|
247
|
+
let size = aKeys.length;
|
|
248
|
+
if (keys(b, hasKey).length !== size) return false;
|
|
249
|
+
while (size--) {
|
|
250
|
+
key = aKeys[size];
|
|
251
|
+
result = hasKey(b, key) && eq(a[key], b[key], aStack, bStack, customTesters, hasKey);
|
|
252
|
+
if (!result) return false;
|
|
253
|
+
}
|
|
254
|
+
aStack.pop();
|
|
255
|
+
bStack.pop();
|
|
256
|
+
return result;
|
|
257
|
+
}
|
|
258
|
+
function isErrorEqual(a, b, aStack, bStack, customTesters, hasKey) {
|
|
259
|
+
let result = Object.prototype.toString.call(a) === Object.prototype.toString.call(b) && a.name === b.name && a.message === b.message;
|
|
260
|
+
if (typeof b.cause !== "undefined") result && (result = eq(a.cause, b.cause, aStack, bStack, customTesters, hasKey));
|
|
261
|
+
if (a instanceof AggregateError && b instanceof AggregateError) result && (result = eq(a.errors, b.errors, aStack, bStack, customTesters, hasKey));
|
|
262
|
+
result && (result = eq({ ...a }, { ...b }, aStack, bStack, customTesters, hasKey));
|
|
263
|
+
return result;
|
|
264
|
+
}
|
|
265
|
+
function keys(obj, hasKey) {
|
|
266
|
+
const keys = [];
|
|
267
|
+
for (const key in obj) if (hasKey(obj, key)) keys.push(key);
|
|
268
|
+
return keys.concat(Object.getOwnPropertySymbols(obj).filter((symbol) => Object.getOwnPropertyDescriptor(obj, symbol).enumerable));
|
|
269
|
+
}
|
|
270
|
+
function hasDefinedKey(obj, key) {
|
|
271
|
+
return hasKey(obj, key) && obj[key] !== void 0;
|
|
272
|
+
}
|
|
273
|
+
function hasKey(obj, key) {
|
|
274
|
+
return Object.hasOwn(obj, key);
|
|
275
|
+
}
|
|
276
|
+
function isA(typeName, value) {
|
|
277
|
+
return Object.prototype.toString.apply(value) === `[object ${typeName}]`;
|
|
278
|
+
}
|
|
279
|
+
function isDomNode(obj) {
|
|
280
|
+
return obj !== null && typeof obj === "object" && "nodeType" in obj && typeof obj.nodeType === "number" && "nodeName" in obj && typeof obj.nodeName === "string" && "isEqualNode" in obj && typeof obj.isEqualNode === "function";
|
|
281
|
+
}
|
|
282
|
+
const IS_KEYED_SENTINEL = "@@__IMMUTABLE_KEYED__@@";
|
|
283
|
+
const IS_SET_SENTINEL = "@@__IMMUTABLE_SET__@@";
|
|
284
|
+
const IS_LIST_SENTINEL = "@@__IMMUTABLE_LIST__@@";
|
|
285
|
+
const IS_ORDERED_SENTINEL = "@@__IMMUTABLE_ORDERED__@@";
|
|
286
|
+
const IS_RECORD_SYMBOL = "@@__IMMUTABLE_RECORD__@@";
|
|
287
|
+
function isImmutableUnorderedKeyed(maybeKeyed) {
|
|
288
|
+
return !!(maybeKeyed && maybeKeyed[IS_KEYED_SENTINEL] && !maybeKeyed[IS_ORDERED_SENTINEL]);
|
|
289
|
+
}
|
|
290
|
+
function isImmutableUnorderedSet(maybeSet) {
|
|
291
|
+
return !!(maybeSet && maybeSet[IS_SET_SENTINEL] && !maybeSet[IS_ORDERED_SENTINEL]);
|
|
292
|
+
}
|
|
293
|
+
function isObjectLiteral(source) {
|
|
294
|
+
return source != null && typeof source === "object" && !Array.isArray(source);
|
|
295
|
+
}
|
|
296
|
+
function isImmutableList(source) {
|
|
297
|
+
return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL]);
|
|
298
|
+
}
|
|
299
|
+
function isImmutableOrderedKeyed(source) {
|
|
300
|
+
return Boolean(source && isObjectLiteral(source) && source[IS_KEYED_SENTINEL] && source[IS_ORDERED_SENTINEL]);
|
|
301
|
+
}
|
|
302
|
+
function isImmutableOrderedSet(source) {
|
|
303
|
+
return Boolean(source && isObjectLiteral(source) && source[IS_SET_SENTINEL] && source[IS_ORDERED_SENTINEL]);
|
|
304
|
+
}
|
|
305
|
+
function isImmutableRecord(source) {
|
|
306
|
+
return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL]);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
310
|
+
*
|
|
311
|
+
* This source code is licensed under the MIT license found in the
|
|
312
|
+
* LICENSE file in the root directory of this source tree.
|
|
313
|
+
*
|
|
314
|
+
*/
|
|
315
|
+
const IteratorSymbol = Symbol.iterator;
|
|
316
|
+
function hasIterator(object) {
|
|
317
|
+
return !!(object != null && object[IteratorSymbol]);
|
|
318
|
+
}
|
|
319
|
+
function iterableEquality(a, b, customTesters = [], aStack = [], bStack = []) {
|
|
320
|
+
if (typeof a !== "object" || typeof b !== "object" || Array.isArray(a) || Array.isArray(b) || !hasIterator(a) || !hasIterator(b)) return;
|
|
321
|
+
if (a.constructor !== b.constructor) return false;
|
|
322
|
+
let length = aStack.length;
|
|
323
|
+
while (length--) if (aStack[length] === a) return bStack[length] === b;
|
|
324
|
+
aStack.push(a);
|
|
325
|
+
bStack.push(b);
|
|
326
|
+
const filteredCustomTesters = [...customTesters.filter((t) => t !== iterableEquality), iterableEqualityWithStack];
|
|
327
|
+
function iterableEqualityWithStack(a, b) {
|
|
328
|
+
return iterableEquality(a, b, [...customTesters], [...aStack], [...bStack]);
|
|
329
|
+
}
|
|
330
|
+
if (a.size !== void 0) {
|
|
331
|
+
if (a.size !== b.size) return false;
|
|
332
|
+
else if (isA("Set", a) || isImmutableUnorderedSet(a)) {
|
|
333
|
+
let allFound = true;
|
|
334
|
+
for (const aValue of a) if (!b.has(aValue)) {
|
|
335
|
+
let has = false;
|
|
336
|
+
for (const bValue of b) if (equals(aValue, bValue, filteredCustomTesters) === true) has = true;
|
|
337
|
+
if (has === false) {
|
|
338
|
+
allFound = false;
|
|
339
|
+
break;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
aStack.pop();
|
|
343
|
+
bStack.pop();
|
|
344
|
+
return allFound;
|
|
345
|
+
} else if (isA("Map", a) || isImmutableUnorderedKeyed(a)) {
|
|
346
|
+
let allFound = true;
|
|
347
|
+
for (const aEntry of a) if (!b.has(aEntry[0]) || !equals(aEntry[1], b.get(aEntry[0]), filteredCustomTesters)) {
|
|
348
|
+
let has = false;
|
|
349
|
+
for (const bEntry of b) {
|
|
350
|
+
const matchedKey = equals(aEntry[0], bEntry[0], filteredCustomTesters);
|
|
351
|
+
let matchedValue = false;
|
|
352
|
+
if (matchedKey === true) matchedValue = equals(aEntry[1], bEntry[1], filteredCustomTesters);
|
|
353
|
+
if (matchedValue === true) has = true;
|
|
354
|
+
}
|
|
355
|
+
if (has === false) {
|
|
356
|
+
allFound = false;
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
aStack.pop();
|
|
361
|
+
bStack.pop();
|
|
362
|
+
return allFound;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
const bIterator = b[IteratorSymbol]();
|
|
366
|
+
for (const aValue of a) {
|
|
367
|
+
const nextB = bIterator.next();
|
|
368
|
+
if (nextB.done || !equals(aValue, nextB.value, filteredCustomTesters)) return false;
|
|
369
|
+
}
|
|
370
|
+
if (!bIterator.next().done) return false;
|
|
371
|
+
if (!isImmutableList(a) && !isImmutableOrderedKeyed(a) && !isImmutableOrderedSet(a) && !isImmutableRecord(a)) {
|
|
372
|
+
if (!equals(Object.entries(a), Object.entries(b), filteredCustomTesters)) return false;
|
|
373
|
+
}
|
|
374
|
+
aStack.pop();
|
|
375
|
+
bStack.pop();
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Checks if `hasOwnProperty(object, key)` up the prototype chain, stopping at `Object.prototype`.
|
|
380
|
+
*/
|
|
381
|
+
function hasPropertyInObject(object, key) {
|
|
382
|
+
if (!object || typeof object !== "object" || object === Object.prototype) return false;
|
|
383
|
+
return Object.hasOwn(object, key) || hasPropertyInObject(Object.getPrototypeOf(object), key);
|
|
384
|
+
}
|
|
385
|
+
function isObjectWithKeys(a) {
|
|
386
|
+
return isObject(a) && !isError(a) && !Array.isArray(a) && !(a instanceof Date) && !(a instanceof Set) && !(a instanceof Map);
|
|
387
|
+
}
|
|
388
|
+
function subsetEquality(object, subset, customTesters = []) {
|
|
389
|
+
const filteredCustomTesters = customTesters.filter((t) => t !== subsetEquality);
|
|
390
|
+
const subsetEqualityWithContext = (seenReferences = /* @__PURE__ */ new WeakMap()) => (object, subset) => {
|
|
391
|
+
if (!isObjectWithKeys(subset)) return;
|
|
392
|
+
return Object.keys(subset).every((key) => {
|
|
393
|
+
if (subset[key] != null && typeof subset[key] === "object") {
|
|
394
|
+
if (seenReferences.has(subset[key])) return equals(object[key], subset[key], filteredCustomTesters);
|
|
395
|
+
seenReferences.set(subset[key], true);
|
|
396
|
+
}
|
|
397
|
+
const result = object != null && hasPropertyInObject(object, key) && equals(object[key], subset[key], [...filteredCustomTesters, subsetEqualityWithContext(seenReferences)]);
|
|
398
|
+
seenReferences.delete(subset[key]);
|
|
399
|
+
return result;
|
|
400
|
+
});
|
|
401
|
+
};
|
|
402
|
+
return subsetEqualityWithContext()(object, subset);
|
|
403
|
+
}
|
|
404
|
+
function typeEquality(a, b) {
|
|
405
|
+
if (a == null || b == null || a.constructor === b.constructor) return;
|
|
406
|
+
return false;
|
|
407
|
+
}
|
|
408
|
+
function arrayBufferEquality(a, b) {
|
|
409
|
+
let dataViewA = a;
|
|
410
|
+
let dataViewB = b;
|
|
411
|
+
if (!(a instanceof DataView && b instanceof DataView)) {
|
|
412
|
+
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) return;
|
|
413
|
+
try {
|
|
414
|
+
dataViewA = new DataView(a);
|
|
415
|
+
dataViewB = new DataView(b);
|
|
416
|
+
} catch {
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
if (dataViewA.byteLength !== dataViewB.byteLength) return false;
|
|
421
|
+
for (let i = 0; i < dataViewA.byteLength; i++) if (dataViewA.getUint8(i) !== dataViewB.getUint8(i)) return false;
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
function sparseArrayEquality(a, b, customTesters = []) {
|
|
425
|
+
if (!Array.isArray(a) || !Array.isArray(b)) return;
|
|
426
|
+
const aKeys = Object.keys(a);
|
|
427
|
+
const bKeys = Object.keys(b);
|
|
428
|
+
return equals(a, b, customTesters.filter((t) => t !== sparseArrayEquality), true) && equals(aKeys, bKeys);
|
|
429
|
+
}
|
|
430
|
+
function generateToBeMessage(deepEqualityName, expected = "#{this}", actual = "#{exp}") {
|
|
431
|
+
const toBeMessage = `expected ${expected} to be ${actual} // Object.is equality`;
|
|
432
|
+
if (["toStrictEqual", "toEqual"].includes(deepEqualityName)) return `${toBeMessage}\n\nIf it should pass with deep equality, replace "toBe" with "${deepEqualityName}"\n\nExpected: ${expected}\nReceived: serializes to the same string\n`;
|
|
433
|
+
return toBeMessage;
|
|
434
|
+
}
|
|
435
|
+
function pluralize(word, count) {
|
|
436
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
437
|
+
}
|
|
438
|
+
function getObjectKeys(object) {
|
|
439
|
+
return [...Object.keys(object), ...Object.getOwnPropertySymbols(object).filter((s) => Object.getOwnPropertyDescriptor(object, s)?.enumerable)];
|
|
440
|
+
}
|
|
441
|
+
function getObjectSubset(object, subset, customTesters) {
|
|
442
|
+
let stripped = 0;
|
|
443
|
+
const getObjectSubsetWithContext = (seenReferences = /* @__PURE__ */ new WeakMap()) => (object, subset) => {
|
|
444
|
+
if (Array.isArray(object)) {
|
|
445
|
+
if (Array.isArray(subset) && subset.length === object.length) return subset.map((sub, i) => getObjectSubsetWithContext(seenReferences)(object[i], sub));
|
|
446
|
+
} else if (object instanceof Date) return object;
|
|
447
|
+
else if (isObject(object) && isObject(subset)) {
|
|
448
|
+
if (equals(object, subset, [
|
|
449
|
+
...customTesters,
|
|
450
|
+
iterableEquality,
|
|
451
|
+
subsetEquality
|
|
452
|
+
])) return subset;
|
|
453
|
+
const trimmed = {};
|
|
454
|
+
seenReferences.set(object, trimmed);
|
|
455
|
+
if (typeof object.constructor === "function" && typeof object.constructor.name === "string") Object.defineProperty(trimmed, "constructor", {
|
|
456
|
+
enumerable: false,
|
|
457
|
+
value: object.constructor
|
|
458
|
+
});
|
|
459
|
+
for (const key of getObjectKeys(object)) if (hasPropertyInObject(subset, key)) trimmed[key] = seenReferences.has(object[key]) ? seenReferences.get(object[key]) : getObjectSubsetWithContext(seenReferences)(object[key], subset[key]);
|
|
460
|
+
else if (!seenReferences.has(object[key])) {
|
|
461
|
+
stripped += 1;
|
|
462
|
+
if (isObject(object[key])) stripped += getObjectKeys(object[key]).length;
|
|
463
|
+
getObjectSubsetWithContext(seenReferences)(object[key], subset[key]);
|
|
464
|
+
}
|
|
465
|
+
if (getObjectKeys(trimmed).length > 0) return trimmed;
|
|
466
|
+
}
|
|
467
|
+
return object;
|
|
468
|
+
};
|
|
469
|
+
return {
|
|
470
|
+
subset: getObjectSubsetWithContext()(object, subset),
|
|
471
|
+
stripped
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* Detects if an object is a Standard Schema V1 compatible schema
|
|
476
|
+
*/
|
|
477
|
+
function isStandardSchema(obj) {
|
|
478
|
+
return !!obj && (typeof obj === "object" || typeof obj === "function") && obj["~standard"] && typeof obj["~standard"].validate === "function";
|
|
479
|
+
}
|
|
480
|
+
if (!Object.hasOwn(globalThis, MATCHERS_OBJECT)) {
|
|
481
|
+
const globalState = /* @__PURE__ */ new WeakMap();
|
|
482
|
+
const matchers = Object.create(null);
|
|
483
|
+
const customEqualityTesters = [];
|
|
484
|
+
const asymmetricMatchers = Object.create(null);
|
|
485
|
+
Object.defineProperty(globalThis, MATCHERS_OBJECT, { get: () => globalState });
|
|
486
|
+
Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, {
|
|
487
|
+
configurable: true,
|
|
488
|
+
get: () => ({
|
|
489
|
+
state: globalState.get(globalThis[GLOBAL_EXPECT]),
|
|
490
|
+
matchers,
|
|
491
|
+
customEqualityTesters
|
|
492
|
+
})
|
|
493
|
+
});
|
|
494
|
+
Object.defineProperty(globalThis, ASYMMETRIC_MATCHERS_OBJECT, { get: () => asymmetricMatchers });
|
|
495
|
+
}
|
|
496
|
+
function getState(expect) {
|
|
497
|
+
return globalThis[MATCHERS_OBJECT].get(expect);
|
|
498
|
+
}
|
|
499
|
+
function setState(state, expect) {
|
|
500
|
+
const map = globalThis[MATCHERS_OBJECT];
|
|
501
|
+
const current = map.get(expect) || {};
|
|
502
|
+
const results = Object.defineProperties(current, {
|
|
503
|
+
...Object.getOwnPropertyDescriptors(current),
|
|
504
|
+
...Object.getOwnPropertyDescriptors(state)
|
|
505
|
+
});
|
|
506
|
+
map.set(expect, results);
|
|
507
|
+
}
|
|
508
|
+
var AsymmetricMatcher = class {
|
|
509
|
+
constructor(sample, inverse = false) {
|
|
510
|
+
this.$$typeof = Symbol.for("jest.asymmetricMatcher");
|
|
511
|
+
this.sample = sample;
|
|
512
|
+
this.inverse = inverse;
|
|
513
|
+
}
|
|
514
|
+
getMatcherContext(expect) {
|
|
515
|
+
return {
|
|
516
|
+
...getState(expect || globalThis[GLOBAL_EXPECT]),
|
|
517
|
+
equals,
|
|
518
|
+
isNot: this.inverse,
|
|
519
|
+
customTesters: getCustomEqualityTesters(),
|
|
520
|
+
utils: {
|
|
521
|
+
...getMatcherUtils(),
|
|
522
|
+
diff,
|
|
523
|
+
stringify,
|
|
524
|
+
iterableEquality,
|
|
525
|
+
subsetEquality
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
AsymmetricMatcher.prototype[Symbol.for("chai/inspect")] = function(options) {
|
|
531
|
+
const result = stringify(this, options.depth, { min: true });
|
|
532
|
+
if (result.length <= options.truncate) return result;
|
|
533
|
+
return `${this.toString()}{…}`;
|
|
534
|
+
};
|
|
535
|
+
var StringContaining = class extends AsymmetricMatcher {
|
|
536
|
+
constructor(sample, inverse = false) {
|
|
537
|
+
if (!isA("String", sample)) throw new Error("Expected is not a string");
|
|
538
|
+
super(sample, inverse);
|
|
539
|
+
}
|
|
540
|
+
asymmetricMatch(other) {
|
|
541
|
+
const result = isA("String", other) && other.includes(this.sample);
|
|
542
|
+
return this.inverse ? !result : result;
|
|
543
|
+
}
|
|
544
|
+
toString() {
|
|
545
|
+
return `String${this.inverse ? "Not" : ""}Containing`;
|
|
546
|
+
}
|
|
547
|
+
getExpectedType() {
|
|
548
|
+
return "string";
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
var Anything = class extends AsymmetricMatcher {
|
|
552
|
+
asymmetricMatch(other) {
|
|
553
|
+
return other != null;
|
|
554
|
+
}
|
|
555
|
+
toString() {
|
|
556
|
+
return "Anything";
|
|
557
|
+
}
|
|
558
|
+
toAsymmetricMatcher() {
|
|
559
|
+
return "Anything";
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
var ObjectContaining = class extends AsymmetricMatcher {
|
|
563
|
+
constructor(sample, inverse = false) {
|
|
564
|
+
super(sample, inverse);
|
|
565
|
+
}
|
|
566
|
+
getPrototype(obj) {
|
|
567
|
+
if (Object.getPrototypeOf) return Object.getPrototypeOf(obj);
|
|
568
|
+
if (obj.constructor.prototype === obj) return null;
|
|
569
|
+
return obj.constructor.prototype;
|
|
570
|
+
}
|
|
571
|
+
hasProperty(obj, property) {
|
|
572
|
+
if (!obj) return false;
|
|
573
|
+
if (Object.hasOwn(obj, property)) return true;
|
|
574
|
+
return this.hasProperty(this.getPrototype(obj), property);
|
|
575
|
+
}
|
|
576
|
+
getProperties(obj) {
|
|
577
|
+
return [...Object.keys(obj), ...Object.getOwnPropertySymbols(obj).filter((s) => Object.getOwnPropertyDescriptor(obj, s)?.enumerable)];
|
|
578
|
+
}
|
|
579
|
+
asymmetricMatch(other, customTesters) {
|
|
580
|
+
if (typeof this.sample !== "object") throw new TypeError(`You must provide an object to ${this.toString()}, not '${typeof this.sample}'.`);
|
|
581
|
+
let result = true;
|
|
582
|
+
const properties = this.getProperties(this.sample);
|
|
583
|
+
for (const property of properties) {
|
|
584
|
+
if (!this.hasProperty(other, property)) {
|
|
585
|
+
result = false;
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
const value = this.sample[property];
|
|
589
|
+
const otherValue = other[property];
|
|
590
|
+
if (!equals(value, otherValue, customTesters)) {
|
|
591
|
+
result = false;
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return this.inverse ? !result : result;
|
|
596
|
+
}
|
|
597
|
+
toString() {
|
|
598
|
+
return `Object${this.inverse ? "Not" : ""}Containing`;
|
|
599
|
+
}
|
|
600
|
+
getExpectedType() {
|
|
601
|
+
return "object";
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
var ArrayContaining = class extends AsymmetricMatcher {
|
|
605
|
+
constructor(sample, inverse = false) {
|
|
606
|
+
super(sample, inverse);
|
|
607
|
+
}
|
|
608
|
+
asymmetricMatch(other, customTesters) {
|
|
609
|
+
if (!Array.isArray(this.sample)) throw new TypeError(`You must provide an array to ${this.toString()}, not '${typeof this.sample}'.`);
|
|
610
|
+
const result = this.sample.length === 0 || Array.isArray(other) && this.sample.every((item) => other.some((another) => equals(item, another, customTesters)));
|
|
611
|
+
return this.inverse ? !result : result;
|
|
612
|
+
}
|
|
613
|
+
toString() {
|
|
614
|
+
return `Array${this.inverse ? "Not" : ""}Containing`;
|
|
615
|
+
}
|
|
616
|
+
getExpectedType() {
|
|
617
|
+
return "array";
|
|
618
|
+
}
|
|
619
|
+
};
|
|
620
|
+
var Any = class extends AsymmetricMatcher {
|
|
621
|
+
constructor(sample) {
|
|
622
|
+
if (typeof sample === "undefined") throw new TypeError("any() expects to be passed a constructor function. Please pass one or use anything() to match any object.");
|
|
623
|
+
super(sample);
|
|
624
|
+
}
|
|
625
|
+
fnNameFor(func) {
|
|
626
|
+
if (func.name) return func.name;
|
|
627
|
+
const matches = Function.prototype.toString.call(func).match(/^(?:async)?\s*function\s*(?:\*\s*)?([\w$]+)\s*\(/);
|
|
628
|
+
return matches ? matches[1] : "<anonymous>";
|
|
629
|
+
}
|
|
630
|
+
asymmetricMatch(other) {
|
|
631
|
+
if (this.sample === String) return typeof other == "string" || other instanceof String;
|
|
632
|
+
if (this.sample === Number) return typeof other == "number" || other instanceof Number;
|
|
633
|
+
if (this.sample === Function) return typeof other == "function" || typeof other === "function";
|
|
634
|
+
if (this.sample === Boolean) return typeof other == "boolean" || other instanceof Boolean;
|
|
635
|
+
if (this.sample === BigInt) return typeof other == "bigint" || other instanceof BigInt;
|
|
636
|
+
if (this.sample === Symbol) return typeof other == "symbol" || other instanceof Symbol;
|
|
637
|
+
if (this.sample === Object) return typeof other == "object";
|
|
638
|
+
return other instanceof this.sample;
|
|
639
|
+
}
|
|
640
|
+
toString() {
|
|
641
|
+
return "Any";
|
|
642
|
+
}
|
|
643
|
+
getExpectedType() {
|
|
644
|
+
if (this.sample === String) return "string";
|
|
645
|
+
if (this.sample === Number) return "number";
|
|
646
|
+
if (this.sample === Function) return "function";
|
|
647
|
+
if (this.sample === Object) return "object";
|
|
648
|
+
if (this.sample === Boolean) return "boolean";
|
|
649
|
+
return this.fnNameFor(this.sample);
|
|
650
|
+
}
|
|
651
|
+
toAsymmetricMatcher() {
|
|
652
|
+
return `Any<${this.fnNameFor(this.sample)}>`;
|
|
653
|
+
}
|
|
654
|
+
};
|
|
655
|
+
var StringMatching = class extends AsymmetricMatcher {
|
|
656
|
+
constructor(sample, inverse = false) {
|
|
657
|
+
if (!isA("String", sample) && !isA("RegExp", sample)) throw new Error("Expected is not a String or a RegExp");
|
|
658
|
+
super(new RegExp(sample), inverse);
|
|
659
|
+
}
|
|
660
|
+
asymmetricMatch(other) {
|
|
661
|
+
const result = isA("String", other) && this.sample.test(other);
|
|
662
|
+
return this.inverse ? !result : result;
|
|
663
|
+
}
|
|
664
|
+
toString() {
|
|
665
|
+
return `String${this.inverse ? "Not" : ""}Matching`;
|
|
666
|
+
}
|
|
667
|
+
getExpectedType() {
|
|
668
|
+
return "string";
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
var CloseTo = class extends AsymmetricMatcher {
|
|
672
|
+
constructor(sample, precision = 2, inverse = false) {
|
|
673
|
+
if (!isA("Number", sample)) throw new Error("Expected is not a Number");
|
|
674
|
+
if (!isA("Number", precision)) throw new Error("Precision is not a Number");
|
|
675
|
+
super(sample);
|
|
676
|
+
this.inverse = inverse;
|
|
677
|
+
this.precision = precision;
|
|
678
|
+
}
|
|
679
|
+
asymmetricMatch(other) {
|
|
680
|
+
if (!isA("Number", other)) return false;
|
|
681
|
+
let result = false;
|
|
682
|
+
if (other === Number.POSITIVE_INFINITY && this.sample === Number.POSITIVE_INFINITY) result = true;
|
|
683
|
+
else if (other === Number.NEGATIVE_INFINITY && this.sample === Number.NEGATIVE_INFINITY) result = true;
|
|
684
|
+
else result = Math.abs(this.sample - other) < 10 ** -this.precision / 2;
|
|
685
|
+
return this.inverse ? !result : result;
|
|
686
|
+
}
|
|
687
|
+
toString() {
|
|
688
|
+
return `Number${this.inverse ? "Not" : ""}CloseTo`;
|
|
689
|
+
}
|
|
690
|
+
getExpectedType() {
|
|
691
|
+
return "number";
|
|
692
|
+
}
|
|
693
|
+
toAsymmetricMatcher() {
|
|
694
|
+
return [
|
|
695
|
+
this.toString(),
|
|
696
|
+
this.sample,
|
|
697
|
+
`(${pluralize("digit", this.precision)})`
|
|
698
|
+
].join(" ");
|
|
699
|
+
}
|
|
700
|
+
};
|
|
701
|
+
var SchemaMatching = class extends AsymmetricMatcher {
|
|
702
|
+
constructor(sample, inverse = false) {
|
|
703
|
+
if (!isStandardSchema(sample)) throw new TypeError("SchemaMatching expected to receive a Standard Schema.");
|
|
704
|
+
super(sample, inverse);
|
|
705
|
+
}
|
|
706
|
+
asymmetricMatch(other) {
|
|
707
|
+
const result = this.sample["~standard"].validate(other);
|
|
708
|
+
if (result instanceof Promise) throw new TypeError("Async schema validation is not supported in asymmetric matchers.");
|
|
709
|
+
this.result = result;
|
|
710
|
+
const pass = !this.result.issues || this.result.issues.length === 0;
|
|
711
|
+
return this.inverse ? !pass : pass;
|
|
712
|
+
}
|
|
713
|
+
toString() {
|
|
714
|
+
return `Schema${this.inverse ? "Not" : ""}Matching`;
|
|
715
|
+
}
|
|
716
|
+
getExpectedType() {
|
|
717
|
+
return "object";
|
|
718
|
+
}
|
|
719
|
+
toAsymmetricMatcher() {
|
|
720
|
+
const { utils } = this.getMatcherContext();
|
|
721
|
+
if ((this.result?.issues || []).length > 0) return `${this.toString()} ${utils.stringify(this.result, void 0, { printBasicPrototype: false })}`;
|
|
722
|
+
return this.toString();
|
|
723
|
+
}
|
|
724
|
+
};
|
|
725
|
+
const JestAsymmetricMatchers = (chai, utils) => {
|
|
726
|
+
utils.addMethod(chai.expect, "anything", () => new Anything());
|
|
727
|
+
utils.addMethod(chai.expect, "any", (expected) => new Any(expected));
|
|
728
|
+
utils.addMethod(chai.expect, "stringContaining", (expected) => new StringContaining(expected));
|
|
729
|
+
utils.addMethod(chai.expect, "objectContaining", (expected) => new ObjectContaining(expected));
|
|
730
|
+
utils.addMethod(chai.expect, "arrayContaining", (expected) => new ArrayContaining(expected));
|
|
731
|
+
utils.addMethod(chai.expect, "stringMatching", (expected) => new StringMatching(expected));
|
|
732
|
+
utils.addMethod(chai.expect, "closeTo", (expected, precision) => new CloseTo(expected, precision));
|
|
733
|
+
utils.addMethod(chai.expect, "schemaMatching", (expected) => new SchemaMatching(expected));
|
|
734
|
+
chai.expect.not = {
|
|
735
|
+
stringContaining: (expected) => new StringContaining(expected, true),
|
|
736
|
+
objectContaining: (expected) => new ObjectContaining(expected, true),
|
|
737
|
+
arrayContaining: (expected) => new ArrayContaining(expected, true),
|
|
738
|
+
stringMatching: (expected) => new StringMatching(expected, true),
|
|
739
|
+
closeTo: (expected, precision) => new CloseTo(expected, precision, true),
|
|
740
|
+
schemaMatching: (expected) => new SchemaMatching(expected, true)
|
|
741
|
+
};
|
|
742
|
+
};
|
|
743
|
+
function createAssertionMessage(util, assertion, hasArgs) {
|
|
744
|
+
const soft = util.flag(assertion, "soft") ? ".soft" : "";
|
|
745
|
+
const not = util.flag(assertion, "negate") ? "not." : "";
|
|
746
|
+
const name = `${util.flag(assertion, "_name")}(${hasArgs ? "expected" : ""})`;
|
|
747
|
+
const promiseName = util.flag(assertion, "promise");
|
|
748
|
+
return `expect${soft}(actual)${promiseName ? `.${promiseName}` : ""}.${not}${name}`;
|
|
749
|
+
}
|
|
750
|
+
function recordAsyncExpect(_test, promise, assertion, error, isSoft) {
|
|
751
|
+
const test = _test;
|
|
752
|
+
if (test && promise instanceof Promise) {
|
|
753
|
+
promise = promise.finally(() => {
|
|
754
|
+
if (!test.promises) return;
|
|
755
|
+
const index = test.promises.indexOf(promise);
|
|
756
|
+
if (index !== -1) test.promises.splice(index, 1);
|
|
757
|
+
});
|
|
758
|
+
if (!test.promises) test.promises = [];
|
|
759
|
+
if (isSoft) promise = promise.then(noop, (err) => {
|
|
760
|
+
handleTestError(test, err);
|
|
761
|
+
});
|
|
762
|
+
test.promises.push(promise);
|
|
763
|
+
let resolved = false;
|
|
764
|
+
test.onFinished ?? (test.onFinished = []);
|
|
765
|
+
test.onFinished.push(() => {
|
|
766
|
+
if (!resolved) {
|
|
767
|
+
const stack = (globalThis.__vitest_worker__?.onFilterStackTrace || ((s) => s || ""))(error.stack);
|
|
768
|
+
console.warn([
|
|
769
|
+
`Promise returned by \`${assertion}\` was not awaited. `,
|
|
770
|
+
"Vitest currently auto-awaits hanging assertions at the end of the test, but this will cause the test to fail in the next Vitest major. ",
|
|
771
|
+
"Please remember to await the assertion.\n",
|
|
772
|
+
stack
|
|
773
|
+
].join(""));
|
|
774
|
+
}
|
|
775
|
+
});
|
|
776
|
+
return {
|
|
777
|
+
then(onFulfilled, onRejected) {
|
|
778
|
+
resolved = true;
|
|
779
|
+
return promise.then(onFulfilled, onRejected);
|
|
780
|
+
},
|
|
781
|
+
catch(onRejected) {
|
|
782
|
+
resolved = true;
|
|
783
|
+
return promise.catch(onRejected);
|
|
784
|
+
},
|
|
785
|
+
finally(onFinally) {
|
|
786
|
+
resolved = true;
|
|
787
|
+
return promise.finally(onFinally);
|
|
788
|
+
},
|
|
789
|
+
[Symbol.toStringTag]: "Promise"
|
|
790
|
+
};
|
|
791
|
+
}
|
|
792
|
+
return promise;
|
|
793
|
+
}
|
|
794
|
+
function handleTestError(test, err) {
|
|
795
|
+
var _test$result;
|
|
796
|
+
test.result || (test.result = { state: "fail" });
|
|
797
|
+
test.result.state = "fail";
|
|
798
|
+
(_test$result = test.result).errors || (_test$result.errors = []);
|
|
799
|
+
test.result.errors.push(processError(err));
|
|
800
|
+
}
|
|
801
|
+
function wrapAssertion(utils, name, fn) {
|
|
802
|
+
return function(...args) {
|
|
803
|
+
if (name !== "withTest") utils.flag(this, "_name", name);
|
|
804
|
+
if (!utils.flag(this, "soft")) try {
|
|
805
|
+
return fn.apply(this, args);
|
|
806
|
+
} finally {}
|
|
807
|
+
const test = utils.flag(this, "vitest-test");
|
|
808
|
+
if (!test) throw new Error("expect.soft() can only be used inside a test");
|
|
809
|
+
try {
|
|
810
|
+
const result = fn.apply(this, args);
|
|
811
|
+
if (result && typeof result === "object" && typeof result.then === "function") return result.then(noop, (err) => {
|
|
812
|
+
handleTestError(test, err);
|
|
813
|
+
});
|
|
814
|
+
return result;
|
|
815
|
+
} catch (err) {
|
|
816
|
+
handleTestError(test, err);
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
const JestChaiExpect = (chai, utils) => {
|
|
821
|
+
const { AssertionError } = chai;
|
|
822
|
+
const customTesters = getCustomEqualityTesters();
|
|
823
|
+
function def(name, fn) {
|
|
824
|
+
const addMethod = (n) => {
|
|
825
|
+
const softWrapper = wrapAssertion(utils, n, fn);
|
|
826
|
+
utils.addMethod(chai.Assertion.prototype, n, softWrapper);
|
|
827
|
+
utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, n, softWrapper);
|
|
828
|
+
};
|
|
829
|
+
if (Array.isArray(name)) name.forEach((n) => addMethod(n));
|
|
830
|
+
else addMethod(name);
|
|
831
|
+
}
|
|
832
|
+
[
|
|
833
|
+
"throw",
|
|
834
|
+
"throws",
|
|
835
|
+
"Throw"
|
|
836
|
+
].forEach((m) => {
|
|
837
|
+
utils.overwriteMethod(chai.Assertion.prototype, m, (_super) => {
|
|
838
|
+
return function(...args) {
|
|
839
|
+
const promise = utils.flag(this, "promise");
|
|
840
|
+
const object = utils.flag(this, "object");
|
|
841
|
+
const isNot = utils.flag(this, "negate");
|
|
842
|
+
if (promise === "rejects") utils.flag(this, "object", () => {
|
|
843
|
+
throw object;
|
|
844
|
+
});
|
|
845
|
+
else if (promise === "resolves" && typeof object !== "function") if (!isNot) throw new AssertionError(utils.flag(this, "message") || "expected promise to throw an error, but it didn't", { showDiff: false }, utils.flag(this, "ssfi"));
|
|
846
|
+
else return;
|
|
847
|
+
_super.apply(this, args);
|
|
848
|
+
};
|
|
849
|
+
});
|
|
850
|
+
});
|
|
851
|
+
def("withTest", function(test) {
|
|
852
|
+
utils.flag(this, "vitest-test", test);
|
|
853
|
+
return this;
|
|
854
|
+
});
|
|
855
|
+
def("toEqual", function(expected) {
|
|
856
|
+
const actual = utils.flag(this, "object");
|
|
857
|
+
const equal = equals(actual, expected, [...customTesters, iterableEquality]);
|
|
858
|
+
return this.assert(equal, "expected #{this} to deeply equal #{exp}", "expected #{this} to not deeply equal #{exp}", expected, actual);
|
|
859
|
+
});
|
|
860
|
+
def("toStrictEqual", function(expected) {
|
|
861
|
+
const obj = utils.flag(this, "object");
|
|
862
|
+
const equal = equals(obj, expected, [
|
|
863
|
+
...customTesters,
|
|
864
|
+
iterableEquality,
|
|
865
|
+
typeEquality,
|
|
866
|
+
sparseArrayEquality,
|
|
867
|
+
arrayBufferEquality
|
|
868
|
+
], true);
|
|
869
|
+
return this.assert(equal, "expected #{this} to strictly equal #{exp}", "expected #{this} to not strictly equal #{exp}", expected, obj);
|
|
870
|
+
});
|
|
871
|
+
def("toBe", function(expected) {
|
|
872
|
+
const actual = this._obj;
|
|
873
|
+
const pass = Object.is(actual, expected);
|
|
874
|
+
let deepEqualityName = "";
|
|
875
|
+
if (!pass) {
|
|
876
|
+
if (equals(actual, expected, [
|
|
877
|
+
...customTesters,
|
|
878
|
+
iterableEquality,
|
|
879
|
+
typeEquality,
|
|
880
|
+
sparseArrayEquality,
|
|
881
|
+
arrayBufferEquality
|
|
882
|
+
], true)) deepEqualityName = "toStrictEqual";
|
|
883
|
+
else if (equals(actual, expected, [...customTesters, iterableEquality])) deepEqualityName = "toEqual";
|
|
884
|
+
}
|
|
885
|
+
return this.assert(pass, generateToBeMessage(deepEqualityName), "expected #{this} not to be #{exp} // Object.is equality", expected, actual);
|
|
886
|
+
});
|
|
887
|
+
def("toMatchObject", function(expected) {
|
|
888
|
+
const actual = this._obj;
|
|
889
|
+
const pass = equals(actual, expected, [
|
|
890
|
+
...customTesters,
|
|
891
|
+
iterableEquality,
|
|
892
|
+
subsetEquality
|
|
893
|
+
]);
|
|
894
|
+
const isNot = utils.flag(this, "negate");
|
|
895
|
+
const { subset: actualSubset, stripped } = getObjectSubset(actual, expected, customTesters);
|
|
896
|
+
if (pass && isNot || !pass && !isNot) {
|
|
897
|
+
const msg = utils.getMessage(this, [
|
|
898
|
+
pass,
|
|
899
|
+
"expected #{this} to match object #{exp}",
|
|
900
|
+
"expected #{this} to not match object #{exp}",
|
|
901
|
+
expected,
|
|
902
|
+
actualSubset,
|
|
903
|
+
false
|
|
904
|
+
]);
|
|
905
|
+
throw new AssertionError(stripped === 0 ? msg : `${msg}\n(${stripped} matching ${stripped === 1 ? "property" : "properties"} omitted from actual)`, {
|
|
906
|
+
showDiff: true,
|
|
907
|
+
expected,
|
|
908
|
+
actual: actualSubset
|
|
909
|
+
});
|
|
910
|
+
}
|
|
911
|
+
});
|
|
912
|
+
def("toMatch", function(expected) {
|
|
913
|
+
const actual = this._obj;
|
|
914
|
+
if (typeof actual !== "string") throw new TypeError(`.toMatch() expects to receive a string, but got ${typeof actual}`);
|
|
915
|
+
return this.assert(typeof expected === "string" ? actual.includes(expected) : actual.match(expected), `expected #{this} to match #{exp}`, `expected #{this} not to match #{exp}`, expected, actual);
|
|
916
|
+
});
|
|
917
|
+
def("toContain", function(item) {
|
|
918
|
+
const actual = this._obj;
|
|
919
|
+
if (typeof Node !== "undefined" && actual instanceof Node) {
|
|
920
|
+
if (!(item instanceof Node)) throw new TypeError(`toContain() expected a DOM node as the argument, but got ${typeof item}`);
|
|
921
|
+
return this.assert(actual.contains(item), "expected #{this} to contain element #{exp}", "expected #{this} not to contain element #{exp}", item, actual);
|
|
922
|
+
}
|
|
923
|
+
if (typeof DOMTokenList !== "undefined" && actual instanceof DOMTokenList) {
|
|
924
|
+
assertTypes(item, "class name", ["string"]);
|
|
925
|
+
const expectedClassList = utils.flag(this, "negate") ? actual.value.replace(item, "").trim() : `${actual.value} ${item}`;
|
|
926
|
+
return this.assert(actual.contains(item), `expected "${actual.value}" to contain "${item}"`, `expected "${actual.value}" not to contain "${item}"`, expectedClassList, actual.value);
|
|
927
|
+
}
|
|
928
|
+
if (typeof actual === "string" && typeof item === "string") return this.assert(actual.includes(item), `expected #{this} to contain #{exp}`, `expected #{this} not to contain #{exp}`, item, actual);
|
|
929
|
+
if (actual != null && typeof actual !== "string") utils.flag(this, "object", Array.from(actual));
|
|
930
|
+
return this.contain(item);
|
|
931
|
+
});
|
|
932
|
+
def("toContainEqual", function(expected) {
|
|
933
|
+
const obj = utils.flag(this, "object");
|
|
934
|
+
const index = Array.from(obj).findIndex((item) => {
|
|
935
|
+
return equals(item, expected, customTesters);
|
|
936
|
+
});
|
|
937
|
+
this.assert(index !== -1, "expected #{this} to deep equally contain #{exp}", "expected #{this} to not deep equally contain #{exp}", expected);
|
|
938
|
+
});
|
|
939
|
+
def("toBeTruthy", function() {
|
|
940
|
+
const obj = utils.flag(this, "object");
|
|
941
|
+
this.assert(Boolean(obj), "expected #{this} to be truthy", "expected #{this} to not be truthy", true, obj);
|
|
942
|
+
});
|
|
943
|
+
def("toBeFalsy", function() {
|
|
944
|
+
const obj = utils.flag(this, "object");
|
|
945
|
+
this.assert(!obj, "expected #{this} to be falsy", "expected #{this} to not be falsy", false, obj);
|
|
946
|
+
});
|
|
947
|
+
def("toBeGreaterThan", function(expected) {
|
|
948
|
+
const actual = this._obj;
|
|
949
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
950
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
951
|
+
return this.assert(actual > expected, `expected ${actual} to be greater than ${expected}`, `expected ${actual} to be not greater than ${expected}`, expected, actual, false);
|
|
952
|
+
});
|
|
953
|
+
def("toBeGreaterThanOrEqual", function(expected) {
|
|
954
|
+
const actual = this._obj;
|
|
955
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
956
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
957
|
+
return this.assert(actual >= expected, `expected ${actual} to be greater than or equal to ${expected}`, `expected ${actual} to be not greater than or equal to ${expected}`, expected, actual, false);
|
|
958
|
+
});
|
|
959
|
+
def("toBeLessThan", function(expected) {
|
|
960
|
+
const actual = this._obj;
|
|
961
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
962
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
963
|
+
return this.assert(actual < expected, `expected ${actual} to be less than ${expected}`, `expected ${actual} to be not less than ${expected}`, expected, actual, false);
|
|
964
|
+
});
|
|
965
|
+
def("toBeLessThanOrEqual", function(expected) {
|
|
966
|
+
const actual = this._obj;
|
|
967
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
968
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
969
|
+
return this.assert(actual <= expected, `expected ${actual} to be less than or equal to ${expected}`, `expected ${actual} to be not less than or equal to ${expected}`, expected, actual, false);
|
|
970
|
+
});
|
|
971
|
+
def("toBeNaN", function() {
|
|
972
|
+
const obj = utils.flag(this, "object");
|
|
973
|
+
this.assert(Number.isNaN(obj), "expected #{this} to be NaN", "expected #{this} not to be NaN", NaN, obj);
|
|
974
|
+
});
|
|
975
|
+
def("toBeUndefined", function() {
|
|
976
|
+
const obj = utils.flag(this, "object");
|
|
977
|
+
this.assert(void 0 === obj, "expected #{this} to be undefined", "expected #{this} not to be undefined", void 0, obj);
|
|
978
|
+
});
|
|
979
|
+
def("toBeNull", function() {
|
|
980
|
+
const obj = utils.flag(this, "object");
|
|
981
|
+
this.assert(obj === null, "expected #{this} to be null", "expected #{this} not to be null", null, obj);
|
|
982
|
+
});
|
|
983
|
+
def("toBeNullable", function() {
|
|
984
|
+
const obj = utils.flag(this, "object");
|
|
985
|
+
this.assert(obj == null, "expected #{this} to be nullish", "expected #{this} not to be nullish", null, obj);
|
|
986
|
+
});
|
|
987
|
+
def("toBeDefined", function() {
|
|
988
|
+
const obj = utils.flag(this, "object");
|
|
989
|
+
this.assert(typeof obj !== "undefined", "expected #{this} to be defined", "expected #{this} to be undefined", obj);
|
|
990
|
+
});
|
|
991
|
+
def("toBeTypeOf", function(expected) {
|
|
992
|
+
const actual = typeof this._obj;
|
|
993
|
+
const equal = expected === actual;
|
|
994
|
+
return this.assert(equal, "expected #{this} to be type of #{exp}", "expected #{this} not to be type of #{exp}", expected, actual);
|
|
995
|
+
});
|
|
996
|
+
def("toBeInstanceOf", function(obj) {
|
|
997
|
+
return this.instanceOf(obj);
|
|
998
|
+
});
|
|
999
|
+
def("toHaveLength", function(length) {
|
|
1000
|
+
return this.have.length(length);
|
|
1001
|
+
});
|
|
1002
|
+
def("toHaveProperty", function(...args) {
|
|
1003
|
+
if (Array.isArray(args[0])) args[0] = args[0].map((key) => String(key).replace(/([.[\]])/g, "\\$1")).join(".");
|
|
1004
|
+
const actual = this._obj;
|
|
1005
|
+
const [propertyName, expected] = args;
|
|
1006
|
+
const getValue = () => {
|
|
1007
|
+
if (Object.hasOwn(actual, propertyName)) return {
|
|
1008
|
+
value: actual[propertyName],
|
|
1009
|
+
exists: true
|
|
1010
|
+
};
|
|
1011
|
+
return utils.getPathInfo(actual, propertyName);
|
|
1012
|
+
};
|
|
1013
|
+
const { value, exists } = getValue();
|
|
1014
|
+
const pass = exists && (args.length === 1 || equals(expected, value, customTesters));
|
|
1015
|
+
const valueString = args.length === 1 ? "" : ` with value ${utils.objDisplay(expected)}`;
|
|
1016
|
+
return this.assert(pass, `expected #{this} to have property "${propertyName}"${valueString}`, `expected #{this} to not have property "${propertyName}"${valueString}`, expected, exists ? value : void 0);
|
|
1017
|
+
});
|
|
1018
|
+
def("toBeCloseTo", function(received, precision = 2) {
|
|
1019
|
+
const expected = this._obj;
|
|
1020
|
+
let pass = false;
|
|
1021
|
+
let expectedDiff = 0;
|
|
1022
|
+
let receivedDiff = 0;
|
|
1023
|
+
if (received === Number.POSITIVE_INFINITY && expected === Number.POSITIVE_INFINITY) pass = true;
|
|
1024
|
+
else if (received === Number.NEGATIVE_INFINITY && expected === Number.NEGATIVE_INFINITY) pass = true;
|
|
1025
|
+
else {
|
|
1026
|
+
expectedDiff = 10 ** -precision / 2;
|
|
1027
|
+
receivedDiff = Math.abs(expected - received);
|
|
1028
|
+
pass = receivedDiff < expectedDiff;
|
|
1029
|
+
}
|
|
1030
|
+
return this.assert(pass, `expected #{this} to be close to #{exp}, received difference is ${receivedDiff}, but expected ${expectedDiff}`, `expected #{this} to not be close to #{exp}, received difference is ${receivedDiff}, but expected ${expectedDiff}`, received, expected, false);
|
|
1031
|
+
});
|
|
1032
|
+
function assertIsMock(assertion) {
|
|
1033
|
+
if (!isMockFunction(assertion._obj)) throw new TypeError(`${utils.inspect(assertion._obj)} is not a spy or a call to a spy!`);
|
|
1034
|
+
}
|
|
1035
|
+
function getSpy(assertion) {
|
|
1036
|
+
assertIsMock(assertion);
|
|
1037
|
+
return assertion._obj;
|
|
1038
|
+
}
|
|
1039
|
+
def(["toHaveBeenCalledTimes", "toBeCalledTimes"], function(number) {
|
|
1040
|
+
const spy = getSpy(this);
|
|
1041
|
+
const spyName = spy.getMockName();
|
|
1042
|
+
const callCount = spy.mock.calls.length;
|
|
1043
|
+
return this.assert(callCount === number, `expected "${spyName}" to be called #{exp} times, but got ${callCount} times`, `expected "${spyName}" to not be called #{exp} times`, number, callCount, false);
|
|
1044
|
+
});
|
|
1045
|
+
def("toHaveBeenCalledOnce", function() {
|
|
1046
|
+
const spy = getSpy(this);
|
|
1047
|
+
const spyName = spy.getMockName();
|
|
1048
|
+
const callCount = spy.mock.calls.length;
|
|
1049
|
+
return this.assert(callCount === 1, `expected "${spyName}" to be called once, but got ${callCount} times`, `expected "${spyName}" to not be called once`, 1, callCount, false);
|
|
1050
|
+
});
|
|
1051
|
+
def(["toHaveBeenCalled", "toBeCalled"], function() {
|
|
1052
|
+
const spy = getSpy(this);
|
|
1053
|
+
const spyName = spy.getMockName();
|
|
1054
|
+
const callCount = spy.mock.calls.length;
|
|
1055
|
+
const called = callCount > 0;
|
|
1056
|
+
const isNot = utils.flag(this, "negate");
|
|
1057
|
+
let msg = utils.getMessage(this, [
|
|
1058
|
+
called,
|
|
1059
|
+
`expected "${spyName}" to be called at least once`,
|
|
1060
|
+
`expected "${spyName}" to not be called at all, but actually been called ${callCount} times`,
|
|
1061
|
+
true,
|
|
1062
|
+
called
|
|
1063
|
+
]);
|
|
1064
|
+
if (called && isNot) msg = formatCalls(spy, msg);
|
|
1065
|
+
if (called && isNot || !called && !isNot) throw new AssertionError(msg);
|
|
1066
|
+
});
|
|
1067
|
+
function equalsArgumentArray(a, b) {
|
|
1068
|
+
return a.length === b.length && a.every((aItem, i) => equals(aItem, b[i], [...customTesters, iterableEquality]));
|
|
1069
|
+
}
|
|
1070
|
+
def(["toHaveBeenCalledWith", "toBeCalledWith"], function(...args) {
|
|
1071
|
+
const spy = getSpy(this);
|
|
1072
|
+
const spyName = spy.getMockName();
|
|
1073
|
+
const pass = spy.mock.calls.some((callArg) => equalsArgumentArray(callArg, args));
|
|
1074
|
+
const isNot = utils.flag(this, "negate");
|
|
1075
|
+
const msg = utils.getMessage(this, [
|
|
1076
|
+
pass,
|
|
1077
|
+
`expected "${spyName}" to be called with arguments: #{exp}`,
|
|
1078
|
+
`expected "${spyName}" to not be called with arguments: #{exp}`,
|
|
1079
|
+
args
|
|
1080
|
+
]);
|
|
1081
|
+
if (pass && isNot || !pass && !isNot) throw new AssertionError(formatCalls(spy, msg, args));
|
|
1082
|
+
});
|
|
1083
|
+
def("toHaveBeenCalledExactlyOnceWith", function(...args) {
|
|
1084
|
+
const spy = getSpy(this);
|
|
1085
|
+
const spyName = spy.getMockName();
|
|
1086
|
+
const callCount = spy.mock.calls.length;
|
|
1087
|
+
const pass = spy.mock.calls.some((callArg) => equalsArgumentArray(callArg, args)) && callCount === 1;
|
|
1088
|
+
const isNot = utils.flag(this, "negate");
|
|
1089
|
+
const msg = utils.getMessage(this, [
|
|
1090
|
+
pass,
|
|
1091
|
+
`expected "${spyName}" to be called once with arguments: #{exp}`,
|
|
1092
|
+
`expected "${spyName}" to not be called once with arguments: #{exp}`,
|
|
1093
|
+
args
|
|
1094
|
+
]);
|
|
1095
|
+
if (pass && isNot || !pass && !isNot) throw new AssertionError(formatCalls(spy, msg, args));
|
|
1096
|
+
});
|
|
1097
|
+
def("toHaveBeenNthCalledWith", function(times, ...args) {
|
|
1098
|
+
const spy = getSpy(this);
|
|
1099
|
+
const spyName = spy.getMockName();
|
|
1100
|
+
const nthCall = spy.mock.calls[times - 1];
|
|
1101
|
+
const callCount = spy.mock.calls.length;
|
|
1102
|
+
const isCalled = times <= callCount;
|
|
1103
|
+
this.assert(nthCall && equalsArgumentArray(nthCall, args), `expected ${ordinal(times)} "${spyName}" call to have been called with #{exp}${isCalled ? `` : `, but called only ${callCount} times`}`, `expected ${ordinal(times)} "${spyName}" call to not have been called with #{exp}`, args, nthCall, isCalled);
|
|
1104
|
+
});
|
|
1105
|
+
def("toHaveBeenLastCalledWith", function(...args) {
|
|
1106
|
+
const spy = getSpy(this);
|
|
1107
|
+
const spyName = spy.getMockName();
|
|
1108
|
+
const lastCall = spy.mock.calls.at(-1);
|
|
1109
|
+
this.assert(lastCall && equalsArgumentArray(lastCall, args), `expected last "${spyName}" call to have been called with #{exp}`, `expected last "${spyName}" call to not have been called with #{exp}`, args, lastCall);
|
|
1110
|
+
});
|
|
1111
|
+
/**
|
|
1112
|
+
* Used for `toHaveBeenCalledBefore` and `toHaveBeenCalledAfter` to determine if the expected spy was called before the result spy.
|
|
1113
|
+
*/
|
|
1114
|
+
function isSpyCalledBeforeAnotherSpy(beforeSpy, afterSpy, failIfNoFirstInvocation) {
|
|
1115
|
+
const beforeInvocationCallOrder = beforeSpy.mock.invocationCallOrder;
|
|
1116
|
+
const afterInvocationCallOrder = afterSpy.mock.invocationCallOrder;
|
|
1117
|
+
if (beforeInvocationCallOrder.length === 0) return !failIfNoFirstInvocation;
|
|
1118
|
+
if (afterInvocationCallOrder.length === 0) return false;
|
|
1119
|
+
return beforeInvocationCallOrder[0] < afterInvocationCallOrder[0];
|
|
1120
|
+
}
|
|
1121
|
+
def(["toHaveBeenCalledBefore"], function(resultSpy, failIfNoFirstInvocation = true) {
|
|
1122
|
+
const expectSpy = getSpy(this);
|
|
1123
|
+
if (!isMockFunction(resultSpy)) throw new TypeError(`${utils.inspect(resultSpy)} is not a spy or a call to a spy`);
|
|
1124
|
+
this.assert(isSpyCalledBeforeAnotherSpy(expectSpy, resultSpy, failIfNoFirstInvocation), `expected "${expectSpy.getMockName()}" to have been called before "${resultSpy.getMockName()}"`, `expected "${expectSpy.getMockName()}" to not have been called before "${resultSpy.getMockName()}"`, resultSpy, expectSpy);
|
|
1125
|
+
});
|
|
1126
|
+
def(["toHaveBeenCalledAfter"], function(resultSpy, failIfNoFirstInvocation = true) {
|
|
1127
|
+
const expectSpy = getSpy(this);
|
|
1128
|
+
if (!isMockFunction(resultSpy)) throw new TypeError(`${utils.inspect(resultSpy)} is not a spy or a call to a spy`);
|
|
1129
|
+
this.assert(isSpyCalledBeforeAnotherSpy(resultSpy, expectSpy, failIfNoFirstInvocation), `expected "${expectSpy.getMockName()}" to have been called after "${resultSpy.getMockName()}"`, `expected "${expectSpy.getMockName()}" to not have been called after "${resultSpy.getMockName()}"`, resultSpy, expectSpy);
|
|
1130
|
+
});
|
|
1131
|
+
def(["toThrow", "toThrowError"], function(expected) {
|
|
1132
|
+
if (typeof expected === "string" || typeof expected === "undefined" || expected instanceof RegExp) return this.throws(expected === "" ? /^$/ : expected);
|
|
1133
|
+
const obj = this._obj;
|
|
1134
|
+
const promise = utils.flag(this, "promise");
|
|
1135
|
+
const isNot = utils.flag(this, "negate");
|
|
1136
|
+
let thrown = null;
|
|
1137
|
+
if (promise === "rejects") thrown = obj;
|
|
1138
|
+
else if (promise === "resolves" && typeof obj !== "function") if (!isNot) throw new AssertionError(utils.flag(this, "message") || "expected promise to throw an error, but it didn't", { showDiff: false }, utils.flag(this, "ssfi"));
|
|
1139
|
+
else return;
|
|
1140
|
+
else {
|
|
1141
|
+
let isThrow = false;
|
|
1142
|
+
try {
|
|
1143
|
+
obj();
|
|
1144
|
+
} catch (err) {
|
|
1145
|
+
isThrow = true;
|
|
1146
|
+
thrown = err;
|
|
1147
|
+
}
|
|
1148
|
+
if (!isThrow && !isNot) throw new AssertionError(utils.flag(this, "message") || "expected function to throw an error, but it didn't", { showDiff: false }, utils.flag(this, "ssfi"));
|
|
1149
|
+
}
|
|
1150
|
+
if (typeof expected === "function") {
|
|
1151
|
+
const name = expected.name || expected.prototype.constructor.name;
|
|
1152
|
+
return this.assert(thrown && thrown instanceof expected, `expected error to be instance of ${name}`, `expected error not to be instance of ${name}`, expected, thrown);
|
|
1153
|
+
}
|
|
1154
|
+
if (isError(expected)) {
|
|
1155
|
+
const equal = equals(thrown, expected, [...customTesters, iterableEquality]);
|
|
1156
|
+
return this.assert(equal, "expected a thrown error to be #{exp}", "expected a thrown error not to be #{exp}", expected, thrown);
|
|
1157
|
+
}
|
|
1158
|
+
if (typeof expected === "object" && "asymmetricMatch" in expected && typeof expected.asymmetricMatch === "function") {
|
|
1159
|
+
const matcher = expected;
|
|
1160
|
+
return this.assert(thrown && matcher.asymmetricMatch(thrown), "expected error to match asymmetric matcher", "expected error not to match asymmetric matcher", matcher, thrown);
|
|
1161
|
+
}
|
|
1162
|
+
const equal = equals(thrown, expected, [...customTesters, iterableEquality]);
|
|
1163
|
+
return this.assert(equal, "expected a thrown value to equal #{exp}", "expected a thrown value not to equal #{exp}", expected, thrown);
|
|
1164
|
+
});
|
|
1165
|
+
[{
|
|
1166
|
+
name: "toHaveResolved",
|
|
1167
|
+
condition: (spy) => spy.mock.settledResults.length > 0 && spy.mock.settledResults.some(({ type }) => type === "fulfilled"),
|
|
1168
|
+
action: "resolved"
|
|
1169
|
+
}, {
|
|
1170
|
+
name: ["toHaveReturned", "toReturn"],
|
|
1171
|
+
condition: (spy) => spy.mock.calls.length > 0 && spy.mock.results.some(({ type }) => type !== "throw"),
|
|
1172
|
+
action: "called"
|
|
1173
|
+
}].forEach(({ name, condition, action }) => {
|
|
1174
|
+
def(name, function() {
|
|
1175
|
+
const spy = getSpy(this);
|
|
1176
|
+
const spyName = spy.getMockName();
|
|
1177
|
+
const pass = condition(spy);
|
|
1178
|
+
this.assert(pass, `expected "${spyName}" to be successfully ${action} at least once`, `expected "${spyName}" to not be successfully ${action}`, pass, !pass, false);
|
|
1179
|
+
});
|
|
1180
|
+
});
|
|
1181
|
+
[{
|
|
1182
|
+
name: "toHaveResolvedTimes",
|
|
1183
|
+
condition: (spy, times) => spy.mock.settledResults.reduce((s, { type }) => type === "fulfilled" ? ++s : s, 0) === times,
|
|
1184
|
+
action: "resolved"
|
|
1185
|
+
}, {
|
|
1186
|
+
name: ["toHaveReturnedTimes", "toReturnTimes"],
|
|
1187
|
+
condition: (spy, times) => spy.mock.results.reduce((s, { type }) => type === "throw" ? s : ++s, 0) === times,
|
|
1188
|
+
action: "called"
|
|
1189
|
+
}].forEach(({ name, condition, action }) => {
|
|
1190
|
+
def(name, function(times) {
|
|
1191
|
+
const spy = getSpy(this);
|
|
1192
|
+
const spyName = spy.getMockName();
|
|
1193
|
+
const pass = condition(spy, times);
|
|
1194
|
+
this.assert(pass, `expected "${spyName}" to be successfully ${action} ${times} times`, `expected "${spyName}" to not be successfully ${action} ${times} times`, `expected resolved times: ${times}`, `received resolved times: ${pass}`, false);
|
|
1195
|
+
});
|
|
1196
|
+
});
|
|
1197
|
+
[{
|
|
1198
|
+
name: "toHaveResolvedWith",
|
|
1199
|
+
condition: (spy, value) => spy.mock.settledResults.some(({ type, value: result }) => type === "fulfilled" && equals(value, result)),
|
|
1200
|
+
action: "resolve"
|
|
1201
|
+
}, {
|
|
1202
|
+
name: ["toHaveReturnedWith", "toReturnWith"],
|
|
1203
|
+
condition: (spy, value) => spy.mock.results.some(({ type, value: result }) => type === "return" && equals(value, result)),
|
|
1204
|
+
action: "return"
|
|
1205
|
+
}].forEach(({ name, condition, action }) => {
|
|
1206
|
+
def(name, function(value) {
|
|
1207
|
+
const spy = getSpy(this);
|
|
1208
|
+
const pass = condition(spy, value);
|
|
1209
|
+
const isNot = utils.flag(this, "negate");
|
|
1210
|
+
if (pass && isNot || !pass && !isNot) {
|
|
1211
|
+
const spyName = spy.getMockName();
|
|
1212
|
+
const msg = utils.getMessage(this, [
|
|
1213
|
+
pass,
|
|
1214
|
+
`expected "${spyName}" to ${action} with: #{exp} at least once`,
|
|
1215
|
+
`expected "${spyName}" to not ${action} with: #{exp}`,
|
|
1216
|
+
value
|
|
1217
|
+
]);
|
|
1218
|
+
throw new AssertionError(formatReturns(spy, action === "return" ? spy.mock.results : spy.mock.settledResults, msg, value));
|
|
1219
|
+
}
|
|
1220
|
+
});
|
|
1221
|
+
});
|
|
1222
|
+
[{
|
|
1223
|
+
name: "toHaveLastResolvedWith",
|
|
1224
|
+
condition: (spy, value) => {
|
|
1225
|
+
const result = spy.mock.settledResults.at(-1);
|
|
1226
|
+
return Boolean(result && result.type === "fulfilled" && equals(result.value, value));
|
|
1227
|
+
},
|
|
1228
|
+
action: "resolve"
|
|
1229
|
+
}, {
|
|
1230
|
+
name: "toHaveLastReturnedWith",
|
|
1231
|
+
condition: (spy, value) => {
|
|
1232
|
+
const result = spy.mock.results.at(-1);
|
|
1233
|
+
return Boolean(result && result.type === "return" && equals(result.value, value));
|
|
1234
|
+
},
|
|
1235
|
+
action: "return"
|
|
1236
|
+
}].forEach(({ name, condition, action }) => {
|
|
1237
|
+
def(name, function(value) {
|
|
1238
|
+
const spy = getSpy(this);
|
|
1239
|
+
const result = (action === "return" ? spy.mock.results : spy.mock.settledResults).at(-1);
|
|
1240
|
+
const spyName = spy.getMockName();
|
|
1241
|
+
this.assert(condition(spy, value), `expected last "${spyName}" call to ${action} #{exp}`, `expected last "${spyName}" call to not ${action} #{exp}`, value, result?.value);
|
|
1242
|
+
});
|
|
1243
|
+
});
|
|
1244
|
+
[{
|
|
1245
|
+
name: "toHaveNthResolvedWith",
|
|
1246
|
+
condition: (spy, index, value) => {
|
|
1247
|
+
const result = spy.mock.settledResults[index - 1];
|
|
1248
|
+
return result && result.type === "fulfilled" && equals(result.value, value);
|
|
1249
|
+
},
|
|
1250
|
+
action: "resolve"
|
|
1251
|
+
}, {
|
|
1252
|
+
name: "toHaveNthReturnedWith",
|
|
1253
|
+
condition: (spy, index, value) => {
|
|
1254
|
+
const result = spy.mock.results[index - 1];
|
|
1255
|
+
return result && result.type === "return" && equals(result.value, value);
|
|
1256
|
+
},
|
|
1257
|
+
action: "return"
|
|
1258
|
+
}].forEach(({ name, condition, action }) => {
|
|
1259
|
+
def(name, function(nthCall, value) {
|
|
1260
|
+
const spy = getSpy(this);
|
|
1261
|
+
const spyName = spy.getMockName();
|
|
1262
|
+
const result = (action === "return" ? spy.mock.results : spy.mock.settledResults)[nthCall - 1];
|
|
1263
|
+
const ordinalCall = `${ordinal(nthCall)} call`;
|
|
1264
|
+
this.assert(condition(spy, nthCall, value), `expected ${ordinalCall} "${spyName}" call to ${action} #{exp}`, `expected ${ordinalCall} "${spyName}" call to not ${action} #{exp}`, value, result?.value);
|
|
1265
|
+
});
|
|
1266
|
+
});
|
|
1267
|
+
def("withContext", function(context) {
|
|
1268
|
+
for (const key in context) utils.flag(this, key, context[key]);
|
|
1269
|
+
return this;
|
|
1270
|
+
});
|
|
1271
|
+
utils.addProperty(chai.Assertion.prototype, "resolves", function __VITEST_RESOLVES__() {
|
|
1272
|
+
const error = /* @__PURE__ */ new Error("resolves");
|
|
1273
|
+
utils.flag(this, "promise", "resolves");
|
|
1274
|
+
utils.flag(this, "error", error);
|
|
1275
|
+
const test = utils.flag(this, "vitest-test");
|
|
1276
|
+
const obj = utils.flag(this, "object");
|
|
1277
|
+
if (utils.flag(this, "poll")) throw new SyntaxError(`expect.poll() is not supported in combination with .resolves`);
|
|
1278
|
+
if (typeof obj?.then !== "function") throw new TypeError(`You must provide a Promise to expect() when using .resolves, not '${typeof obj}'.`);
|
|
1279
|
+
const proxy = new Proxy(this, { get: (target, key, receiver) => {
|
|
1280
|
+
const result = Reflect.get(target, key, receiver);
|
|
1281
|
+
if (typeof result !== "function") return result instanceof chai.Assertion ? proxy : result;
|
|
1282
|
+
return (...args) => {
|
|
1283
|
+
utils.flag(this, "_name", key);
|
|
1284
|
+
return recordAsyncExpect(test, Promise.resolve(obj).then((value) => {
|
|
1285
|
+
utils.flag(this, "object", value);
|
|
1286
|
+
return result.call(this, ...args);
|
|
1287
|
+
}, (err) => {
|
|
1288
|
+
const _error = new AssertionError(`promise rejected "${utils.inspect(err)}" instead of resolving`, { showDiff: false });
|
|
1289
|
+
_error.cause = err;
|
|
1290
|
+
throw _error;
|
|
1291
|
+
}).catch((err) => {
|
|
1292
|
+
if (isError(err) && error.stack) err.stack = error.stack.replace(error.message, err.message);
|
|
1293
|
+
throw err;
|
|
1294
|
+
}), createAssertionMessage(utils, this, !!args.length), error);
|
|
1295
|
+
};
|
|
1296
|
+
} });
|
|
1297
|
+
return proxy;
|
|
1298
|
+
});
|
|
1299
|
+
utils.addProperty(chai.Assertion.prototype, "rejects", function __VITEST_REJECTS__() {
|
|
1300
|
+
const error = /* @__PURE__ */ new Error("rejects");
|
|
1301
|
+
utils.flag(this, "promise", "rejects");
|
|
1302
|
+
utils.flag(this, "error", error);
|
|
1303
|
+
const test = utils.flag(this, "vitest-test");
|
|
1304
|
+
const obj = utils.flag(this, "object");
|
|
1305
|
+
const wrapper = typeof obj === "function" ? obj() : obj;
|
|
1306
|
+
if (utils.flag(this, "poll")) throw new SyntaxError(`expect.poll() is not supported in combination with .rejects`);
|
|
1307
|
+
if (typeof wrapper?.then !== "function") throw new TypeError(`You must provide a Promise to expect() when using .rejects, not '${typeof wrapper}'.`);
|
|
1308
|
+
const proxy = new Proxy(this, { get: (target, key, receiver) => {
|
|
1309
|
+
const result = Reflect.get(target, key, receiver);
|
|
1310
|
+
if (typeof result !== "function") return result instanceof chai.Assertion ? proxy : result;
|
|
1311
|
+
return (...args) => {
|
|
1312
|
+
utils.flag(this, "_name", key);
|
|
1313
|
+
return recordAsyncExpect(test, Promise.resolve(wrapper).then((value) => {
|
|
1314
|
+
throw new AssertionError(`promise resolved "${utils.inspect(value)}" instead of rejecting`, {
|
|
1315
|
+
showDiff: true,
|
|
1316
|
+
expected: /* @__PURE__ */ new Error("rejected promise"),
|
|
1317
|
+
actual: value
|
|
1318
|
+
});
|
|
1319
|
+
}, (err) => {
|
|
1320
|
+
utils.flag(this, "object", err);
|
|
1321
|
+
return result.call(this, ...args);
|
|
1322
|
+
}).catch((err) => {
|
|
1323
|
+
if (isError(err) && error.stack) err.stack = error.stack.replace(error.message, err.message);
|
|
1324
|
+
throw err;
|
|
1325
|
+
}), createAssertionMessage(utils, this, !!args.length), error);
|
|
1326
|
+
};
|
|
1327
|
+
} });
|
|
1328
|
+
return proxy;
|
|
1329
|
+
});
|
|
1330
|
+
};
|
|
1331
|
+
function formatCalls(spy, msg, showActualCall) {
|
|
1332
|
+
if (spy.mock.calls.length) msg += y.gray(`\n\nReceived:\n\n${spy.mock.calls.map((callArg, i) => {
|
|
1333
|
+
let methodCall = y.bold(` ${ordinal(i + 1)} ${spy.getMockName()} call:\n\n`);
|
|
1334
|
+
if (showActualCall) methodCall += diff(showActualCall, callArg, { omitAnnotationLines: true });
|
|
1335
|
+
else methodCall += stringify(callArg).split("\n").map((line) => ` ${line}`).join("\n");
|
|
1336
|
+
methodCall += "\n";
|
|
1337
|
+
return methodCall;
|
|
1338
|
+
}).join("\n")}`);
|
|
1339
|
+
msg += y.gray(`\n\nNumber of calls: ${y.bold(spy.mock.calls.length)}\n`);
|
|
1340
|
+
return msg;
|
|
1341
|
+
}
|
|
1342
|
+
function formatReturns(spy, results, msg, showActualReturn) {
|
|
1343
|
+
if (results.length) msg += y.gray(`\n\nReceived:\n\n${results.map((callReturn, i) => {
|
|
1344
|
+
let methodCall = y.bold(` ${ordinal(i + 1)} ${spy.getMockName()} call return:\n\n`);
|
|
1345
|
+
if (showActualReturn) methodCall += diff(showActualReturn, callReturn.value, { omitAnnotationLines: true });
|
|
1346
|
+
else methodCall += stringify(callReturn).split("\n").map((line) => ` ${line}`).join("\n");
|
|
1347
|
+
methodCall += "\n";
|
|
1348
|
+
return methodCall;
|
|
1349
|
+
}).join("\n")}`);
|
|
1350
|
+
msg += y.gray(`\n\nNumber of calls: ${y.bold(spy.mock.calls.length)}\n`);
|
|
1351
|
+
return msg;
|
|
1352
|
+
}
|
|
1353
|
+
function getMatcherState(assertion, expect) {
|
|
1354
|
+
const obj = assertion._obj;
|
|
1355
|
+
const isNot = utils_exports.flag(assertion, "negate");
|
|
1356
|
+
const promise = utils_exports.flag(assertion, "promise") || "";
|
|
1357
|
+
const customMessage = utils_exports.flag(assertion, "message");
|
|
1358
|
+
const jestUtils = {
|
|
1359
|
+
...getMatcherUtils(),
|
|
1360
|
+
diff,
|
|
1361
|
+
stringify,
|
|
1362
|
+
iterableEquality,
|
|
1363
|
+
subsetEquality
|
|
1364
|
+
};
|
|
1365
|
+
let task = utils_exports.flag(assertion, "vitest-test");
|
|
1366
|
+
const currentTestName = task?.fullTestName ?? "";
|
|
1367
|
+
if (task?.type !== "test") task = void 0;
|
|
1368
|
+
return {
|
|
1369
|
+
state: {
|
|
1370
|
+
...getState(expect),
|
|
1371
|
+
task,
|
|
1372
|
+
currentTestName,
|
|
1373
|
+
customTesters: getCustomEqualityTesters(),
|
|
1374
|
+
isNot,
|
|
1375
|
+
utils: jestUtils,
|
|
1376
|
+
promise,
|
|
1377
|
+
equals,
|
|
1378
|
+
suppressedErrors: [],
|
|
1379
|
+
soft: utils_exports.flag(assertion, "soft"),
|
|
1380
|
+
poll: utils_exports.flag(assertion, "poll")
|
|
1381
|
+
},
|
|
1382
|
+
isNot,
|
|
1383
|
+
obj,
|
|
1384
|
+
customMessage
|
|
1385
|
+
};
|
|
1386
|
+
}
|
|
1387
|
+
var JestExtendError = class extends Error {
|
|
1388
|
+
constructor(message, actual, expected) {
|
|
1389
|
+
super(message);
|
|
1390
|
+
this.actual = actual;
|
|
1391
|
+
this.expected = expected;
|
|
1392
|
+
}
|
|
1393
|
+
};
|
|
1394
|
+
function JestExtendPlugin(c, expect, matchers) {
|
|
1395
|
+
return (_, utils) => {
|
|
1396
|
+
Object.entries(matchers).forEach(([expectAssertionName, expectAssertion]) => {
|
|
1397
|
+
function expectWrapper(...args) {
|
|
1398
|
+
const { state, isNot, obj, customMessage } = getMatcherState(this, expect);
|
|
1399
|
+
const result = expectAssertion.call(state, obj, ...args);
|
|
1400
|
+
if (result && typeof result === "object" && typeof result.then === "function") return result.then(({ pass, message, actual, expected }) => {
|
|
1401
|
+
if (pass && isNot || !pass && !isNot) throw new JestExtendError(customMessage != null ? customMessage : message(), actual, expected);
|
|
1402
|
+
});
|
|
1403
|
+
const { pass, message, actual, expected } = result;
|
|
1404
|
+
if (pass && isNot || !pass && !isNot) throw new JestExtendError(customMessage != null ? customMessage : message(), actual, expected);
|
|
1405
|
+
}
|
|
1406
|
+
const softWrapper = wrapAssertion(utils, expectAssertionName, expectWrapper);
|
|
1407
|
+
utils.addMethod(globalThis[JEST_MATCHERS_OBJECT].matchers, expectAssertionName, softWrapper);
|
|
1408
|
+
utils.addMethod(c.Assertion.prototype, expectAssertionName, softWrapper);
|
|
1409
|
+
class CustomMatcher extends AsymmetricMatcher {
|
|
1410
|
+
constructor(inverse = false, ...sample) {
|
|
1411
|
+
super(sample, inverse);
|
|
1412
|
+
}
|
|
1413
|
+
asymmetricMatch(other) {
|
|
1414
|
+
const { pass } = expectAssertion.call(this.getMatcherContext(expect), other, ...this.sample);
|
|
1415
|
+
return this.inverse ? !pass : pass;
|
|
1416
|
+
}
|
|
1417
|
+
toString() {
|
|
1418
|
+
return `${this.inverse ? "not." : ""}${expectAssertionName}`;
|
|
1419
|
+
}
|
|
1420
|
+
getExpectedType() {
|
|
1421
|
+
return "any";
|
|
1422
|
+
}
|
|
1423
|
+
toAsymmetricMatcher() {
|
|
1424
|
+
return `${this.toString()}<${this.sample.map((item) => stringify(item)).join(", ")}>`;
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
const customMatcher = (...sample) => new CustomMatcher(false, ...sample);
|
|
1428
|
+
Object.defineProperty(expect, expectAssertionName, {
|
|
1429
|
+
configurable: true,
|
|
1430
|
+
enumerable: true,
|
|
1431
|
+
value: customMatcher,
|
|
1432
|
+
writable: true
|
|
1433
|
+
});
|
|
1434
|
+
Object.defineProperty(expect.not, expectAssertionName, {
|
|
1435
|
+
configurable: true,
|
|
1436
|
+
enumerable: true,
|
|
1437
|
+
value: (...sample) => new CustomMatcher(true, ...sample),
|
|
1438
|
+
writable: true
|
|
1439
|
+
});
|
|
1440
|
+
Object.defineProperty(globalThis[ASYMMETRIC_MATCHERS_OBJECT], expectAssertionName, {
|
|
1441
|
+
configurable: true,
|
|
1442
|
+
enumerable: true,
|
|
1443
|
+
value: customMatcher,
|
|
1444
|
+
writable: true
|
|
1445
|
+
});
|
|
1446
|
+
});
|
|
1447
|
+
};
|
|
1448
|
+
}
|
|
1449
|
+
const JestExtend = (chai, utils) => {
|
|
1450
|
+
utils.addMethod(chai.expect, "extend", (expect, expects) => {
|
|
1451
|
+
use(JestExtendPlugin(chai, expect, expects));
|
|
1452
|
+
});
|
|
1453
|
+
};
|
|
1454
|
+
//#endregion
|
|
1455
|
+
export { ASYMMETRIC_MATCHERS_OBJECT, ChaiStyleAssertions, GLOBAL_EXPECT, JestAsymmetricMatchers, JestChaiExpect, JestExtend, addCustomEqualityTesters, createAssertionMessage, customMatchers, equals, getState, iterableEquality, recordAsyncExpect, setState, subsetEquality, wrapAssertion };
|
|
1456
|
+
|
|
1457
|
+
//# sourceMappingURL=index.js.map
|