@noma.to/qwik-testing-library 1.0.3 → 1.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/README.md +120 -7
- package/lib/_virtual/_commonjsHelpers.qwik.cjs +2 -0
- package/lib/_virtual/_commonjsHelpers.qwik.mjs +2 -0
- package/lib/_virtual/sourcemap-codec.umd.qwik.cjs +4 -0
- package/lib/_virtual/sourcemap-codec.umd.qwik.mjs +4 -0
- package/lib/index.qwik.cjs +4 -0
- package/lib/index.qwik.mjs +4 -0
- package/lib/lib/mock.qwik.cjs +14 -0
- package/lib/lib/mock.qwik.mjs +14 -0
- package/lib/lib/qwik-testing-library.qwik.cjs +4 -0
- package/lib/lib/qwik-testing-library.qwik.mjs +4 -0
- package/lib/node_modules/.pnpm/@jridgewell_sourcemap-codec@1.5.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.umd.qwik.cjs +425 -0
- package/lib/node_modules/.pnpm/@jridgewell_sourcemap-codec@1.5.0/node_modules/@jridgewell/sourcemap-codec/dist/sourcemap-codec.umd.qwik.mjs +425 -0
- package/lib/node_modules/.pnpm/@vitest_expect@2.1.1/node_modules/@vitest/expect/dist/index.qwik.cjs +1967 -0
- package/lib/node_modules/.pnpm/@vitest_expect@2.1.1/node_modules/@vitest/expect/dist/index.qwik.mjs +1967 -0
- package/lib/node_modules/.pnpm/@vitest_pretty-format@2.1.1/node_modules/@vitest/pretty-format/dist/index.qwik.cjs +1202 -0
- package/lib/node_modules/.pnpm/@vitest_pretty-format@2.1.1/node_modules/@vitest/pretty-format/dist/index.qwik.mjs +1202 -0
- package/lib/node_modules/.pnpm/@vitest_runner@2.1.1/node_modules/@vitest/runner/dist/chunk-tasks.qwik.cjs +44 -0
- package/lib/node_modules/.pnpm/@vitest_runner@2.1.1/node_modules/@vitest/runner/dist/chunk-tasks.qwik.mjs +44 -0
- package/lib/node_modules/.pnpm/@vitest_runner@2.1.1/node_modules/@vitest/runner/dist/index.qwik.cjs +665 -0
- package/lib/node_modules/.pnpm/@vitest_runner@2.1.1/node_modules/@vitest/runner/dist/index.qwik.mjs +665 -0
- package/lib/node_modules/.pnpm/@vitest_snapshot@2.1.1/node_modules/@vitest/snapshot/dist/index.qwik.cjs +1641 -0
- package/lib/node_modules/.pnpm/@vitest_snapshot@2.1.1/node_modules/@vitest/snapshot/dist/index.qwik.mjs +1641 -0
- package/lib/node_modules/.pnpm/@vitest_spy@2.1.1/node_modules/@vitest/spy/dist/index.qwik.cjs +148 -0
- package/lib/node_modules/.pnpm/@vitest_spy@2.1.1/node_modules/@vitest/spy/dist/index.qwik.mjs +148 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/chunk-display.qwik.cjs +158 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/chunk-display.qwik.mjs +158 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/diff.qwik.cjs +1731 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/diff.qwik.mjs +1731 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/error.qwik.cjs +137 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/error.qwik.mjs +137 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/helpers.qwik.cjs +150 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/helpers.qwik.mjs +150 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/index.qwik.cjs +81 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/index.qwik.mjs +81 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/source-map.qwik.cjs +216 -0
- package/lib/node_modules/.pnpm/@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/source-map.qwik.mjs +216 -0
- package/lib/node_modules/.pnpm/chai@5.1.1/node_modules/chai/chai.qwik.cjs +3905 -0
- package/lib/node_modules/.pnpm/chai@5.1.1/node_modules/chai/chai.qwik.mjs +3905 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/arguments.qwik.cjs +9 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/arguments.qwik.mjs +10 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/array.qwik.cjs +16 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/array.qwik.mjs +17 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/bigint.qwik.cjs +9 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/bigint.qwik.mjs +10 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/class.qwik.cjs +16 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/class.qwik.mjs +17 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/date.qwik.cjs +12 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/date.qwik.mjs +13 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/error.qwik.cjs +30 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/error.qwik.mjs +31 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/function.qwik.cjs +11 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/function.qwik.mjs +12 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/helpers.qwik.cjs +160 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/helpers.qwik.mjs +160 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/html.qwik.cjs +36 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/html.qwik.mjs +36 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/index.qwik.cjs +130 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/index.qwik.mjs +130 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/map.qwik.cjs +25 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/map.qwik.mjs +26 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/number.qwik.cjs +19 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/number.qwik.mjs +20 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/object.qwik.cjs +24 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/object.qwik.mjs +25 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/promise.qwik.cjs +17 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/promise.qwik.mjs +18 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/regexp.qwik.cjs +9 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/regexp.qwik.mjs +10 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/set.qwik.cjs +16 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/set.qwik.mjs +17 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/string.qwik.cjs +24 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/string.qwik.mjs +25 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/symbol.qwik.cjs +8 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/symbol.qwik.mjs +9 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/typedarray.qwik.cjs +34 -0
- package/lib/node_modules/.pnpm/loupe@3.1.1/node_modules/loupe/lib/typedarray.qwik.mjs +35 -0
- package/lib/node_modules/.pnpm/magic-string@0.30.11/node_modules/magic-string/dist/magic-string.es.qwik.cjs +968 -0
- package/lib/node_modules/.pnpm/magic-string@0.30.11/node_modules/magic-string/dist/magic-string.es.qwik.mjs +968 -0
- package/lib/node_modules/.pnpm/pathe@1.1.2/node_modules/pathe/dist/shared/pathe.ff20891b.qwik.cjs +101 -0
- package/lib/node_modules/.pnpm/pathe@1.1.2/node_modules/pathe/dist/shared/pathe.ff20891b.qwik.mjs +101 -0
- package/lib/node_modules/.pnpm/tinyrainbow@1.2.0/node_modules/tinyrainbow/dist/browser.qwik.cjs +7 -0
- package/lib/node_modules/.pnpm/tinyrainbow@1.2.0/node_modules/tinyrainbow/dist/browser.qwik.mjs +8 -0
- package/lib/node_modules/.pnpm/tinyrainbow@1.2.0/node_modules/tinyrainbow/dist/chunk-BVHSVHOK.qwik.cjs +80 -0
- package/lib/node_modules/.pnpm/tinyrainbow@1.2.0/node_modules/tinyrainbow/dist/chunk-BVHSVHOK.qwik.mjs +80 -0
- package/lib/node_modules/.pnpm/tinyspy@3.0.2/node_modules/tinyspy/dist/index.qwik.cjs +104 -0
- package/lib/node_modules/.pnpm/tinyspy@3.0.2/node_modules/tinyspy/dist/index.qwik.mjs +104 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/_commonjsHelpers.BFTU3MAI.qwik.cjs +8 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/_commonjsHelpers.BFTU3MAI.qwik.mjs +8 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/base.BlXpj3e_.qwik.cjs +6 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/base.BlXpj3e_.qwik.mjs +6 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/date.W2xKR2qe.qwik.cjs +56 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/date.W2xKR2qe.qwik.mjs +56 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/utils.Ck2hJTRs.qwik.cjs +16 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/utils.Ck2hJTRs.qwik.mjs +16 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/vi.D6IHiKAI.qwik.cjs +2556 -0
- package/lib/node_modules/.pnpm/vitest@2.1.1_@types_node@22.7.4_@vitest_ui@2.1.1_jsdom@25.0.1/node_modules/vitest/dist/chunks/vi.D6IHiKAI.qwik.mjs +2556 -0
- package/lib-types/lib/mock.d.ts +34 -0
- package/lib-types/lib/qwik-testing-library.d.ts +2 -1
- package/package.json +9 -8
package/lib/node_modules/.pnpm/@vitest_expect@2.1.1/node_modules/@vitest/expect/dist/index.qwik.mjs
ADDED
|
@@ -0,0 +1,1967 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
import f from "../../../../../tinyrainbow@1.2.0/node_modules/tinyrainbow/dist/browser.qwik.mjs";
|
|
5
|
+
import { diff, printDiffOrStringify } from "../../../../../@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/diff.qwik.mjs";
|
|
6
|
+
import { isMockFunction } from "../../../../../@vitest_spy@2.1.1/node_modules/@vitest/spy/dist/index.qwik.mjs";
|
|
7
|
+
import { processError } from "../../../../../@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/error.qwik.mjs";
|
|
8
|
+
import { use, util as utils_exports } from "../../../../../chai@5.1.1/node_modules/chai/chai.qwik.mjs";
|
|
9
|
+
import { getType, assertTypes, isObject } from "../../../../../@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/helpers.qwik.mjs";
|
|
10
|
+
import { s as stringify } from "../../../../../@vitest_utils@2.1.1/node_modules/@vitest/utils/dist/chunk-display.qwik.mjs";
|
|
11
|
+
const MATCHERS_OBJECT = Symbol.for("matchers-object");
|
|
12
|
+
const JEST_MATCHERS_OBJECT = Symbol.for("$$jest-matchers-object");
|
|
13
|
+
const GLOBAL_EXPECT = Symbol.for("expect-global");
|
|
14
|
+
const ASYMMETRIC_MATCHERS_OBJECT = Symbol.for(
|
|
15
|
+
"asymmetric-matchers-object"
|
|
16
|
+
);
|
|
17
|
+
if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
|
|
18
|
+
const globalState = /* @__PURE__ */ new WeakMap();
|
|
19
|
+
const matchers = /* @__PURE__ */ Object.create(null);
|
|
20
|
+
const customEqualityTesters = [];
|
|
21
|
+
const assymetricMatchers = /* @__PURE__ */ Object.create(null);
|
|
22
|
+
Object.defineProperty(globalThis, MATCHERS_OBJECT, {
|
|
23
|
+
get: () => globalState
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, {
|
|
26
|
+
configurable: true,
|
|
27
|
+
get: () => ({
|
|
28
|
+
state: globalState.get(globalThis[GLOBAL_EXPECT]),
|
|
29
|
+
matchers,
|
|
30
|
+
customEqualityTesters
|
|
31
|
+
})
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(globalThis, ASYMMETRIC_MATCHERS_OBJECT, {
|
|
34
|
+
get: () => assymetricMatchers
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function getState(expect) {
|
|
38
|
+
return globalThis[MATCHERS_OBJECT].get(expect);
|
|
39
|
+
}
|
|
40
|
+
function setState(state, expect) {
|
|
41
|
+
const map = globalThis[MATCHERS_OBJECT];
|
|
42
|
+
const current = map.get(expect) || {};
|
|
43
|
+
const results = Object.defineProperties(current, {
|
|
44
|
+
...Object.getOwnPropertyDescriptors(current),
|
|
45
|
+
...Object.getOwnPropertyDescriptors(state)
|
|
46
|
+
});
|
|
47
|
+
map.set(expect, results);
|
|
48
|
+
}
|
|
49
|
+
const EXPECTED_COLOR = f.green;
|
|
50
|
+
const RECEIVED_COLOR = f.red;
|
|
51
|
+
const INVERTED_COLOR = f.inverse;
|
|
52
|
+
const BOLD_WEIGHT = f.bold;
|
|
53
|
+
const DIM_COLOR = f.dim;
|
|
54
|
+
function matcherHint(matcherName, received = "received", expected = "expected", options = {}) {
|
|
55
|
+
const {
|
|
56
|
+
comment = "",
|
|
57
|
+
isDirectExpectCall = false,
|
|
58
|
+
// seems redundant with received === ''
|
|
59
|
+
isNot = false,
|
|
60
|
+
promise = "",
|
|
61
|
+
secondArgument = "",
|
|
62
|
+
expectedColor = EXPECTED_COLOR,
|
|
63
|
+
receivedColor = RECEIVED_COLOR,
|
|
64
|
+
secondArgumentColor = EXPECTED_COLOR
|
|
65
|
+
} = options;
|
|
66
|
+
let hint = "";
|
|
67
|
+
let dimString = "expect";
|
|
68
|
+
if (!isDirectExpectCall && received !== "") {
|
|
69
|
+
hint += DIM_COLOR(`${dimString}(`) + receivedColor(received);
|
|
70
|
+
dimString = ")";
|
|
71
|
+
}
|
|
72
|
+
if (promise !== "") {
|
|
73
|
+
hint += DIM_COLOR(`${dimString}.`) + promise;
|
|
74
|
+
dimString = "";
|
|
75
|
+
}
|
|
76
|
+
if (isNot) {
|
|
77
|
+
hint += `${DIM_COLOR(`${dimString}.`)}not`;
|
|
78
|
+
dimString = "";
|
|
79
|
+
}
|
|
80
|
+
if (matcherName.includes(".")) {
|
|
81
|
+
dimString += matcherName;
|
|
82
|
+
} else {
|
|
83
|
+
hint += DIM_COLOR(`${dimString}.`) + matcherName;
|
|
84
|
+
dimString = "";
|
|
85
|
+
}
|
|
86
|
+
if (expected === "") {
|
|
87
|
+
dimString += "()";
|
|
88
|
+
} else {
|
|
89
|
+
hint += DIM_COLOR(`${dimString}(`) + expectedColor(expected);
|
|
90
|
+
if (secondArgument) {
|
|
91
|
+
hint += DIM_COLOR(", ") + secondArgumentColor(secondArgument);
|
|
92
|
+
}
|
|
93
|
+
dimString = ")";
|
|
94
|
+
}
|
|
95
|
+
if (comment !== "") {
|
|
96
|
+
dimString += ` // ${comment}`;
|
|
97
|
+
}
|
|
98
|
+
if (dimString !== "") {
|
|
99
|
+
hint += DIM_COLOR(dimString);
|
|
100
|
+
}
|
|
101
|
+
return hint;
|
|
102
|
+
}
|
|
103
|
+
const SPACE_SYMBOL = "·";
|
|
104
|
+
function replaceTrailingSpaces(text) {
|
|
105
|
+
return text.replace(/\s+$/gm, (spaces) => SPACE_SYMBOL.repeat(spaces.length));
|
|
106
|
+
}
|
|
107
|
+
function printReceived(object) {
|
|
108
|
+
return RECEIVED_COLOR(replaceTrailingSpaces(stringify(object)));
|
|
109
|
+
}
|
|
110
|
+
function printExpected(value) {
|
|
111
|
+
return EXPECTED_COLOR(replaceTrailingSpaces(stringify(value)));
|
|
112
|
+
}
|
|
113
|
+
function getMatcherUtils() {
|
|
114
|
+
return {
|
|
115
|
+
EXPECTED_COLOR,
|
|
116
|
+
RECEIVED_COLOR,
|
|
117
|
+
INVERTED_COLOR,
|
|
118
|
+
BOLD_WEIGHT,
|
|
119
|
+
DIM_COLOR,
|
|
120
|
+
diff,
|
|
121
|
+
matcherHint,
|
|
122
|
+
printReceived,
|
|
123
|
+
printExpected,
|
|
124
|
+
printDiffOrStringify
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function addCustomEqualityTesters(newTesters) {
|
|
128
|
+
if (!Array.isArray(newTesters)) {
|
|
129
|
+
throw new TypeError(
|
|
130
|
+
`expect.customEqualityTesters: Must be set to an array of Testers. Was given "${getType(
|
|
131
|
+
newTesters
|
|
132
|
+
)}"`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
globalThis[JEST_MATCHERS_OBJECT].customEqualityTesters.push(
|
|
136
|
+
...newTesters
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
function getCustomEqualityTesters() {
|
|
140
|
+
return globalThis[JEST_MATCHERS_OBJECT].customEqualityTesters;
|
|
141
|
+
}
|
|
142
|
+
function equals(a, b, customTesters, strictCheck) {
|
|
143
|
+
customTesters = customTesters || [];
|
|
144
|
+
return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey);
|
|
145
|
+
}
|
|
146
|
+
function isAsymmetric(obj) {
|
|
147
|
+
return !!obj && typeof obj === "object" && "asymmetricMatch" in obj && isA("Function", obj.asymmetricMatch);
|
|
148
|
+
}
|
|
149
|
+
function asymmetricMatch(a, b) {
|
|
150
|
+
const asymmetricA = isAsymmetric(a);
|
|
151
|
+
const asymmetricB = isAsymmetric(b);
|
|
152
|
+
if (asymmetricA && asymmetricB) {
|
|
153
|
+
return void 0;
|
|
154
|
+
}
|
|
155
|
+
if (asymmetricA) {
|
|
156
|
+
return a.asymmetricMatch(b);
|
|
157
|
+
}
|
|
158
|
+
if (asymmetricB) {
|
|
159
|
+
return b.asymmetricMatch(a);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function eq(a, b, aStack, bStack, customTesters, hasKey2) {
|
|
163
|
+
let result = true;
|
|
164
|
+
const asymmetricResult = asymmetricMatch(a, b);
|
|
165
|
+
if (asymmetricResult !== void 0) {
|
|
166
|
+
return asymmetricResult;
|
|
167
|
+
}
|
|
168
|
+
const testerContext = { equals };
|
|
169
|
+
for (let i = 0; i < customTesters.length; i++) {
|
|
170
|
+
const customTesterResult = customTesters[i].call(
|
|
171
|
+
testerContext,
|
|
172
|
+
a,
|
|
173
|
+
b,
|
|
174
|
+
customTesters
|
|
175
|
+
);
|
|
176
|
+
if (customTesterResult !== void 0) {
|
|
177
|
+
return customTesterResult;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (a instanceof Error && b instanceof Error) {
|
|
181
|
+
return a.message === b.message;
|
|
182
|
+
}
|
|
183
|
+
if (typeof URL === "function" && a instanceof URL && b instanceof URL) {
|
|
184
|
+
return a.href === b.href;
|
|
185
|
+
}
|
|
186
|
+
if (Object.is(a, b)) {
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
if (a === null || b === null) {
|
|
190
|
+
return a === b;
|
|
191
|
+
}
|
|
192
|
+
const className = Object.prototype.toString.call(a);
|
|
193
|
+
if (className !== Object.prototype.toString.call(b)) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
switch (className) {
|
|
197
|
+
case "[object Boolean]":
|
|
198
|
+
case "[object String]":
|
|
199
|
+
case "[object Number]":
|
|
200
|
+
if (typeof a !== typeof b) {
|
|
201
|
+
return false;
|
|
202
|
+
} else if (typeof a !== "object" && typeof b !== "object") {
|
|
203
|
+
return Object.is(a, b);
|
|
204
|
+
} else {
|
|
205
|
+
return Object.is(a.valueOf(), b.valueOf());
|
|
206
|
+
}
|
|
207
|
+
case "[object Date]": {
|
|
208
|
+
const numA = +a;
|
|
209
|
+
const numB = +b;
|
|
210
|
+
return numA === numB || Number.isNaN(numA) && Number.isNaN(numB);
|
|
211
|
+
}
|
|
212
|
+
case "[object RegExp]":
|
|
213
|
+
return a.source === b.source && a.flags === b.flags;
|
|
214
|
+
}
|
|
215
|
+
if (typeof a !== "object" || typeof b !== "object") {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
if (isDomNode(a) && isDomNode(b)) {
|
|
219
|
+
return a.isEqualNode(b);
|
|
220
|
+
}
|
|
221
|
+
let length = aStack.length;
|
|
222
|
+
while (length--) {
|
|
223
|
+
if (aStack[length] === a) {
|
|
224
|
+
return bStack[length] === b;
|
|
225
|
+
} else if (bStack[length] === b) {
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
aStack.push(a);
|
|
230
|
+
bStack.push(b);
|
|
231
|
+
if (className === "[object Array]" && a.length !== b.length) {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
const aKeys = keys(a, hasKey2);
|
|
235
|
+
let key;
|
|
236
|
+
let size = aKeys.length;
|
|
237
|
+
if (keys(b, hasKey2).length !== size) {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
while (size--) {
|
|
241
|
+
key = aKeys[size];
|
|
242
|
+
result = hasKey2(b, key) && eq(a[key], b[key], aStack, bStack, customTesters, hasKey2);
|
|
243
|
+
if (!result) {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
aStack.pop();
|
|
248
|
+
bStack.pop();
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
function keys(obj, hasKey2) {
|
|
252
|
+
const keys2 = [];
|
|
253
|
+
for (const key in obj) {
|
|
254
|
+
if (hasKey2(obj, key)) {
|
|
255
|
+
keys2.push(key);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return keys2.concat(
|
|
259
|
+
Object.getOwnPropertySymbols(obj).filter(
|
|
260
|
+
(symbol) => Object.getOwnPropertyDescriptor(obj, symbol).enumerable
|
|
261
|
+
)
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
function hasDefinedKey(obj, key) {
|
|
265
|
+
return hasKey(obj, key) && obj[key] !== void 0;
|
|
266
|
+
}
|
|
267
|
+
function hasKey(obj, key) {
|
|
268
|
+
return Object.prototype.hasOwnProperty.call(obj, key);
|
|
269
|
+
}
|
|
270
|
+
function isA(typeName, value) {
|
|
271
|
+
return Object.prototype.toString.apply(value) === `[object ${typeName}]`;
|
|
272
|
+
}
|
|
273
|
+
function isDomNode(obj) {
|
|
274
|
+
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";
|
|
275
|
+
}
|
|
276
|
+
const IS_KEYED_SENTINEL = "@@__IMMUTABLE_KEYED__@@";
|
|
277
|
+
const IS_SET_SENTINEL = "@@__IMMUTABLE_SET__@@";
|
|
278
|
+
const IS_LIST_SENTINEL = "@@__IMMUTABLE_LIST__@@";
|
|
279
|
+
const IS_ORDERED_SENTINEL = "@@__IMMUTABLE_ORDERED__@@";
|
|
280
|
+
const IS_RECORD_SYMBOL = "@@__IMMUTABLE_RECORD__@@";
|
|
281
|
+
function isImmutableUnorderedKeyed(maybeKeyed) {
|
|
282
|
+
return !!(maybeKeyed && maybeKeyed[IS_KEYED_SENTINEL] && !maybeKeyed[IS_ORDERED_SENTINEL]);
|
|
283
|
+
}
|
|
284
|
+
function isImmutableUnorderedSet(maybeSet) {
|
|
285
|
+
return !!(maybeSet && maybeSet[IS_SET_SENTINEL] && !maybeSet[IS_ORDERED_SENTINEL]);
|
|
286
|
+
}
|
|
287
|
+
function isObjectLiteral(source) {
|
|
288
|
+
return source != null && typeof source === "object" && !Array.isArray(source);
|
|
289
|
+
}
|
|
290
|
+
function isImmutableList(source) {
|
|
291
|
+
return Boolean(source && isObjectLiteral(source) && source[IS_LIST_SENTINEL]);
|
|
292
|
+
}
|
|
293
|
+
function isImmutableOrderedKeyed(source) {
|
|
294
|
+
return Boolean(
|
|
295
|
+
source && isObjectLiteral(source) && source[IS_KEYED_SENTINEL] && source[IS_ORDERED_SENTINEL]
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
function isImmutableOrderedSet(source) {
|
|
299
|
+
return Boolean(
|
|
300
|
+
source && isObjectLiteral(source) && source[IS_SET_SENTINEL] && source[IS_ORDERED_SENTINEL]
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
function isImmutableRecord(source) {
|
|
304
|
+
return Boolean(source && isObjectLiteral(source) && source[IS_RECORD_SYMBOL]);
|
|
305
|
+
}
|
|
306
|
+
const IteratorSymbol = Symbol.iterator;
|
|
307
|
+
function hasIterator(object) {
|
|
308
|
+
return !!(object != null && object[IteratorSymbol]);
|
|
309
|
+
}
|
|
310
|
+
function iterableEquality(a, b, customTesters = [], aStack = [], bStack = []) {
|
|
311
|
+
if (typeof a !== "object" || typeof b !== "object" || Array.isArray(a) || Array.isArray(b) || !hasIterator(a) || !hasIterator(b)) {
|
|
312
|
+
return void 0;
|
|
313
|
+
}
|
|
314
|
+
if (a.constructor !== b.constructor) {
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
let length = aStack.length;
|
|
318
|
+
while (length--) {
|
|
319
|
+
if (aStack[length] === a) {
|
|
320
|
+
return bStack[length] === b;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
aStack.push(a);
|
|
324
|
+
bStack.push(b);
|
|
325
|
+
const filteredCustomTesters = [
|
|
326
|
+
...customTesters.filter((t) => t !== iterableEquality),
|
|
327
|
+
iterableEqualityWithStack
|
|
328
|
+
];
|
|
329
|
+
function iterableEqualityWithStack(a2, b2) {
|
|
330
|
+
return iterableEquality(a2, b2, [...customTesters], [...aStack], [...bStack]);
|
|
331
|
+
}
|
|
332
|
+
if (a.size !== void 0) {
|
|
333
|
+
if (a.size !== b.size) {
|
|
334
|
+
return false;
|
|
335
|
+
} else if (isA("Set", a) || isImmutableUnorderedSet(a)) {
|
|
336
|
+
let allFound = true;
|
|
337
|
+
for (const aValue of a) {
|
|
338
|
+
if (!b.has(aValue)) {
|
|
339
|
+
let has = false;
|
|
340
|
+
for (const bValue of b) {
|
|
341
|
+
const isEqual = equals(aValue, bValue, filteredCustomTesters);
|
|
342
|
+
if (isEqual === true) {
|
|
343
|
+
has = true;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (has === false) {
|
|
347
|
+
allFound = false;
|
|
348
|
+
break;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
aStack.pop();
|
|
353
|
+
bStack.pop();
|
|
354
|
+
return allFound;
|
|
355
|
+
} else if (isA("Map", a) || isImmutableUnorderedKeyed(a)) {
|
|
356
|
+
let allFound = true;
|
|
357
|
+
for (const aEntry of a) {
|
|
358
|
+
if (!b.has(aEntry[0]) || !equals(aEntry[1], b.get(aEntry[0]), filteredCustomTesters)) {
|
|
359
|
+
let has = false;
|
|
360
|
+
for (const bEntry of b) {
|
|
361
|
+
const matchedKey = equals(
|
|
362
|
+
aEntry[0],
|
|
363
|
+
bEntry[0],
|
|
364
|
+
filteredCustomTesters
|
|
365
|
+
);
|
|
366
|
+
let matchedValue = false;
|
|
367
|
+
if (matchedKey === true) {
|
|
368
|
+
matchedValue = equals(
|
|
369
|
+
aEntry[1],
|
|
370
|
+
bEntry[1],
|
|
371
|
+
filteredCustomTesters
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
if (matchedValue === true) {
|
|
375
|
+
has = true;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
if (has === false) {
|
|
379
|
+
allFound = false;
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
aStack.pop();
|
|
385
|
+
bStack.pop();
|
|
386
|
+
return allFound;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
const bIterator = b[IteratorSymbol]();
|
|
390
|
+
for (const aValue of a) {
|
|
391
|
+
const nextB = bIterator.next();
|
|
392
|
+
if (nextB.done || !equals(aValue, nextB.value, filteredCustomTesters)) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (!bIterator.next().done) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
if (!isImmutableList(a) && !isImmutableOrderedKeyed(a) && !isImmutableOrderedSet(a) && !isImmutableRecord(a)) {
|
|
400
|
+
const aEntries = Object.entries(a);
|
|
401
|
+
const bEntries = Object.entries(b);
|
|
402
|
+
if (!equals(aEntries, bEntries)) {
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
aStack.pop();
|
|
407
|
+
bStack.pop();
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
410
|
+
function hasPropertyInObject(object, key) {
|
|
411
|
+
const shouldTerminate = !object || typeof object !== "object" || object === Object.prototype;
|
|
412
|
+
if (shouldTerminate) {
|
|
413
|
+
return false;
|
|
414
|
+
}
|
|
415
|
+
return Object.prototype.hasOwnProperty.call(object, key) || hasPropertyInObject(Object.getPrototypeOf(object), key);
|
|
416
|
+
}
|
|
417
|
+
function isObjectWithKeys(a) {
|
|
418
|
+
return isObject(a) && !(a instanceof Error) && !Array.isArray(a) && !(a instanceof Date);
|
|
419
|
+
}
|
|
420
|
+
function subsetEquality(object, subset, customTesters = []) {
|
|
421
|
+
const filteredCustomTesters = customTesters.filter(
|
|
422
|
+
(t) => t !== subsetEquality
|
|
423
|
+
);
|
|
424
|
+
const subsetEqualityWithContext = (seenReferences = /* @__PURE__ */ new WeakMap()) => (object2, subset2) => {
|
|
425
|
+
if (!isObjectWithKeys(subset2)) {
|
|
426
|
+
return void 0;
|
|
427
|
+
}
|
|
428
|
+
return Object.keys(subset2).every((key) => {
|
|
429
|
+
if (subset2[key] != null && typeof subset2[key] === "object") {
|
|
430
|
+
if (seenReferences.has(subset2[key])) {
|
|
431
|
+
return equals(object2[key], subset2[key], filteredCustomTesters);
|
|
432
|
+
}
|
|
433
|
+
seenReferences.set(subset2[key], true);
|
|
434
|
+
}
|
|
435
|
+
const result = object2 != null && hasPropertyInObject(object2, key) && equals(object2[key], subset2[key], [
|
|
436
|
+
...filteredCustomTesters,
|
|
437
|
+
subsetEqualityWithContext(seenReferences)
|
|
438
|
+
]);
|
|
439
|
+
seenReferences.delete(subset2[key]);
|
|
440
|
+
return result;
|
|
441
|
+
});
|
|
442
|
+
};
|
|
443
|
+
return subsetEqualityWithContext()(object, subset);
|
|
444
|
+
}
|
|
445
|
+
function typeEquality(a, b) {
|
|
446
|
+
if (a == null || b == null || a.constructor === b.constructor) {
|
|
447
|
+
return void 0;
|
|
448
|
+
}
|
|
449
|
+
return false;
|
|
450
|
+
}
|
|
451
|
+
function arrayBufferEquality(a, b) {
|
|
452
|
+
let dataViewA = a;
|
|
453
|
+
let dataViewB = b;
|
|
454
|
+
if (!(a instanceof DataView && b instanceof DataView)) {
|
|
455
|
+
if (!(a instanceof ArrayBuffer) || !(b instanceof ArrayBuffer)) {
|
|
456
|
+
return void 0;
|
|
457
|
+
}
|
|
458
|
+
try {
|
|
459
|
+
dataViewA = new DataView(a);
|
|
460
|
+
dataViewB = new DataView(b);
|
|
461
|
+
} catch {
|
|
462
|
+
return void 0;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
if (dataViewA.byteLength !== dataViewB.byteLength) {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
for (let i = 0; i < dataViewA.byteLength; i++) {
|
|
469
|
+
if (dataViewA.getUint8(i) !== dataViewB.getUint8(i)) {
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return true;
|
|
474
|
+
}
|
|
475
|
+
function sparseArrayEquality(a, b, customTesters = []) {
|
|
476
|
+
if (!Array.isArray(a) || !Array.isArray(b)) {
|
|
477
|
+
return void 0;
|
|
478
|
+
}
|
|
479
|
+
const aKeys = Object.keys(a);
|
|
480
|
+
const bKeys = Object.keys(b);
|
|
481
|
+
const filteredCustomTesters = customTesters.filter(
|
|
482
|
+
(t) => t !== sparseArrayEquality
|
|
483
|
+
);
|
|
484
|
+
return equals(a, b, filteredCustomTesters, true) && equals(aKeys, bKeys);
|
|
485
|
+
}
|
|
486
|
+
function generateToBeMessage(deepEqualityName, expected = "#{this}", actual = "#{exp}") {
|
|
487
|
+
const toBeMessage = `expected ${expected} to be ${actual} // Object.is equality`;
|
|
488
|
+
if (["toStrictEqual", "toEqual"].includes(deepEqualityName)) {
|
|
489
|
+
return `${toBeMessage}
|
|
490
|
+
|
|
491
|
+
If it should pass with deep equality, replace "toBe" with "${deepEqualityName}"
|
|
492
|
+
|
|
493
|
+
Expected: ${expected}
|
|
494
|
+
Received: serializes to the same string
|
|
495
|
+
`;
|
|
496
|
+
}
|
|
497
|
+
return toBeMessage;
|
|
498
|
+
}
|
|
499
|
+
function pluralize(word, count) {
|
|
500
|
+
return `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
501
|
+
}
|
|
502
|
+
function getObjectKeys(object) {
|
|
503
|
+
return [
|
|
504
|
+
...Object.keys(object),
|
|
505
|
+
...Object.getOwnPropertySymbols(object).filter(
|
|
506
|
+
(s) => {
|
|
507
|
+
var _a;
|
|
508
|
+
return (_a = Object.getOwnPropertyDescriptor(object, s)) == null ? void 0 : _a.enumerable;
|
|
509
|
+
}
|
|
510
|
+
)
|
|
511
|
+
];
|
|
512
|
+
}
|
|
513
|
+
function getObjectSubset(object, subset, customTesters = []) {
|
|
514
|
+
let stripped = 0;
|
|
515
|
+
const getObjectSubsetWithContext = (seenReferences = /* @__PURE__ */ new WeakMap()) => (object2, subset2) => {
|
|
516
|
+
if (Array.isArray(object2)) {
|
|
517
|
+
if (Array.isArray(subset2) && subset2.length === object2.length) {
|
|
518
|
+
return subset2.map(
|
|
519
|
+
(sub, i) => getObjectSubsetWithContext(seenReferences)(object2[i], sub)
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
} else if (object2 instanceof Date) {
|
|
523
|
+
return object2;
|
|
524
|
+
} else if (isObject(object2) && isObject(subset2)) {
|
|
525
|
+
if (equals(object2, subset2, [
|
|
526
|
+
...customTesters,
|
|
527
|
+
iterableEquality,
|
|
528
|
+
subsetEquality
|
|
529
|
+
])) {
|
|
530
|
+
return subset2;
|
|
531
|
+
}
|
|
532
|
+
const trimmed = {};
|
|
533
|
+
seenReferences.set(object2, trimmed);
|
|
534
|
+
for (const key of getObjectKeys(object2)) {
|
|
535
|
+
if (hasPropertyInObject(subset2, key)) {
|
|
536
|
+
trimmed[key] = seenReferences.has(object2[key]) ? seenReferences.get(object2[key]) : getObjectSubsetWithContext(seenReferences)(
|
|
537
|
+
object2[key],
|
|
538
|
+
subset2[key]
|
|
539
|
+
);
|
|
540
|
+
} else {
|
|
541
|
+
if (!seenReferences.has(object2[key])) {
|
|
542
|
+
stripped += 1;
|
|
543
|
+
if (isObject(object2[key])) {
|
|
544
|
+
stripped += getObjectKeys(object2[key]).length;
|
|
545
|
+
}
|
|
546
|
+
getObjectSubsetWithContext(seenReferences)(
|
|
547
|
+
object2[key],
|
|
548
|
+
subset2[key]
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
if (getObjectKeys(trimmed).length > 0) {
|
|
554
|
+
return trimmed;
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
return object2;
|
|
558
|
+
};
|
|
559
|
+
return { subset: getObjectSubsetWithContext()(object, subset), stripped };
|
|
560
|
+
}
|
|
561
|
+
class AsymmetricMatcher {
|
|
562
|
+
constructor(sample, inverse = false) {
|
|
563
|
+
// should have "jest" to be compatible with its ecosystem
|
|
564
|
+
__publicField(this, "$$typeof", Symbol.for("jest.asymmetricMatcher"));
|
|
565
|
+
this.sample = sample;
|
|
566
|
+
this.inverse = inverse;
|
|
567
|
+
}
|
|
568
|
+
getMatcherContext(expect) {
|
|
569
|
+
return {
|
|
570
|
+
...getState(expect || globalThis[GLOBAL_EXPECT]),
|
|
571
|
+
equals,
|
|
572
|
+
isNot: this.inverse,
|
|
573
|
+
customTesters: getCustomEqualityTesters(),
|
|
574
|
+
utils: {
|
|
575
|
+
...getMatcherUtils(),
|
|
576
|
+
diff,
|
|
577
|
+
stringify,
|
|
578
|
+
iterableEquality,
|
|
579
|
+
subsetEquality
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
// implement custom chai/loupe inspect for better AssertionError.message formatting
|
|
584
|
+
// https://github.com/chaijs/loupe/blob/9b8a6deabcd50adc056a64fb705896194710c5c6/src/index.ts#L29
|
|
585
|
+
[Symbol.for("chai/inspect")](options) {
|
|
586
|
+
const result = stringify(this, options.depth, { min: true });
|
|
587
|
+
if (result.length <= options.truncate) {
|
|
588
|
+
return result;
|
|
589
|
+
}
|
|
590
|
+
return `${this.toString()}{…}`;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
class StringContaining extends AsymmetricMatcher {
|
|
594
|
+
constructor(sample, inverse = false) {
|
|
595
|
+
if (!isA("String", sample)) {
|
|
596
|
+
throw new Error("Expected is not a string");
|
|
597
|
+
}
|
|
598
|
+
super(sample, inverse);
|
|
599
|
+
}
|
|
600
|
+
asymmetricMatch(other) {
|
|
601
|
+
const result = isA("String", other) && other.includes(this.sample);
|
|
602
|
+
return this.inverse ? !result : result;
|
|
603
|
+
}
|
|
604
|
+
toString() {
|
|
605
|
+
return `String${this.inverse ? "Not" : ""}Containing`;
|
|
606
|
+
}
|
|
607
|
+
getExpectedType() {
|
|
608
|
+
return "string";
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
class Anything extends AsymmetricMatcher {
|
|
612
|
+
asymmetricMatch(other) {
|
|
613
|
+
return other != null;
|
|
614
|
+
}
|
|
615
|
+
toString() {
|
|
616
|
+
return "Anything";
|
|
617
|
+
}
|
|
618
|
+
toAsymmetricMatcher() {
|
|
619
|
+
return "Anything";
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
class ObjectContaining extends AsymmetricMatcher {
|
|
623
|
+
constructor(sample, inverse = false) {
|
|
624
|
+
super(sample, inverse);
|
|
625
|
+
}
|
|
626
|
+
getPrototype(obj) {
|
|
627
|
+
if (Object.getPrototypeOf) {
|
|
628
|
+
return Object.getPrototypeOf(obj);
|
|
629
|
+
}
|
|
630
|
+
if (obj.constructor.prototype === obj) {
|
|
631
|
+
return null;
|
|
632
|
+
}
|
|
633
|
+
return obj.constructor.prototype;
|
|
634
|
+
}
|
|
635
|
+
hasProperty(obj, property) {
|
|
636
|
+
if (!obj) {
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
if (Object.prototype.hasOwnProperty.call(obj, property)) {
|
|
640
|
+
return true;
|
|
641
|
+
}
|
|
642
|
+
return this.hasProperty(this.getPrototype(obj), property);
|
|
643
|
+
}
|
|
644
|
+
asymmetricMatch(other) {
|
|
645
|
+
if (typeof this.sample !== "object") {
|
|
646
|
+
throw new TypeError(
|
|
647
|
+
`You must provide an object to ${this.toString()}, not '${typeof this.sample}'.`
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
let result = true;
|
|
651
|
+
const matcherContext = this.getMatcherContext();
|
|
652
|
+
for (const property in this.sample) {
|
|
653
|
+
if (!this.hasProperty(other, property) || !equals(
|
|
654
|
+
this.sample[property],
|
|
655
|
+
other[property],
|
|
656
|
+
matcherContext.customTesters
|
|
657
|
+
)) {
|
|
658
|
+
result = false;
|
|
659
|
+
break;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
return this.inverse ? !result : result;
|
|
663
|
+
}
|
|
664
|
+
toString() {
|
|
665
|
+
return `Object${this.inverse ? "Not" : ""}Containing`;
|
|
666
|
+
}
|
|
667
|
+
getExpectedType() {
|
|
668
|
+
return "object";
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
class ArrayContaining extends AsymmetricMatcher {
|
|
672
|
+
constructor(sample, inverse = false) {
|
|
673
|
+
super(sample, inverse);
|
|
674
|
+
}
|
|
675
|
+
asymmetricMatch(other) {
|
|
676
|
+
if (!Array.isArray(this.sample)) {
|
|
677
|
+
throw new TypeError(
|
|
678
|
+
`You must provide an array to ${this.toString()}, not '${typeof this.sample}'.`
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
const matcherContext = this.getMatcherContext();
|
|
682
|
+
const result = this.sample.length === 0 || Array.isArray(other) && this.sample.every(
|
|
683
|
+
(item) => other.some(
|
|
684
|
+
(another) => equals(item, another, matcherContext.customTesters)
|
|
685
|
+
)
|
|
686
|
+
);
|
|
687
|
+
return this.inverse ? !result : result;
|
|
688
|
+
}
|
|
689
|
+
toString() {
|
|
690
|
+
return `Array${this.inverse ? "Not" : ""}Containing`;
|
|
691
|
+
}
|
|
692
|
+
getExpectedType() {
|
|
693
|
+
return "array";
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
class Any extends AsymmetricMatcher {
|
|
697
|
+
constructor(sample) {
|
|
698
|
+
if (typeof sample === "undefined") {
|
|
699
|
+
throw new TypeError(
|
|
700
|
+
"any() expects to be passed a constructor function. Please pass one or use anything() to match any object."
|
|
701
|
+
);
|
|
702
|
+
}
|
|
703
|
+
super(sample);
|
|
704
|
+
}
|
|
705
|
+
fnNameFor(func) {
|
|
706
|
+
if (func.name) {
|
|
707
|
+
return func.name;
|
|
708
|
+
}
|
|
709
|
+
const functionToString = Function.prototype.toString;
|
|
710
|
+
const matches = functionToString.call(func).match(/^(?:async)?\s*function\s*(?:\*\s*)?([\w$]+)\s*\(/);
|
|
711
|
+
return matches ? matches[1] : "<anonymous>";
|
|
712
|
+
}
|
|
713
|
+
asymmetricMatch(other) {
|
|
714
|
+
if (this.sample === String) {
|
|
715
|
+
return typeof other == "string" || other instanceof String;
|
|
716
|
+
}
|
|
717
|
+
if (this.sample === Number) {
|
|
718
|
+
return typeof other == "number" || other instanceof Number;
|
|
719
|
+
}
|
|
720
|
+
if (this.sample === Function) {
|
|
721
|
+
return typeof other == "function" || other instanceof Function;
|
|
722
|
+
}
|
|
723
|
+
if (this.sample === Boolean) {
|
|
724
|
+
return typeof other == "boolean" || other instanceof Boolean;
|
|
725
|
+
}
|
|
726
|
+
if (this.sample === BigInt) {
|
|
727
|
+
return typeof other == "bigint" || other instanceof BigInt;
|
|
728
|
+
}
|
|
729
|
+
if (this.sample === Symbol) {
|
|
730
|
+
return typeof other == "symbol" || other instanceof Symbol;
|
|
731
|
+
}
|
|
732
|
+
if (this.sample === Object) {
|
|
733
|
+
return typeof other == "object";
|
|
734
|
+
}
|
|
735
|
+
return other instanceof this.sample;
|
|
736
|
+
}
|
|
737
|
+
toString() {
|
|
738
|
+
return "Any";
|
|
739
|
+
}
|
|
740
|
+
getExpectedType() {
|
|
741
|
+
if (this.sample === String) {
|
|
742
|
+
return "string";
|
|
743
|
+
}
|
|
744
|
+
if (this.sample === Number) {
|
|
745
|
+
return "number";
|
|
746
|
+
}
|
|
747
|
+
if (this.sample === Function) {
|
|
748
|
+
return "function";
|
|
749
|
+
}
|
|
750
|
+
if (this.sample === Object) {
|
|
751
|
+
return "object";
|
|
752
|
+
}
|
|
753
|
+
if (this.sample === Boolean) {
|
|
754
|
+
return "boolean";
|
|
755
|
+
}
|
|
756
|
+
return this.fnNameFor(this.sample);
|
|
757
|
+
}
|
|
758
|
+
toAsymmetricMatcher() {
|
|
759
|
+
return `Any<${this.fnNameFor(this.sample)}>`;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
class StringMatching extends AsymmetricMatcher {
|
|
763
|
+
constructor(sample, inverse = false) {
|
|
764
|
+
if (!isA("String", sample) && !isA("RegExp", sample)) {
|
|
765
|
+
throw new Error("Expected is not a String or a RegExp");
|
|
766
|
+
}
|
|
767
|
+
super(new RegExp(sample), inverse);
|
|
768
|
+
}
|
|
769
|
+
asymmetricMatch(other) {
|
|
770
|
+
const result = isA("String", other) && this.sample.test(other);
|
|
771
|
+
return this.inverse ? !result : result;
|
|
772
|
+
}
|
|
773
|
+
toString() {
|
|
774
|
+
return `String${this.inverse ? "Not" : ""}Matching`;
|
|
775
|
+
}
|
|
776
|
+
getExpectedType() {
|
|
777
|
+
return "string";
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
class CloseTo extends AsymmetricMatcher {
|
|
781
|
+
constructor(sample, precision = 2, inverse = false) {
|
|
782
|
+
if (!isA("Number", sample)) {
|
|
783
|
+
throw new Error("Expected is not a Number");
|
|
784
|
+
}
|
|
785
|
+
if (!isA("Number", precision)) {
|
|
786
|
+
throw new Error("Precision is not a Number");
|
|
787
|
+
}
|
|
788
|
+
super(sample);
|
|
789
|
+
__publicField(this, "precision");
|
|
790
|
+
this.inverse = inverse;
|
|
791
|
+
this.precision = precision;
|
|
792
|
+
}
|
|
793
|
+
asymmetricMatch(other) {
|
|
794
|
+
if (!isA("Number", other)) {
|
|
795
|
+
return false;
|
|
796
|
+
}
|
|
797
|
+
let result = false;
|
|
798
|
+
if (other === Number.POSITIVE_INFINITY && this.sample === Number.POSITIVE_INFINITY) {
|
|
799
|
+
result = true;
|
|
800
|
+
} else if (other === Number.NEGATIVE_INFINITY && this.sample === Number.NEGATIVE_INFINITY) {
|
|
801
|
+
result = true;
|
|
802
|
+
} else {
|
|
803
|
+
result = Math.abs(this.sample - other) < 10 ** -this.precision / 2;
|
|
804
|
+
}
|
|
805
|
+
return this.inverse ? !result : result;
|
|
806
|
+
}
|
|
807
|
+
toString() {
|
|
808
|
+
return `Number${this.inverse ? "Not" : ""}CloseTo`;
|
|
809
|
+
}
|
|
810
|
+
getExpectedType() {
|
|
811
|
+
return "number";
|
|
812
|
+
}
|
|
813
|
+
toAsymmetricMatcher() {
|
|
814
|
+
return [
|
|
815
|
+
this.toString(),
|
|
816
|
+
this.sample,
|
|
817
|
+
`(${pluralize("digit", this.precision)})`
|
|
818
|
+
].join(" ");
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
const JestAsymmetricMatchers = (chai, utils) => {
|
|
822
|
+
utils.addMethod(chai.expect, "anything", () => new Anything());
|
|
823
|
+
utils.addMethod(chai.expect, "any", (expected) => new Any(expected));
|
|
824
|
+
utils.addMethod(
|
|
825
|
+
chai.expect,
|
|
826
|
+
"stringContaining",
|
|
827
|
+
(expected) => new StringContaining(expected)
|
|
828
|
+
);
|
|
829
|
+
utils.addMethod(
|
|
830
|
+
chai.expect,
|
|
831
|
+
"objectContaining",
|
|
832
|
+
(expected) => new ObjectContaining(expected)
|
|
833
|
+
);
|
|
834
|
+
utils.addMethod(
|
|
835
|
+
chai.expect,
|
|
836
|
+
"arrayContaining",
|
|
837
|
+
(expected) => new ArrayContaining(expected)
|
|
838
|
+
);
|
|
839
|
+
utils.addMethod(
|
|
840
|
+
chai.expect,
|
|
841
|
+
"stringMatching",
|
|
842
|
+
(expected) => new StringMatching(expected)
|
|
843
|
+
);
|
|
844
|
+
utils.addMethod(
|
|
845
|
+
chai.expect,
|
|
846
|
+
"closeTo",
|
|
847
|
+
(expected, precision) => new CloseTo(expected, precision)
|
|
848
|
+
);
|
|
849
|
+
chai.expect.not = {
|
|
850
|
+
stringContaining: (expected) => new StringContaining(expected, true),
|
|
851
|
+
objectContaining: (expected) => new ObjectContaining(expected, true),
|
|
852
|
+
arrayContaining: (expected) => new ArrayContaining(expected, true),
|
|
853
|
+
stringMatching: (expected) => new StringMatching(expected, true),
|
|
854
|
+
closeTo: (expected, precision) => new CloseTo(expected, precision, true)
|
|
855
|
+
};
|
|
856
|
+
};
|
|
857
|
+
function recordAsyncExpect(test, promise) {
|
|
858
|
+
if (test && promise instanceof Promise) {
|
|
859
|
+
promise = promise.finally(() => {
|
|
860
|
+
const index = test.promises.indexOf(promise);
|
|
861
|
+
if (index !== -1) {
|
|
862
|
+
test.promises.splice(index, 1);
|
|
863
|
+
}
|
|
864
|
+
});
|
|
865
|
+
if (!test.promises) {
|
|
866
|
+
test.promises = [];
|
|
867
|
+
}
|
|
868
|
+
test.promises.push(promise);
|
|
869
|
+
}
|
|
870
|
+
return promise;
|
|
871
|
+
}
|
|
872
|
+
function wrapSoft(utils, fn) {
|
|
873
|
+
return function(...args) {
|
|
874
|
+
var _a;
|
|
875
|
+
if (!utils.flag(this, "soft")) {
|
|
876
|
+
return fn.apply(this, args);
|
|
877
|
+
}
|
|
878
|
+
const test = utils.flag(this, "vitest-test");
|
|
879
|
+
if (!test) {
|
|
880
|
+
throw new Error("expect.soft() can only be used inside a test");
|
|
881
|
+
}
|
|
882
|
+
try {
|
|
883
|
+
return fn.apply(this, args);
|
|
884
|
+
} catch (err) {
|
|
885
|
+
test.result || (test.result = { state: "fail" });
|
|
886
|
+
test.result.state = "fail";
|
|
887
|
+
(_a = test.result).errors || (_a.errors = []);
|
|
888
|
+
test.result.errors.push(processError(err));
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
const JestChaiExpect = (chai, utils) => {
|
|
893
|
+
const { AssertionError } = chai;
|
|
894
|
+
const customTesters = getCustomEqualityTesters();
|
|
895
|
+
function def(name, fn) {
|
|
896
|
+
const addMethod = (n) => {
|
|
897
|
+
const softWrapper = wrapSoft(utils, fn);
|
|
898
|
+
utils.addMethod(chai.Assertion.prototype, n, softWrapper);
|
|
899
|
+
utils.addMethod(
|
|
900
|
+
globalThis[JEST_MATCHERS_OBJECT].matchers,
|
|
901
|
+
n,
|
|
902
|
+
softWrapper
|
|
903
|
+
);
|
|
904
|
+
};
|
|
905
|
+
if (Array.isArray(name)) {
|
|
906
|
+
name.forEach((n) => addMethod(n));
|
|
907
|
+
} else {
|
|
908
|
+
addMethod(name);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
["throw", "throws", "Throw"].forEach((m) => {
|
|
912
|
+
utils.overwriteMethod(chai.Assertion.prototype, m, (_super) => {
|
|
913
|
+
return function(...args) {
|
|
914
|
+
const promise = utils.flag(this, "promise");
|
|
915
|
+
const object = utils.flag(this, "object");
|
|
916
|
+
const isNot = utils.flag(this, "negate");
|
|
917
|
+
if (promise === "rejects") {
|
|
918
|
+
utils.flag(this, "object", () => {
|
|
919
|
+
throw object;
|
|
920
|
+
});
|
|
921
|
+
} else if (promise === "resolves" && typeof object !== "function") {
|
|
922
|
+
if (!isNot) {
|
|
923
|
+
const message = utils.flag(this, "message") || "expected promise to throw an error, but it didn't";
|
|
924
|
+
const error = {
|
|
925
|
+
showDiff: false
|
|
926
|
+
};
|
|
927
|
+
throw new AssertionError(message, error, utils.flag(this, "ssfi"));
|
|
928
|
+
} else {
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
_super.apply(this, args);
|
|
933
|
+
};
|
|
934
|
+
});
|
|
935
|
+
});
|
|
936
|
+
def("withTest", function(test) {
|
|
937
|
+
utils.flag(this, "vitest-test", test);
|
|
938
|
+
return this;
|
|
939
|
+
});
|
|
940
|
+
def("toEqual", function(expected) {
|
|
941
|
+
const actual = utils.flag(this, "object");
|
|
942
|
+
const equal = equals(actual, expected, [
|
|
943
|
+
...customTesters,
|
|
944
|
+
iterableEquality
|
|
945
|
+
]);
|
|
946
|
+
return this.assert(
|
|
947
|
+
equal,
|
|
948
|
+
"expected #{this} to deeply equal #{exp}",
|
|
949
|
+
"expected #{this} to not deeply equal #{exp}",
|
|
950
|
+
expected,
|
|
951
|
+
actual
|
|
952
|
+
);
|
|
953
|
+
});
|
|
954
|
+
def("toStrictEqual", function(expected) {
|
|
955
|
+
const obj = utils.flag(this, "object");
|
|
956
|
+
const equal = equals(
|
|
957
|
+
obj,
|
|
958
|
+
expected,
|
|
959
|
+
[
|
|
960
|
+
...customTesters,
|
|
961
|
+
iterableEquality,
|
|
962
|
+
typeEquality,
|
|
963
|
+
sparseArrayEquality,
|
|
964
|
+
arrayBufferEquality
|
|
965
|
+
],
|
|
966
|
+
true
|
|
967
|
+
);
|
|
968
|
+
return this.assert(
|
|
969
|
+
equal,
|
|
970
|
+
"expected #{this} to strictly equal #{exp}",
|
|
971
|
+
"expected #{this} to not strictly equal #{exp}",
|
|
972
|
+
expected,
|
|
973
|
+
obj
|
|
974
|
+
);
|
|
975
|
+
});
|
|
976
|
+
def("toBe", function(expected) {
|
|
977
|
+
const actual = this._obj;
|
|
978
|
+
const pass = Object.is(actual, expected);
|
|
979
|
+
let deepEqualityName = "";
|
|
980
|
+
if (!pass) {
|
|
981
|
+
const toStrictEqualPass = equals(
|
|
982
|
+
actual,
|
|
983
|
+
expected,
|
|
984
|
+
[
|
|
985
|
+
...customTesters,
|
|
986
|
+
iterableEquality,
|
|
987
|
+
typeEquality,
|
|
988
|
+
sparseArrayEquality,
|
|
989
|
+
arrayBufferEquality
|
|
990
|
+
],
|
|
991
|
+
true
|
|
992
|
+
);
|
|
993
|
+
if (toStrictEqualPass) {
|
|
994
|
+
deepEqualityName = "toStrictEqual";
|
|
995
|
+
} else {
|
|
996
|
+
const toEqualPass = equals(actual, expected, [
|
|
997
|
+
...customTesters,
|
|
998
|
+
iterableEquality
|
|
999
|
+
]);
|
|
1000
|
+
if (toEqualPass) {
|
|
1001
|
+
deepEqualityName = "toEqual";
|
|
1002
|
+
}
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
return this.assert(
|
|
1006
|
+
pass,
|
|
1007
|
+
generateToBeMessage(deepEqualityName),
|
|
1008
|
+
"expected #{this} not to be #{exp} // Object.is equality",
|
|
1009
|
+
expected,
|
|
1010
|
+
actual
|
|
1011
|
+
);
|
|
1012
|
+
});
|
|
1013
|
+
def("toMatchObject", function(expected) {
|
|
1014
|
+
const actual = this._obj;
|
|
1015
|
+
const pass = equals(actual, expected, [
|
|
1016
|
+
...customTesters,
|
|
1017
|
+
iterableEquality,
|
|
1018
|
+
subsetEquality
|
|
1019
|
+
]);
|
|
1020
|
+
const isNot = utils.flag(this, "negate");
|
|
1021
|
+
const { subset: actualSubset, stripped } = getObjectSubset(
|
|
1022
|
+
actual,
|
|
1023
|
+
expected
|
|
1024
|
+
);
|
|
1025
|
+
if (pass && isNot || !pass && !isNot) {
|
|
1026
|
+
const msg = utils.getMessage(this, [
|
|
1027
|
+
pass,
|
|
1028
|
+
"expected #{this} to match object #{exp}",
|
|
1029
|
+
"expected #{this} to not match object #{exp}",
|
|
1030
|
+
expected,
|
|
1031
|
+
actualSubset,
|
|
1032
|
+
false
|
|
1033
|
+
]);
|
|
1034
|
+
const message = stripped === 0 ? msg : `${msg}
|
|
1035
|
+
(${stripped} matching ${stripped === 1 ? "property" : "properties"} omitted from actual)`;
|
|
1036
|
+
throw new AssertionError(message, {
|
|
1037
|
+
showDiff: true,
|
|
1038
|
+
expected,
|
|
1039
|
+
actual: actualSubset
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
});
|
|
1043
|
+
def("toMatch", function(expected) {
|
|
1044
|
+
const actual = this._obj;
|
|
1045
|
+
if (typeof actual !== "string") {
|
|
1046
|
+
throw new TypeError(
|
|
1047
|
+
`.toMatch() expects to receive a string, but got ${typeof actual}`
|
|
1048
|
+
);
|
|
1049
|
+
}
|
|
1050
|
+
return this.assert(
|
|
1051
|
+
typeof expected === "string" ? actual.includes(expected) : actual.match(expected),
|
|
1052
|
+
`expected #{this} to match #{exp}`,
|
|
1053
|
+
`expected #{this} not to match #{exp}`,
|
|
1054
|
+
expected,
|
|
1055
|
+
actual
|
|
1056
|
+
);
|
|
1057
|
+
});
|
|
1058
|
+
def("toContain", function(item) {
|
|
1059
|
+
const actual = this._obj;
|
|
1060
|
+
if (typeof Node !== "undefined" && actual instanceof Node) {
|
|
1061
|
+
if (!(item instanceof Node)) {
|
|
1062
|
+
throw new TypeError(
|
|
1063
|
+
`toContain() expected a DOM node as the argument, but got ${typeof item}`
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
return this.assert(
|
|
1067
|
+
actual.contains(item),
|
|
1068
|
+
"expected #{this} to contain element #{exp}",
|
|
1069
|
+
"expected #{this} not to contain element #{exp}",
|
|
1070
|
+
item,
|
|
1071
|
+
actual
|
|
1072
|
+
);
|
|
1073
|
+
}
|
|
1074
|
+
if (typeof DOMTokenList !== "undefined" && actual instanceof DOMTokenList) {
|
|
1075
|
+
assertTypes(item, "class name", ["string"]);
|
|
1076
|
+
const isNot = utils.flag(this, "negate");
|
|
1077
|
+
const expectedClassList = isNot ? actual.value.replace(item, "").trim() : `${actual.value} ${item}`;
|
|
1078
|
+
return this.assert(
|
|
1079
|
+
actual.contains(item),
|
|
1080
|
+
`expected "${actual.value}" to contain "${item}"`,
|
|
1081
|
+
`expected "${actual.value}" not to contain "${item}"`,
|
|
1082
|
+
expectedClassList,
|
|
1083
|
+
actual.value
|
|
1084
|
+
);
|
|
1085
|
+
}
|
|
1086
|
+
if (typeof actual === "string" && typeof item === "string") {
|
|
1087
|
+
return this.assert(
|
|
1088
|
+
actual.includes(item),
|
|
1089
|
+
`expected #{this} to contain #{exp}`,
|
|
1090
|
+
`expected #{this} not to contain #{exp}`,
|
|
1091
|
+
item,
|
|
1092
|
+
actual
|
|
1093
|
+
);
|
|
1094
|
+
}
|
|
1095
|
+
if (actual != null && typeof actual !== "string") {
|
|
1096
|
+
utils.flag(this, "object", Array.from(actual));
|
|
1097
|
+
}
|
|
1098
|
+
return this.contain(item);
|
|
1099
|
+
});
|
|
1100
|
+
def("toContainEqual", function(expected) {
|
|
1101
|
+
const obj = utils.flag(this, "object");
|
|
1102
|
+
const index = Array.from(obj).findIndex((item) => {
|
|
1103
|
+
return equals(item, expected, customTesters);
|
|
1104
|
+
});
|
|
1105
|
+
this.assert(
|
|
1106
|
+
index !== -1,
|
|
1107
|
+
"expected #{this} to deep equally contain #{exp}",
|
|
1108
|
+
"expected #{this} to not deep equally contain #{exp}",
|
|
1109
|
+
expected
|
|
1110
|
+
);
|
|
1111
|
+
});
|
|
1112
|
+
def("toBeTruthy", function() {
|
|
1113
|
+
const obj = utils.flag(this, "object");
|
|
1114
|
+
this.assert(
|
|
1115
|
+
Boolean(obj),
|
|
1116
|
+
"expected #{this} to be truthy",
|
|
1117
|
+
"expected #{this} to not be truthy",
|
|
1118
|
+
obj,
|
|
1119
|
+
false
|
|
1120
|
+
);
|
|
1121
|
+
});
|
|
1122
|
+
def("toBeFalsy", function() {
|
|
1123
|
+
const obj = utils.flag(this, "object");
|
|
1124
|
+
this.assert(
|
|
1125
|
+
!obj,
|
|
1126
|
+
"expected #{this} to be falsy",
|
|
1127
|
+
"expected #{this} to not be falsy",
|
|
1128
|
+
obj,
|
|
1129
|
+
false
|
|
1130
|
+
);
|
|
1131
|
+
});
|
|
1132
|
+
def("toBeGreaterThan", function(expected) {
|
|
1133
|
+
const actual = this._obj;
|
|
1134
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1135
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1136
|
+
return this.assert(
|
|
1137
|
+
actual > expected,
|
|
1138
|
+
`expected ${actual} to be greater than ${expected}`,
|
|
1139
|
+
`expected ${actual} to be not greater than ${expected}`,
|
|
1140
|
+
actual,
|
|
1141
|
+
expected,
|
|
1142
|
+
false
|
|
1143
|
+
);
|
|
1144
|
+
});
|
|
1145
|
+
def("toBeGreaterThanOrEqual", function(expected) {
|
|
1146
|
+
const actual = this._obj;
|
|
1147
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1148
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1149
|
+
return this.assert(
|
|
1150
|
+
actual >= expected,
|
|
1151
|
+
`expected ${actual} to be greater than or equal to ${expected}`,
|
|
1152
|
+
`expected ${actual} to be not greater than or equal to ${expected}`,
|
|
1153
|
+
actual,
|
|
1154
|
+
expected,
|
|
1155
|
+
false
|
|
1156
|
+
);
|
|
1157
|
+
});
|
|
1158
|
+
def("toBeLessThan", function(expected) {
|
|
1159
|
+
const actual = this._obj;
|
|
1160
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1161
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1162
|
+
return this.assert(
|
|
1163
|
+
actual < expected,
|
|
1164
|
+
`expected ${actual} to be less than ${expected}`,
|
|
1165
|
+
`expected ${actual} to be not less than ${expected}`,
|
|
1166
|
+
actual,
|
|
1167
|
+
expected,
|
|
1168
|
+
false
|
|
1169
|
+
);
|
|
1170
|
+
});
|
|
1171
|
+
def("toBeLessThanOrEqual", function(expected) {
|
|
1172
|
+
const actual = this._obj;
|
|
1173
|
+
assertTypes(actual, "actual", ["number", "bigint"]);
|
|
1174
|
+
assertTypes(expected, "expected", ["number", "bigint"]);
|
|
1175
|
+
return this.assert(
|
|
1176
|
+
actual <= expected,
|
|
1177
|
+
`expected ${actual} to be less than or equal to ${expected}`,
|
|
1178
|
+
`expected ${actual} to be not less than or equal to ${expected}`,
|
|
1179
|
+
actual,
|
|
1180
|
+
expected,
|
|
1181
|
+
false
|
|
1182
|
+
);
|
|
1183
|
+
});
|
|
1184
|
+
def("toBeNaN", function() {
|
|
1185
|
+
return this.be.NaN;
|
|
1186
|
+
});
|
|
1187
|
+
def("toBeUndefined", function() {
|
|
1188
|
+
return this.be.undefined;
|
|
1189
|
+
});
|
|
1190
|
+
def("toBeNull", function() {
|
|
1191
|
+
return this.be.null;
|
|
1192
|
+
});
|
|
1193
|
+
def("toBeDefined", function() {
|
|
1194
|
+
const negate = utils.flag(this, "negate");
|
|
1195
|
+
utils.flag(this, "negate", false);
|
|
1196
|
+
if (negate) {
|
|
1197
|
+
return this.be.undefined;
|
|
1198
|
+
}
|
|
1199
|
+
return this.not.be.undefined;
|
|
1200
|
+
});
|
|
1201
|
+
def(
|
|
1202
|
+
"toBeTypeOf",
|
|
1203
|
+
function(expected) {
|
|
1204
|
+
const actual = typeof this._obj;
|
|
1205
|
+
const equal = expected === actual;
|
|
1206
|
+
return this.assert(
|
|
1207
|
+
equal,
|
|
1208
|
+
"expected #{this} to be type of #{exp}",
|
|
1209
|
+
"expected #{this} not to be type of #{exp}",
|
|
1210
|
+
expected,
|
|
1211
|
+
actual
|
|
1212
|
+
);
|
|
1213
|
+
}
|
|
1214
|
+
);
|
|
1215
|
+
def("toBeInstanceOf", function(obj) {
|
|
1216
|
+
return this.instanceOf(obj);
|
|
1217
|
+
});
|
|
1218
|
+
def("toHaveLength", function(length) {
|
|
1219
|
+
return this.have.length(length);
|
|
1220
|
+
});
|
|
1221
|
+
def(
|
|
1222
|
+
"toHaveProperty",
|
|
1223
|
+
function(...args) {
|
|
1224
|
+
if (Array.isArray(args[0])) {
|
|
1225
|
+
args[0] = args[0].map((key) => String(key).replace(/([.[\]])/g, "\\$1")).join(".");
|
|
1226
|
+
}
|
|
1227
|
+
const actual = this._obj;
|
|
1228
|
+
const [propertyName, expected] = args;
|
|
1229
|
+
const getValue = () => {
|
|
1230
|
+
const hasOwn = Object.prototype.hasOwnProperty.call(
|
|
1231
|
+
actual,
|
|
1232
|
+
propertyName
|
|
1233
|
+
);
|
|
1234
|
+
if (hasOwn) {
|
|
1235
|
+
return { value: actual[propertyName], exists: true };
|
|
1236
|
+
}
|
|
1237
|
+
return utils.getPathInfo(actual, propertyName);
|
|
1238
|
+
};
|
|
1239
|
+
const { value, exists } = getValue();
|
|
1240
|
+
const pass = exists && (args.length === 1 || equals(expected, value, customTesters));
|
|
1241
|
+
const valueString = args.length === 1 ? "" : ` with value ${utils.objDisplay(expected)}`;
|
|
1242
|
+
return this.assert(
|
|
1243
|
+
pass,
|
|
1244
|
+
`expected #{this} to have property "${propertyName}"${valueString}`,
|
|
1245
|
+
`expected #{this} to not have property "${propertyName}"${valueString}`,
|
|
1246
|
+
expected,
|
|
1247
|
+
exists ? value : void 0
|
|
1248
|
+
);
|
|
1249
|
+
}
|
|
1250
|
+
);
|
|
1251
|
+
def("toBeCloseTo", function(received, precision = 2) {
|
|
1252
|
+
const expected = this._obj;
|
|
1253
|
+
let pass = false;
|
|
1254
|
+
let expectedDiff = 0;
|
|
1255
|
+
let receivedDiff = 0;
|
|
1256
|
+
if (received === Number.POSITIVE_INFINITY && expected === Number.POSITIVE_INFINITY) {
|
|
1257
|
+
pass = true;
|
|
1258
|
+
} else if (received === Number.NEGATIVE_INFINITY && expected === Number.NEGATIVE_INFINITY) {
|
|
1259
|
+
pass = true;
|
|
1260
|
+
} else {
|
|
1261
|
+
expectedDiff = 10 ** -precision / 2;
|
|
1262
|
+
receivedDiff = Math.abs(expected - received);
|
|
1263
|
+
pass = receivedDiff < expectedDiff;
|
|
1264
|
+
}
|
|
1265
|
+
return this.assert(
|
|
1266
|
+
pass,
|
|
1267
|
+
`expected #{this} to be close to #{exp}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
|
|
1268
|
+
`expected #{this} to not be close to #{exp}, received difference is ${receivedDiff}, but expected ${expectedDiff}`,
|
|
1269
|
+
received,
|
|
1270
|
+
expected,
|
|
1271
|
+
false
|
|
1272
|
+
);
|
|
1273
|
+
});
|
|
1274
|
+
function assertIsMock(assertion) {
|
|
1275
|
+
if (!isMockFunction(assertion._obj)) {
|
|
1276
|
+
throw new TypeError(
|
|
1277
|
+
`${utils.inspect(assertion._obj)} is not a spy or a call to a spy!`
|
|
1278
|
+
);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
function getSpy(assertion) {
|
|
1282
|
+
assertIsMock(assertion);
|
|
1283
|
+
return assertion._obj;
|
|
1284
|
+
}
|
|
1285
|
+
def(["toHaveBeenCalledTimes", "toBeCalledTimes"], function(number) {
|
|
1286
|
+
const spy = getSpy(this);
|
|
1287
|
+
const spyName = spy.getMockName();
|
|
1288
|
+
const callCount = spy.mock.calls.length;
|
|
1289
|
+
return this.assert(
|
|
1290
|
+
callCount === number,
|
|
1291
|
+
`expected "${spyName}" to be called #{exp} times, but got ${callCount} times`,
|
|
1292
|
+
`expected "${spyName}" to not be called #{exp} times`,
|
|
1293
|
+
number,
|
|
1294
|
+
callCount,
|
|
1295
|
+
false
|
|
1296
|
+
);
|
|
1297
|
+
});
|
|
1298
|
+
def("toHaveBeenCalledOnce", function() {
|
|
1299
|
+
const spy = getSpy(this);
|
|
1300
|
+
const spyName = spy.getMockName();
|
|
1301
|
+
const callCount = spy.mock.calls.length;
|
|
1302
|
+
return this.assert(
|
|
1303
|
+
callCount === 1,
|
|
1304
|
+
`expected "${spyName}" to be called once, but got ${callCount} times`,
|
|
1305
|
+
`expected "${spyName}" to not be called once`,
|
|
1306
|
+
1,
|
|
1307
|
+
callCount,
|
|
1308
|
+
false
|
|
1309
|
+
);
|
|
1310
|
+
});
|
|
1311
|
+
def(["toHaveBeenCalled", "toBeCalled"], function() {
|
|
1312
|
+
const spy = getSpy(this);
|
|
1313
|
+
const spyName = spy.getMockName();
|
|
1314
|
+
const callCount = spy.mock.calls.length;
|
|
1315
|
+
const called = callCount > 0;
|
|
1316
|
+
const isNot = utils.flag(this, "negate");
|
|
1317
|
+
let msg = utils.getMessage(this, [
|
|
1318
|
+
called,
|
|
1319
|
+
`expected "${spyName}" to be called at least once`,
|
|
1320
|
+
`expected "${spyName}" to not be called at all, but actually been called ${callCount} times`,
|
|
1321
|
+
true,
|
|
1322
|
+
called
|
|
1323
|
+
]);
|
|
1324
|
+
if (called && isNot) {
|
|
1325
|
+
msg = formatCalls(spy, msg);
|
|
1326
|
+
}
|
|
1327
|
+
if (called && isNot || !called && !isNot) {
|
|
1328
|
+
throw new AssertionError(msg);
|
|
1329
|
+
}
|
|
1330
|
+
});
|
|
1331
|
+
def(["toHaveBeenCalledWith", "toBeCalledWith"], function(...args) {
|
|
1332
|
+
const spy = getSpy(this);
|
|
1333
|
+
const spyName = spy.getMockName();
|
|
1334
|
+
const pass = spy.mock.calls.some(
|
|
1335
|
+
(callArg) => equals(callArg, args, [...customTesters, iterableEquality])
|
|
1336
|
+
);
|
|
1337
|
+
const isNot = utils.flag(this, "negate");
|
|
1338
|
+
const msg = utils.getMessage(this, [
|
|
1339
|
+
pass,
|
|
1340
|
+
`expected "${spyName}" to be called with arguments: #{exp}`,
|
|
1341
|
+
`expected "${spyName}" to not be called with arguments: #{exp}`,
|
|
1342
|
+
args
|
|
1343
|
+
]);
|
|
1344
|
+
if (pass && isNot || !pass && !isNot) {
|
|
1345
|
+
throw new AssertionError(formatCalls(spy, msg, args));
|
|
1346
|
+
}
|
|
1347
|
+
});
|
|
1348
|
+
def(
|
|
1349
|
+
["toHaveBeenNthCalledWith", "nthCalledWith"],
|
|
1350
|
+
function(times, ...args) {
|
|
1351
|
+
const spy = getSpy(this);
|
|
1352
|
+
const spyName = spy.getMockName();
|
|
1353
|
+
const nthCall = spy.mock.calls[times - 1];
|
|
1354
|
+
const callCount = spy.mock.calls.length;
|
|
1355
|
+
const isCalled = times <= callCount;
|
|
1356
|
+
this.assert(
|
|
1357
|
+
equals(nthCall, args, [...customTesters, iterableEquality]),
|
|
1358
|
+
`expected ${ordinalOf(
|
|
1359
|
+
times
|
|
1360
|
+
)} "${spyName}" call to have been called with #{exp}${isCalled ? `` : `, but called only ${callCount} times`}`,
|
|
1361
|
+
`expected ${ordinalOf(
|
|
1362
|
+
times
|
|
1363
|
+
)} "${spyName}" call to not have been called with #{exp}`,
|
|
1364
|
+
args,
|
|
1365
|
+
nthCall,
|
|
1366
|
+
isCalled
|
|
1367
|
+
);
|
|
1368
|
+
}
|
|
1369
|
+
);
|
|
1370
|
+
def(
|
|
1371
|
+
["toHaveBeenLastCalledWith", "lastCalledWith"],
|
|
1372
|
+
function(...args) {
|
|
1373
|
+
const spy = getSpy(this);
|
|
1374
|
+
const spyName = spy.getMockName();
|
|
1375
|
+
const lastCall = spy.mock.calls[spy.mock.calls.length - 1];
|
|
1376
|
+
this.assert(
|
|
1377
|
+
equals(lastCall, args, [...customTesters, iterableEquality]),
|
|
1378
|
+
`expected last "${spyName}" call to have been called with #{exp}`,
|
|
1379
|
+
`expected last "${spyName}" call to not have been called with #{exp}`,
|
|
1380
|
+
args,
|
|
1381
|
+
lastCall
|
|
1382
|
+
);
|
|
1383
|
+
}
|
|
1384
|
+
);
|
|
1385
|
+
def(
|
|
1386
|
+
["toThrow", "toThrowError"],
|
|
1387
|
+
function(expected) {
|
|
1388
|
+
if (typeof expected === "string" || typeof expected === "undefined" || expected instanceof RegExp) {
|
|
1389
|
+
return this.throws(expected);
|
|
1390
|
+
}
|
|
1391
|
+
const obj = this._obj;
|
|
1392
|
+
const promise = utils.flag(this, "promise");
|
|
1393
|
+
const isNot = utils.flag(this, "negate");
|
|
1394
|
+
let thrown = null;
|
|
1395
|
+
if (promise === "rejects") {
|
|
1396
|
+
thrown = obj;
|
|
1397
|
+
} else if (promise === "resolves" && typeof obj !== "function") {
|
|
1398
|
+
if (!isNot) {
|
|
1399
|
+
const message = utils.flag(this, "message") || "expected promise to throw an error, but it didn't";
|
|
1400
|
+
const error = {
|
|
1401
|
+
showDiff: false
|
|
1402
|
+
};
|
|
1403
|
+
throw new AssertionError(message, error, utils.flag(this, "ssfi"));
|
|
1404
|
+
} else {
|
|
1405
|
+
return;
|
|
1406
|
+
}
|
|
1407
|
+
} else {
|
|
1408
|
+
let isThrow = false;
|
|
1409
|
+
try {
|
|
1410
|
+
obj();
|
|
1411
|
+
} catch (err) {
|
|
1412
|
+
isThrow = true;
|
|
1413
|
+
thrown = err;
|
|
1414
|
+
}
|
|
1415
|
+
if (!isThrow && !isNot) {
|
|
1416
|
+
const message = utils.flag(this, "message") || "expected function to throw an error, but it didn't";
|
|
1417
|
+
const error = {
|
|
1418
|
+
showDiff: false
|
|
1419
|
+
};
|
|
1420
|
+
throw new AssertionError(message, error, utils.flag(this, "ssfi"));
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
if (typeof expected === "function") {
|
|
1424
|
+
const name = expected.name || expected.prototype.constructor.name;
|
|
1425
|
+
return this.assert(
|
|
1426
|
+
thrown && thrown instanceof expected,
|
|
1427
|
+
`expected error to be instance of ${name}`,
|
|
1428
|
+
`expected error not to be instance of ${name}`,
|
|
1429
|
+
expected,
|
|
1430
|
+
thrown
|
|
1431
|
+
);
|
|
1432
|
+
}
|
|
1433
|
+
if (expected instanceof Error) {
|
|
1434
|
+
return this.assert(
|
|
1435
|
+
thrown && expected.message === thrown.message,
|
|
1436
|
+
`expected error to have message: ${expected.message}`,
|
|
1437
|
+
`expected error not to have message: ${expected.message}`,
|
|
1438
|
+
expected.message,
|
|
1439
|
+
thrown && thrown.message
|
|
1440
|
+
);
|
|
1441
|
+
}
|
|
1442
|
+
if (typeof expected === "object" && "asymmetricMatch" in expected && typeof expected.asymmetricMatch === "function") {
|
|
1443
|
+
const matcher = expected;
|
|
1444
|
+
return this.assert(
|
|
1445
|
+
thrown && matcher.asymmetricMatch(thrown),
|
|
1446
|
+
"expected error to match asymmetric matcher",
|
|
1447
|
+
"expected error not to match asymmetric matcher",
|
|
1448
|
+
matcher,
|
|
1449
|
+
thrown
|
|
1450
|
+
);
|
|
1451
|
+
}
|
|
1452
|
+
throw new Error(
|
|
1453
|
+
`"toThrow" expects string, RegExp, function, Error instance or asymmetric matcher, got "${typeof expected}"`
|
|
1454
|
+
);
|
|
1455
|
+
}
|
|
1456
|
+
);
|
|
1457
|
+
[
|
|
1458
|
+
{
|
|
1459
|
+
name: "toHaveResolved",
|
|
1460
|
+
condition: (spy) => spy.mock.settledResults.length > 0 && spy.mock.settledResults.some(({ type }) => type === "fulfilled"),
|
|
1461
|
+
action: "resolved"
|
|
1462
|
+
},
|
|
1463
|
+
{
|
|
1464
|
+
name: ["toHaveReturned", "toReturn"],
|
|
1465
|
+
condition: (spy) => spy.mock.calls.length > 0 && spy.mock.results.some(({ type }) => type !== "throw"),
|
|
1466
|
+
action: "called"
|
|
1467
|
+
}
|
|
1468
|
+
].forEach(({ name, condition, action }) => {
|
|
1469
|
+
def(name, function() {
|
|
1470
|
+
const spy = getSpy(this);
|
|
1471
|
+
const spyName = spy.getMockName();
|
|
1472
|
+
const pass = condition(spy);
|
|
1473
|
+
this.assert(
|
|
1474
|
+
pass,
|
|
1475
|
+
`expected "${spyName}" to be successfully ${action} at least once`,
|
|
1476
|
+
`expected "${spyName}" to not be successfully ${action}`,
|
|
1477
|
+
pass,
|
|
1478
|
+
!pass,
|
|
1479
|
+
false
|
|
1480
|
+
);
|
|
1481
|
+
});
|
|
1482
|
+
});
|
|
1483
|
+
[
|
|
1484
|
+
{
|
|
1485
|
+
name: "toHaveResolvedTimes",
|
|
1486
|
+
condition: (spy, times) => spy.mock.settledResults.reduce(
|
|
1487
|
+
(s, { type }) => type === "fulfilled" ? ++s : s,
|
|
1488
|
+
0
|
|
1489
|
+
) === times,
|
|
1490
|
+
action: "resolved"
|
|
1491
|
+
},
|
|
1492
|
+
{
|
|
1493
|
+
name: ["toHaveReturnedTimes", "toReturnTimes"],
|
|
1494
|
+
condition: (spy, times) => spy.mock.results.reduce(
|
|
1495
|
+
(s, { type }) => type === "throw" ? s : ++s,
|
|
1496
|
+
0
|
|
1497
|
+
) === times,
|
|
1498
|
+
action: "called"
|
|
1499
|
+
}
|
|
1500
|
+
].forEach(({ name, condition, action }) => {
|
|
1501
|
+
def(name, function(times) {
|
|
1502
|
+
const spy = getSpy(this);
|
|
1503
|
+
const spyName = spy.getMockName();
|
|
1504
|
+
const pass = condition(spy, times);
|
|
1505
|
+
this.assert(
|
|
1506
|
+
pass,
|
|
1507
|
+
`expected "${spyName}" to be successfully ${action} ${times} times`,
|
|
1508
|
+
`expected "${spyName}" to not be successfully ${action} ${times} times`,
|
|
1509
|
+
`expected resolved times: ${times}`,
|
|
1510
|
+
`received resolved times: ${pass}`,
|
|
1511
|
+
false
|
|
1512
|
+
);
|
|
1513
|
+
});
|
|
1514
|
+
});
|
|
1515
|
+
[
|
|
1516
|
+
{
|
|
1517
|
+
name: "toHaveResolvedWith",
|
|
1518
|
+
condition: (spy, value) => spy.mock.settledResults.some(
|
|
1519
|
+
({ type, value: result }) => type === "fulfilled" && equals(value, result)
|
|
1520
|
+
),
|
|
1521
|
+
action: "resolve"
|
|
1522
|
+
},
|
|
1523
|
+
{
|
|
1524
|
+
name: ["toHaveReturnedWith", "toReturnWith"],
|
|
1525
|
+
condition: (spy, value) => spy.mock.results.some(
|
|
1526
|
+
({ type, value: result }) => type === "return" && equals(value, result)
|
|
1527
|
+
),
|
|
1528
|
+
action: "return"
|
|
1529
|
+
}
|
|
1530
|
+
].forEach(({ name, condition, action }) => {
|
|
1531
|
+
def(name, function(value) {
|
|
1532
|
+
const spy = getSpy(this);
|
|
1533
|
+
const pass = condition(spy, value);
|
|
1534
|
+
const isNot = utils.flag(this, "negate");
|
|
1535
|
+
if (pass && isNot || !pass && !isNot) {
|
|
1536
|
+
const spyName = spy.getMockName();
|
|
1537
|
+
const msg = utils.getMessage(this, [
|
|
1538
|
+
pass,
|
|
1539
|
+
`expected "${spyName}" to ${action} with: #{exp} at least once`,
|
|
1540
|
+
`expected "${spyName}" to not ${action} with: #{exp}`,
|
|
1541
|
+
value
|
|
1542
|
+
]);
|
|
1543
|
+
const results = action === "return" ? spy.mock.results : spy.mock.settledResults;
|
|
1544
|
+
throw new AssertionError(formatReturns(spy, results, msg, value));
|
|
1545
|
+
}
|
|
1546
|
+
});
|
|
1547
|
+
});
|
|
1548
|
+
[
|
|
1549
|
+
{
|
|
1550
|
+
name: "toHaveLastResolvedWith",
|
|
1551
|
+
condition: (spy, value) => {
|
|
1552
|
+
const result = spy.mock.settledResults[spy.mock.settledResults.length - 1];
|
|
1553
|
+
return result && result.type === "fulfilled" && equals(result.value, value);
|
|
1554
|
+
},
|
|
1555
|
+
action: "resolve"
|
|
1556
|
+
},
|
|
1557
|
+
{
|
|
1558
|
+
name: ["toHaveLastReturnedWith", "lastReturnedWith"],
|
|
1559
|
+
condition: (spy, value) => {
|
|
1560
|
+
const result = spy.mock.results[spy.mock.results.length - 1];
|
|
1561
|
+
return result && result.type === "return" && equals(result.value, value);
|
|
1562
|
+
},
|
|
1563
|
+
action: "return"
|
|
1564
|
+
}
|
|
1565
|
+
].forEach(({ name, condition, action }) => {
|
|
1566
|
+
def(name, function(value) {
|
|
1567
|
+
const spy = getSpy(this);
|
|
1568
|
+
const results = action === "return" ? spy.mock.results : spy.mock.settledResults;
|
|
1569
|
+
const result = results[results.length - 1];
|
|
1570
|
+
const spyName = spy.getMockName();
|
|
1571
|
+
this.assert(
|
|
1572
|
+
condition(spy, value),
|
|
1573
|
+
`expected last "${spyName}" call to ${action} #{exp}`,
|
|
1574
|
+
`expected last "${spyName}" call to not ${action} #{exp}`,
|
|
1575
|
+
value,
|
|
1576
|
+
result == null ? void 0 : result.value
|
|
1577
|
+
);
|
|
1578
|
+
});
|
|
1579
|
+
});
|
|
1580
|
+
[
|
|
1581
|
+
{
|
|
1582
|
+
name: "toHaveNthResolvedWith",
|
|
1583
|
+
condition: (spy, index, value) => {
|
|
1584
|
+
const result = spy.mock.settledResults[index - 1];
|
|
1585
|
+
return result && result.type === "fulfilled" && equals(result.value, value);
|
|
1586
|
+
},
|
|
1587
|
+
action: "resolve"
|
|
1588
|
+
},
|
|
1589
|
+
{
|
|
1590
|
+
name: ["toHaveNthReturnedWith", "nthReturnedWith"],
|
|
1591
|
+
condition: (spy, index, value) => {
|
|
1592
|
+
const result = spy.mock.results[index - 1];
|
|
1593
|
+
return result && result.type === "return" && equals(result.value, value);
|
|
1594
|
+
},
|
|
1595
|
+
action: "return"
|
|
1596
|
+
}
|
|
1597
|
+
].forEach(({ name, condition, action }) => {
|
|
1598
|
+
def(name, function(nthCall, value) {
|
|
1599
|
+
const spy = getSpy(this);
|
|
1600
|
+
const spyName = spy.getMockName();
|
|
1601
|
+
const results = action === "return" ? spy.mock.results : spy.mock.settledResults;
|
|
1602
|
+
const result = results[nthCall - 1];
|
|
1603
|
+
const ordinalCall = `${ordinalOf(nthCall)} call`;
|
|
1604
|
+
this.assert(
|
|
1605
|
+
condition(spy, nthCall, value),
|
|
1606
|
+
`expected ${ordinalCall} "${spyName}" call to ${action} #{exp}`,
|
|
1607
|
+
`expected ${ordinalCall} "${spyName}" call to not ${action} #{exp}`,
|
|
1608
|
+
value,
|
|
1609
|
+
result == null ? void 0 : result.value
|
|
1610
|
+
);
|
|
1611
|
+
});
|
|
1612
|
+
});
|
|
1613
|
+
def("toSatisfy", function(matcher, message) {
|
|
1614
|
+
return this.be.satisfy(matcher, message);
|
|
1615
|
+
});
|
|
1616
|
+
def("withContext", function(context) {
|
|
1617
|
+
for (const key in context) {
|
|
1618
|
+
utils.flag(this, key, context[key]);
|
|
1619
|
+
}
|
|
1620
|
+
return this;
|
|
1621
|
+
});
|
|
1622
|
+
utils.addProperty(
|
|
1623
|
+
chai.Assertion.prototype,
|
|
1624
|
+
"resolves",
|
|
1625
|
+
function __VITEST_RESOLVES__() {
|
|
1626
|
+
const error = new Error("resolves");
|
|
1627
|
+
utils.flag(this, "promise", "resolves");
|
|
1628
|
+
utils.flag(this, "error", error);
|
|
1629
|
+
const test = utils.flag(this, "vitest-test");
|
|
1630
|
+
const obj = utils.flag(this, "object");
|
|
1631
|
+
if (utils.flag(this, "poll")) {
|
|
1632
|
+
throw new SyntaxError(
|
|
1633
|
+
`expect.poll() is not supported in combination with .resolves`
|
|
1634
|
+
);
|
|
1635
|
+
}
|
|
1636
|
+
if (typeof (obj == null ? void 0 : obj.then) !== "function") {
|
|
1637
|
+
throw new TypeError(
|
|
1638
|
+
`You must provide a Promise to expect() when using .resolves, not '${typeof obj}'.`
|
|
1639
|
+
);
|
|
1640
|
+
}
|
|
1641
|
+
const proxy = new Proxy(this, {
|
|
1642
|
+
get: (target, key, receiver) => {
|
|
1643
|
+
const result = Reflect.get(target, key, receiver);
|
|
1644
|
+
if (typeof result !== "function") {
|
|
1645
|
+
return result instanceof chai.Assertion ? proxy : result;
|
|
1646
|
+
}
|
|
1647
|
+
return (...args) => {
|
|
1648
|
+
const promise = obj.then(
|
|
1649
|
+
(value) => {
|
|
1650
|
+
utils.flag(this, "object", value);
|
|
1651
|
+
return result.call(this, ...args);
|
|
1652
|
+
},
|
|
1653
|
+
(err) => {
|
|
1654
|
+
const _error = new AssertionError(
|
|
1655
|
+
`promise rejected "${utils.inspect(
|
|
1656
|
+
err
|
|
1657
|
+
)}" instead of resolving`,
|
|
1658
|
+
{ showDiff: false }
|
|
1659
|
+
);
|
|
1660
|
+
_error.cause = err;
|
|
1661
|
+
_error.stack = error.stack.replace(
|
|
1662
|
+
error.message,
|
|
1663
|
+
_error.message
|
|
1664
|
+
);
|
|
1665
|
+
throw _error;
|
|
1666
|
+
}
|
|
1667
|
+
);
|
|
1668
|
+
return recordAsyncExpect(test, promise);
|
|
1669
|
+
};
|
|
1670
|
+
}
|
|
1671
|
+
});
|
|
1672
|
+
return proxy;
|
|
1673
|
+
}
|
|
1674
|
+
);
|
|
1675
|
+
utils.addProperty(
|
|
1676
|
+
chai.Assertion.prototype,
|
|
1677
|
+
"rejects",
|
|
1678
|
+
function __VITEST_REJECTS__() {
|
|
1679
|
+
const error = new Error("rejects");
|
|
1680
|
+
utils.flag(this, "promise", "rejects");
|
|
1681
|
+
utils.flag(this, "error", error);
|
|
1682
|
+
const test = utils.flag(this, "vitest-test");
|
|
1683
|
+
const obj = utils.flag(this, "object");
|
|
1684
|
+
const wrapper = typeof obj === "function" ? obj() : obj;
|
|
1685
|
+
if (utils.flag(this, "poll")) {
|
|
1686
|
+
throw new SyntaxError(
|
|
1687
|
+
`expect.poll() is not supported in combination with .rejects`
|
|
1688
|
+
);
|
|
1689
|
+
}
|
|
1690
|
+
if (typeof (wrapper == null ? void 0 : wrapper.then) !== "function") {
|
|
1691
|
+
throw new TypeError(
|
|
1692
|
+
`You must provide a Promise to expect() when using .rejects, not '${typeof wrapper}'.`
|
|
1693
|
+
);
|
|
1694
|
+
}
|
|
1695
|
+
const proxy = new Proxy(this, {
|
|
1696
|
+
get: (target, key, receiver) => {
|
|
1697
|
+
const result = Reflect.get(target, key, receiver);
|
|
1698
|
+
if (typeof result !== "function") {
|
|
1699
|
+
return result instanceof chai.Assertion ? proxy : result;
|
|
1700
|
+
}
|
|
1701
|
+
return (...args) => {
|
|
1702
|
+
const promise = wrapper.then(
|
|
1703
|
+
(value) => {
|
|
1704
|
+
const _error = new AssertionError(
|
|
1705
|
+
`promise resolved "${utils.inspect(
|
|
1706
|
+
value
|
|
1707
|
+
)}" instead of rejecting`,
|
|
1708
|
+
{
|
|
1709
|
+
showDiff: true,
|
|
1710
|
+
expected: new Error("rejected promise"),
|
|
1711
|
+
actual: value
|
|
1712
|
+
}
|
|
1713
|
+
);
|
|
1714
|
+
_error.stack = error.stack.replace(
|
|
1715
|
+
error.message,
|
|
1716
|
+
_error.message
|
|
1717
|
+
);
|
|
1718
|
+
throw _error;
|
|
1719
|
+
},
|
|
1720
|
+
(err) => {
|
|
1721
|
+
utils.flag(this, "object", err);
|
|
1722
|
+
return result.call(this, ...args);
|
|
1723
|
+
}
|
|
1724
|
+
);
|
|
1725
|
+
return recordAsyncExpect(test, promise);
|
|
1726
|
+
};
|
|
1727
|
+
}
|
|
1728
|
+
});
|
|
1729
|
+
return proxy;
|
|
1730
|
+
}
|
|
1731
|
+
);
|
|
1732
|
+
};
|
|
1733
|
+
function ordinalOf(i) {
|
|
1734
|
+
const j = i % 10;
|
|
1735
|
+
const k = i % 100;
|
|
1736
|
+
if (j === 1 && k !== 11) {
|
|
1737
|
+
return `${i}st`;
|
|
1738
|
+
}
|
|
1739
|
+
if (j === 2 && k !== 12) {
|
|
1740
|
+
return `${i}nd`;
|
|
1741
|
+
}
|
|
1742
|
+
if (j === 3 && k !== 13) {
|
|
1743
|
+
return `${i}rd`;
|
|
1744
|
+
}
|
|
1745
|
+
return `${i}th`;
|
|
1746
|
+
}
|
|
1747
|
+
function formatCalls(spy, msg, showActualCall) {
|
|
1748
|
+
if (spy.mock.calls) {
|
|
1749
|
+
msg += f.gray(
|
|
1750
|
+
`
|
|
1751
|
+
|
|
1752
|
+
Received:
|
|
1753
|
+
|
|
1754
|
+
${spy.mock.calls.map((callArg, i) => {
|
|
1755
|
+
let methodCall = f.bold(
|
|
1756
|
+
` ${ordinalOf(i + 1)} ${spy.getMockName()} call:
|
|
1757
|
+
|
|
1758
|
+
`
|
|
1759
|
+
);
|
|
1760
|
+
if (showActualCall) {
|
|
1761
|
+
methodCall += diff(showActualCall, callArg, {
|
|
1762
|
+
omitAnnotationLines: true
|
|
1763
|
+
});
|
|
1764
|
+
} else {
|
|
1765
|
+
methodCall += stringify(callArg).split("\n").map((line) => ` ${line}`).join("\n");
|
|
1766
|
+
}
|
|
1767
|
+
methodCall += "\n";
|
|
1768
|
+
return methodCall;
|
|
1769
|
+
}).join("\n")}`
|
|
1770
|
+
);
|
|
1771
|
+
}
|
|
1772
|
+
msg += f.gray(
|
|
1773
|
+
`
|
|
1774
|
+
|
|
1775
|
+
Number of calls: ${f.bold(spy.mock.calls.length)}
|
|
1776
|
+
`
|
|
1777
|
+
);
|
|
1778
|
+
return msg;
|
|
1779
|
+
}
|
|
1780
|
+
function formatReturns(spy, results, msg, showActualReturn) {
|
|
1781
|
+
msg += f.gray(
|
|
1782
|
+
`
|
|
1783
|
+
|
|
1784
|
+
Received:
|
|
1785
|
+
|
|
1786
|
+
${results.map((callReturn, i) => {
|
|
1787
|
+
let methodCall = f.bold(
|
|
1788
|
+
` ${ordinalOf(i + 1)} ${spy.getMockName()} call return:
|
|
1789
|
+
|
|
1790
|
+
`
|
|
1791
|
+
);
|
|
1792
|
+
if (showActualReturn) {
|
|
1793
|
+
methodCall += diff(showActualReturn, callReturn.value, {
|
|
1794
|
+
omitAnnotationLines: true
|
|
1795
|
+
});
|
|
1796
|
+
} else {
|
|
1797
|
+
methodCall += stringify(callReturn).split("\n").map((line) => ` ${line}`).join("\n");
|
|
1798
|
+
}
|
|
1799
|
+
methodCall += "\n";
|
|
1800
|
+
return methodCall;
|
|
1801
|
+
}).join("\n")}`
|
|
1802
|
+
);
|
|
1803
|
+
msg += f.gray(
|
|
1804
|
+
`
|
|
1805
|
+
|
|
1806
|
+
Number of calls: ${f.bold(spy.mock.calls.length)}
|
|
1807
|
+
`
|
|
1808
|
+
);
|
|
1809
|
+
return msg;
|
|
1810
|
+
}
|
|
1811
|
+
function getMatcherState(assertion, expect) {
|
|
1812
|
+
const obj = assertion._obj;
|
|
1813
|
+
const isNot = utils_exports.flag(assertion, "negate");
|
|
1814
|
+
const promise = utils_exports.flag(assertion, "promise") || "";
|
|
1815
|
+
const jestUtils = {
|
|
1816
|
+
...getMatcherUtils(),
|
|
1817
|
+
diff,
|
|
1818
|
+
stringify,
|
|
1819
|
+
iterableEquality,
|
|
1820
|
+
subsetEquality
|
|
1821
|
+
};
|
|
1822
|
+
const matcherState = {
|
|
1823
|
+
...getState(expect),
|
|
1824
|
+
customTesters: getCustomEqualityTesters(),
|
|
1825
|
+
isNot,
|
|
1826
|
+
utils: jestUtils,
|
|
1827
|
+
promise,
|
|
1828
|
+
equals,
|
|
1829
|
+
// needed for built-in jest-snapshots, but we don't use it
|
|
1830
|
+
suppressedErrors: [],
|
|
1831
|
+
soft: utils_exports.flag(assertion, "soft"),
|
|
1832
|
+
poll: utils_exports.flag(assertion, "poll")
|
|
1833
|
+
};
|
|
1834
|
+
return {
|
|
1835
|
+
state: matcherState,
|
|
1836
|
+
isNot,
|
|
1837
|
+
obj
|
|
1838
|
+
};
|
|
1839
|
+
}
|
|
1840
|
+
class JestExtendError extends Error {
|
|
1841
|
+
constructor(message, actual, expected) {
|
|
1842
|
+
super(message);
|
|
1843
|
+
this.actual = actual;
|
|
1844
|
+
this.expected = expected;
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
function JestExtendPlugin(c, expect, matchers) {
|
|
1848
|
+
return (_, utils) => {
|
|
1849
|
+
Object.entries(matchers).forEach(
|
|
1850
|
+
([expectAssertionName, expectAssertion]) => {
|
|
1851
|
+
function expectWrapper(...args) {
|
|
1852
|
+
const { state, isNot, obj } = getMatcherState(this, expect);
|
|
1853
|
+
const result = expectAssertion.call(state, obj, ...args);
|
|
1854
|
+
if (result && typeof result === "object" && result instanceof Promise) {
|
|
1855
|
+
return result.then(({ pass: pass2, message: message2, actual: actual2, expected: expected2 }) => {
|
|
1856
|
+
if (pass2 && isNot || !pass2 && !isNot) {
|
|
1857
|
+
throw new JestExtendError(message2(), actual2, expected2);
|
|
1858
|
+
}
|
|
1859
|
+
});
|
|
1860
|
+
}
|
|
1861
|
+
const { pass, message, actual, expected } = result;
|
|
1862
|
+
if (pass && isNot || !pass && !isNot) {
|
|
1863
|
+
throw new JestExtendError(message(), actual, expected);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
const softWrapper = wrapSoft(utils, expectWrapper);
|
|
1867
|
+
utils.addMethod(
|
|
1868
|
+
globalThis[JEST_MATCHERS_OBJECT].matchers,
|
|
1869
|
+
expectAssertionName,
|
|
1870
|
+
softWrapper
|
|
1871
|
+
);
|
|
1872
|
+
utils.addMethod(
|
|
1873
|
+
c.Assertion.prototype,
|
|
1874
|
+
expectAssertionName,
|
|
1875
|
+
softWrapper
|
|
1876
|
+
);
|
|
1877
|
+
class CustomMatcher extends AsymmetricMatcher {
|
|
1878
|
+
constructor(inverse = false, ...sample) {
|
|
1879
|
+
super(sample, inverse);
|
|
1880
|
+
}
|
|
1881
|
+
asymmetricMatch(other) {
|
|
1882
|
+
const { pass } = expectAssertion.call(
|
|
1883
|
+
this.getMatcherContext(expect),
|
|
1884
|
+
other,
|
|
1885
|
+
...this.sample
|
|
1886
|
+
);
|
|
1887
|
+
return this.inverse ? !pass : pass;
|
|
1888
|
+
}
|
|
1889
|
+
toString() {
|
|
1890
|
+
return `${this.inverse ? "not." : ""}${expectAssertionName}`;
|
|
1891
|
+
}
|
|
1892
|
+
getExpectedType() {
|
|
1893
|
+
return "any";
|
|
1894
|
+
}
|
|
1895
|
+
toAsymmetricMatcher() {
|
|
1896
|
+
return `${this.toString()}<${this.sample.map(String).join(", ")}>`;
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
const customMatcher = (...sample) => new CustomMatcher(false, ...sample);
|
|
1900
|
+
Object.defineProperty(expect, expectAssertionName, {
|
|
1901
|
+
configurable: true,
|
|
1902
|
+
enumerable: true,
|
|
1903
|
+
value: customMatcher,
|
|
1904
|
+
writable: true
|
|
1905
|
+
});
|
|
1906
|
+
Object.defineProperty(expect.not, expectAssertionName, {
|
|
1907
|
+
configurable: true,
|
|
1908
|
+
enumerable: true,
|
|
1909
|
+
value: (...sample) => new CustomMatcher(true, ...sample),
|
|
1910
|
+
writable: true
|
|
1911
|
+
});
|
|
1912
|
+
Object.defineProperty(
|
|
1913
|
+
globalThis[ASYMMETRIC_MATCHERS_OBJECT],
|
|
1914
|
+
expectAssertionName,
|
|
1915
|
+
{
|
|
1916
|
+
configurable: true,
|
|
1917
|
+
enumerable: true,
|
|
1918
|
+
value: customMatcher,
|
|
1919
|
+
writable: true
|
|
1920
|
+
}
|
|
1921
|
+
);
|
|
1922
|
+
}
|
|
1923
|
+
);
|
|
1924
|
+
};
|
|
1925
|
+
}
|
|
1926
|
+
const JestExtend = (chai, utils) => {
|
|
1927
|
+
utils.addMethod(
|
|
1928
|
+
chai.expect,
|
|
1929
|
+
"extend",
|
|
1930
|
+
(expect, expects) => {
|
|
1931
|
+
use(JestExtendPlugin(chai, expect, expects));
|
|
1932
|
+
}
|
|
1933
|
+
);
|
|
1934
|
+
};
|
|
1935
|
+
export {
|
|
1936
|
+
ASYMMETRIC_MATCHERS_OBJECT,
|
|
1937
|
+
Any,
|
|
1938
|
+
Anything,
|
|
1939
|
+
ArrayContaining,
|
|
1940
|
+
AsymmetricMatcher,
|
|
1941
|
+
GLOBAL_EXPECT,
|
|
1942
|
+
JEST_MATCHERS_OBJECT,
|
|
1943
|
+
JestAsymmetricMatchers,
|
|
1944
|
+
JestChaiExpect,
|
|
1945
|
+
JestExtend,
|
|
1946
|
+
MATCHERS_OBJECT,
|
|
1947
|
+
ObjectContaining,
|
|
1948
|
+
StringContaining,
|
|
1949
|
+
StringMatching,
|
|
1950
|
+
addCustomEqualityTesters,
|
|
1951
|
+
arrayBufferEquality,
|
|
1952
|
+
equals,
|
|
1953
|
+
generateToBeMessage,
|
|
1954
|
+
getObjectKeys,
|
|
1955
|
+
getObjectSubset,
|
|
1956
|
+
getState,
|
|
1957
|
+
isA,
|
|
1958
|
+
isAsymmetric,
|
|
1959
|
+
isImmutableUnorderedKeyed,
|
|
1960
|
+
isImmutableUnorderedSet,
|
|
1961
|
+
iterableEquality,
|
|
1962
|
+
pluralize,
|
|
1963
|
+
setState,
|
|
1964
|
+
sparseArrayEquality,
|
|
1965
|
+
subsetEquality,
|
|
1966
|
+
typeEquality
|
|
1967
|
+
};
|