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