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