vitest-native 0.14.0-rc.9 → 0.14.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 +76 -60
  2. package/dist/cli.d.mts +1 -3
  3. package/dist/cli.mjs +175 -63
  4. package/dist/context.d.mts +45 -0
  5. package/dist/context.mjs +243 -0
  6. package/dist/errors.d.mts +3 -0
  7. package/dist/helpers.cjs +53 -4
  8. package/dist/helpers.d.cts +1 -3
  9. package/dist/helpers.d.mts +1 -3
  10. package/dist/helpers.mjs +2 -3
  11. package/dist/host-5o48Ujzs.mjs +939 -0
  12. package/dist/{index-CzQazgp9.d.cts → index-DqU4YO3D.d.mts} +27 -49
  13. package/dist/index.d.mts +1 -3
  14. package/dist/index.mjs +463 -595
  15. package/dist/jest-compat/error-equality.mjs +127 -0
  16. package/dist/jest-compat/jest-object.mjs +149 -4
  17. package/dist/jest-compat/node-registry.mjs +233 -58
  18. package/dist/jest-compat/setup.mjs +28 -4
  19. package/dist/jest-compat.cjs +227 -52
  20. package/dist/jest-compat.d.cts +5 -6
  21. package/dist/jest-compat.d.mts +5 -6
  22. package/dist/jest-compat.mjs +204 -52
  23. package/dist/matchers.cjs +1 -2
  24. package/dist/matchers.d.cts +0 -2
  25. package/dist/matchers.d.mts +0 -2
  26. package/dist/matchers.mjs +1 -2
  27. package/dist/{memory-FYbP0_gg.mjs → memory-Couw0Esx.mjs} +31 -6
  28. package/dist/native/assets.d.mts +2 -0
  29. package/dist/native/assets.mjs +34 -0
  30. package/dist/native/babel-toolchain.mjs +190 -0
  31. package/dist/native/babel-worker.mjs +47 -0
  32. package/dist/native/boundary.mjs +23 -8
  33. package/dist/native/compile-cache.mjs +21 -9
  34. package/dist/native/coverage-v8.mjs +287 -0
  35. package/dist/native/esm-interop.mjs +216 -0
  36. package/dist/native/expo-native-modules.mjs +7 -1
  37. package/dist/native/globals.mjs +104 -6
  38. package/dist/native/hooks.mjs +271 -60
  39. package/dist/native/loader.mjs +378 -20
  40. package/dist/native/match.d.mts +24 -2
  41. package/dist/native/match.mjs +117 -12
  42. package/dist/native/metro-profile-compiler.mjs +2 -31
  43. package/dist/native/module-reset.mjs +37 -5
  44. package/dist/native/native-modules.d.mts +2 -3
  45. package/dist/native/native-modules.mjs +22 -21
  46. package/dist/native/ownership.d.mts +10 -0
  47. package/dist/native/ownership.mjs +96 -9
  48. package/dist/native/registry-compiler.mjs +6 -0
  49. package/dist/native/registry.mjs +138 -119
  50. package/dist/native/resident-health.mjs +133 -0
  51. package/dist/native/resolve.d.mts +6 -0
  52. package/dist/native/resolve.mjs +100 -9
  53. package/dist/native/rntl-hooks.mjs +157 -6
  54. package/dist/native/rntl-text-host.mjs +64 -0
  55. package/dist/native/runner.mjs +73 -0
  56. package/dist/native/setup.mjs +96 -70
  57. package/dist/native/state-manifest.mjs +14 -8
  58. package/dist/native/transform.d.mts +2 -0
  59. package/dist/native/transform.mjs +476 -59
  60. package/dist/native/worker.mjs +65 -36
  61. package/dist/{pool-DjpOjOf3.mjs → pool-CFSpNfyw.mjs} +37 -11
  62. package/dist/{preset-map-BlBriTA0.mjs → preset-map-CogjYmSw.mjs} +4 -19
  63. package/dist/preset-modules.mjs +2 -11
  64. package/dist/{presets--zrEWQC2.mjs → presets-CXDyg51l.mjs} +65 -82
  65. package/dist/presets.d.mts +1 -1
  66. package/dist/presets.mjs +1 -1
  67. package/dist/report-error.d.mts +20 -0
  68. package/dist/report-error.mjs +145 -0
  69. package/dist/rntl-support.d.mts +12 -0
  70. package/dist/rntl-support.mjs +189 -0
  71. package/dist/{rolldown-runtime-BhDjJH2R.mjs → rolldown-runtime-D70C1uOS.mjs} +1 -2
  72. package/dist/serializer.cjs +1 -2
  73. package/dist/serializer.d.cts +1 -3
  74. package/dist/serializer.d.mts +1 -3
  75. package/dist/serializer.mjs +1 -2
  76. package/dist/setup.mjs +95 -156
  77. package/dist/{worker-vitest-BGPo0ek6.mjs → worker-vitest-CILPFeRX.mjs} +2 -3
  78. package/docs/migrating-from-jest.md +76 -12
  79. package/docs/release-readiness.md +1 -1
  80. package/package.json +119 -102
  81. package/CHANGELOG.md +0 -2491
  82. package/dist/cli.cjs +0 -1951
  83. package/dist/cli.d.cts +0 -3
  84. package/dist/ecosystem-D6e1OVOg.mjs +0 -1019
  85. package/dist/ecosystem-MFwkbnaC.cjs +0 -1099
  86. package/dist/errors-B3lJK46O.cjs +0 -75
  87. package/dist/errors-CxoBUfVJ.mjs +0 -64
  88. package/dist/index-xw43lBlv.d.mts +0 -481
  89. package/dist/memory-6EPHrJA1.cjs +0 -144
  90. package/dist/pool-B4yesGhI.cjs +0 -121
  91. package/dist/preset-map-jSFsNy1Q.cjs +0 -241
  92. package/dist/preset-modules-BoLkaeEN.cjs +0 -53
  93. package/dist/preset-modules-CG8-G82P.mjs +0 -36
  94. package/dist/presets-9f83H0WW.cjs +0 -3939
  95. package/dist/rolldown-runtime-C0BPl7ul.cjs +0 -43
  96. package/dist/worker-vitest-BfiQB8ND.cjs +0 -62
  97. package/docs/hot-default-design.md +0 -216
@@ -0,0 +1,127 @@
1
+ // Equality of two Errors that carry methods as own properties.
2
+ //
3
+ // Vitest compares two Errors by their toString tag, name, message, cause (when the
4
+ // expected one has one), AggregateError `errors`, and every own enumerable property
5
+ // (`isErrorEqual` in vitest 5.0.5 dist/chunks/index.aTZaPVhf.js:1344, which spreads
6
+ // `{ ...a }` against `{ ...b }`). An error class that assigns methods as per-instance
7
+ // fields — zod 3's ZodError does, `addIssue = (sub) => …` and `addIssues = …` in its
8
+ // constructor — gives every instance its own closures. Two closures are never equal, so
9
+ // two identical ZodErrors are never equal: `toEqual`, `rejects.toEqual` and
10
+ // `toThrow(instance)` all fail with "Compared values have no visual difference". Jest
11
+ // passes them, because Jest compares two Errors by message alone (@jest/expect-utils
12
+ // 30.2.0 build/index.js:179).
13
+ //
14
+ // Adopting Jest's rule would be more lenient than Jest itself: a custom tester runs
15
+ // before Vitest's `typeEquality`, so a message-only tester makes
16
+ // `expect(TypeError('x')).toStrictEqual(Error('x'))` pass, and Vitest's toThrow
17
+ // compares through the same testers, so it would also ignore a differing `cause`, which
18
+ // Jest 30's toThrow(object) checks (expect 30.2.0 build/index.js:1879-1884). So this
19
+ // keeps Vitest's comparison and changes one thing: an own enumerable property that is a
20
+ // function on BOTH errors is left out. A function on an error instance is behaviour, not
21
+ // data — no error is distinguished from another by which closure it carries — and the
22
+ // comparison is otherwise exactly Vitest's, so it is never more lenient than Vitest
23
+ // anywhere a function-valued field is not involved. A property that is a function on
24
+ // one side only (a method against a value, or a missing key) still differs.
25
+ //
26
+ // The tester returns undefined, leaving the pair to Vitest, unless both are Errors
27
+ // sharing such a property. It is registered by the jest-compat setup, and `expect` is
28
+ // shared, so Vitest-style assertions in the same project get it too.
29
+
30
+ /** Vitest's `isError` (index.aTZaPVhf.js:1246), so the same values count as Errors. */
31
+ function isError(value) {
32
+ if (typeof Error.isError === "function") return Error.isError(value);
33
+ switch (Object.prototype.toString.call(value)) {
34
+ case "[object Error]":
35
+ case "[object Exception]":
36
+ case "[object DOMException]":
37
+ return true;
38
+ default:
39
+ return value instanceof Error;
40
+ }
41
+ }
42
+
43
+ /** Own enumerable keys, strings and symbols, as `{ ...value }` copies them. */
44
+ const ownEnumerableKeys = (value) =>
45
+ Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
46
+
47
+ /** Keys whose value is a function on both errors. */
48
+ function sharedMethodKeys(a, b) {
49
+ const keys = new Set();
50
+ for (const key of ownEnumerableKeys(a)) {
51
+ if (
52
+ typeof a[key] === "function" &&
53
+ Object.prototype.propertyIsEnumerable.call(b, key) &&
54
+ typeof b[key] === "function"
55
+ ) {
56
+ keys.add(key);
57
+ }
58
+ }
59
+ return keys;
60
+ }
61
+
62
+ /**
63
+ * `{ ...value }` without `skip`. The spread defines each key, where assigning would run
64
+ * the `__proto__` setter for an own `__proto__` key and hide that key's value.
65
+ */
66
+ function ownData(value, skip) {
67
+ const data = { ...value };
68
+ for (const key of skip) delete data[key];
69
+ return data;
70
+ }
71
+
72
+ // Errors being compared on this call stack, so a cycle through a cause or a property
73
+ // ends as Vitest's `eq` ends one: a pair met again is equal only at the same depth.
74
+ // Recursion goes through `this.equals`, which starts fresh stacks of its own.
75
+ const aStack = [];
76
+ const bStack = [];
77
+
78
+ export function errorsWithMethodsEquality(a, b, customTesters) {
79
+ // Vitest runs the testers on every pair it compares, primitives and plain objects
80
+ // included, so the common case returns before `isError`.
81
+ if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
82
+ return undefined;
83
+ }
84
+ if (a === b || !isError(a) || !isError(b)) return undefined;
85
+ if (typeof this?.equals !== "function") return undefined;
86
+ const skip = sharedMethodKeys(a, b);
87
+ if (skip.size === 0) return undefined;
88
+
89
+ // The testers after this one are the ones Vitest would still have run on this pair
90
+ // (eq's loop, index.aTZaPVhf.js:1262). toStrictEqual appends `typeEquality` to the
91
+ // list, so its constructor check is kept, and a user tester registered later still
92
+ // decides its own errors.
93
+ const testers = Array.isArray(customTesters) ? customTesters : [];
94
+ for (const tester of testers.slice(testers.indexOf(errorsWithMethodsEquality) + 1)) {
95
+ const result = tester.call(this, a, b, testers);
96
+ if (result !== undefined) return result;
97
+ }
98
+ // A tester sees no strict flag. toStrictEqual is the matcher that passes Vitest's
99
+ // `typeEquality` (index.aTZaPVhf.js:2020-2029), and its strict check is what makes an
100
+ // `undefined`-valued property differ from a missing one.
101
+ const strict = testers.some((tester) => tester.name === "typeEquality");
102
+
103
+ let depth = aStack.length;
104
+ while (depth--) {
105
+ if (aStack[depth] === a) return bStack[depth] === b;
106
+ if (bStack[depth] === b) return false;
107
+ }
108
+ aStack.push(a);
109
+ bStack.push(b);
110
+ try {
111
+ // isErrorEqual (index.aTZaPVhf.js:1344-1357), in its order.
112
+ const equals = (x, y) => this.equals(x, y, testers, strict);
113
+ let result =
114
+ Object.prototype.toString.call(a) === Object.prototype.toString.call(b) &&
115
+ a.name === b.name &&
116
+ a.message === b.message;
117
+ if (typeof b.cause !== "undefined") result &&= equals(a.cause, b.cause);
118
+ if (a instanceof AggregateError && b instanceof AggregateError) {
119
+ result &&= equals(a.errors, b.errors);
120
+ }
121
+ result &&= equals(ownData(a, skip), ownData(b, skip));
122
+ return result;
123
+ } finally {
124
+ aStack.pop();
125
+ bStack.pop();
126
+ }
127
+ }
@@ -141,10 +141,140 @@ function originalOf(object, key, accessType) {
141
141
  }
142
142
  }
143
143
 
144
+ // `jest.useFakeTimers(config)` takes Jest's options, which are not Vitest's. Vitest
145
+ // spreads its argument straight into @sinonjs/fake-timers' `install`, so passing
146
+ // Jest's keys through unchanged drops them without a word: `{ advanceTimers: true }`
147
+ // never advanced, `{ doNotFake: ['performance'] }` faked it anyway. Jest translates
148
+ // them in `_toSinonFakeTimersConfig` (@jest/fake-timers 30.2, modernFakeTimers):
149
+ //
150
+ // advanceTimeDelta: typeof advanceTimers === 'number' ? advanceTimers : undefined,
151
+ // loopLimit: timerLimit || 100_000,
152
+ // now: now ?? Date.now(),
153
+ // shouldAdvanceTime: Boolean(advanceTimers),
154
+ // shouldClearNativeTimers: true,
155
+ // toFake: every fakeable API minus doNotFake,
156
+ //
157
+ // and that translation is mirrored here, with these deliberate differences:
158
+ //
159
+ // - A Jest option left out is left to Vitest, so a project's `test.fakeTimers` (the
160
+ // nearest thing to Jest's `fakeTimers` config, which Jest merges into every call)
161
+ // still applies. Jest's defaults and Vitest's agree for every key but two, which are
162
+ // always passed: `loopLimit` (Jest 100_000, Vitest 10_000), and `now`, which Jest
163
+ // reads from the real clock on every install while Vitest resumes the last fake one.
164
+ // A `now` set in `test.fakeTimers` is therefore overridden, as in Jest only an
165
+ // explicit `now` sets the start.
166
+ // - `nextTick` and `queueMicrotask` stay real. Jest fakes them by default; Vitest
167
+ // never fakes them unless asked, because Node and Vitest's runner use them
168
+ // internally ("Do not mock timers internally used by node by default", Vitest's
169
+ // FakeTimers.useFakeTimers). This keeps Vitest's choice.
170
+ // - `doNotFake` becomes an explicit `toFake`. Vitest 5 also accepts `toNotFake`, but
171
+ // Vitest 4 always passes its own `toFake` to sinon, whose bundled version has no
172
+ // `toNotFake` at all, so only `toFake` means the same on every supported major.
173
+ // - `legacyFakeTimers: true` has no Vitest counterpart (Jest's legacy timers are
174
+ // jest.fn mocks). It is approximated with modern timers over the functions legacy
175
+ // timers replace, leaving Date real as they do, and warns once that assertions on
176
+ // the timer functions themselves cannot work.
177
+ //
178
+ // Known limitation: options given before React Native Testing Library loads do not
179
+ // survive it. At import RNTL calls `jest.useRealTimers()` and then
180
+ // `jest.useFakeTimers({ legacyFakeTimers: false })` (helpers/timers runWithRealTimers),
181
+ // which this translates without the earlier options, so a setup file's top-level
182
+ // `jest.useFakeTimers({ doNotFake })` ends up faking everything. Jest merges its
183
+ // project `fakeTimers` config into that call; carrying a setup file's options across
184
+ // it belongs to a versioned environment layer, not to this shim. Until then the docs
185
+ // recommend a `beforeEach` call or Vitest's `test.fakeTimers`.
186
+
187
+ /**
188
+ * Every API @sinonjs/fake-timers can fake: its `FakeMethod` typedef, as bundled in
189
+ * Vitest 5.0. Names the running sinon lacks are skipped (Vitest installs with
190
+ * `ignoreMissingTimers: true`); tests/jest-compat-fake-timers.test.ts fails if the
191
+ * installed sinon gains a name this list lacks.
192
+ */
193
+ export const FAKEABLE_TIMER_APIS = [
194
+ "setTimeout",
195
+ "clearTimeout",
196
+ "setImmediate",
197
+ "clearImmediate",
198
+ "setInterval",
199
+ "clearInterval",
200
+ "Date",
201
+ "nextTick",
202
+ "hrtime",
203
+ "requestAnimationFrame",
204
+ "cancelAnimationFrame",
205
+ "requestIdleCallback",
206
+ "cancelIdleCallback",
207
+ "performance",
208
+ "queueMicrotask",
209
+ "Intl",
210
+ "Temporal",
211
+ ];
212
+
213
+ /** Faked only when asked for by name, as Vitest's own default list leaves them out. */
214
+ const KEPT_REAL = new Set(["nextTick", "queueMicrotask"]);
215
+
216
+ /**
217
+ * What Jest's legacy timers replace (LegacyFakeTimers.useFakeTimers, @jest/fake-timers
218
+ * 30.2): the timer functions and animation frames, never Date. `process.nextTick`,
219
+ * which they also replace, stays real for the reason above.
220
+ */
221
+ const LEGACY_FAKED = [
222
+ "setTimeout",
223
+ "clearTimeout",
224
+ "setInterval",
225
+ "clearInterval",
226
+ "setImmediate",
227
+ "clearImmediate",
228
+ "requestAnimationFrame",
229
+ "cancelAnimationFrame",
230
+ ];
231
+
232
+ /** Jest's `timerLimit || 100_000`. */
233
+ const loopLimitOf = (timerLimit) => timerLimit || 100_000;
234
+
144
235
  /**
145
- * The `jest` object: `vi` for everything except `fn` and `spyOn`. A proxy rather than
146
- * a copy, so members the compat setup installs on `vi` (requireActual, the timer
147
- * guards), and anything a suite assigns to `jest.*`, stay one shared object.
236
+ * Translate a Jest fake-timers config into the one `vi.useFakeTimers` takes. Keys that
237
+ * are not Jest's (a Vitest option passed through `jest.*`) are kept as given.
238
+ */
239
+ export function toVitestFakeTimersConfig(config, realNow = Date.now) {
240
+ const { advanceTimers, doNotFake, legacyFakeTimers, now, timerLimit, ...rest } = config ?? {};
241
+ const out = { ...rest, loopLimit: loopLimitOf(timerLimit) };
242
+ if (legacyFakeTimers) {
243
+ // Legacy timers take no options of their own: no auto-advance, no clock to set.
244
+ return { ...out, shouldAdvanceTime: false, toFake: [...LEGACY_FAKED] };
245
+ }
246
+ if (advanceTimers !== undefined) {
247
+ out.shouldAdvanceTime = Boolean(advanceTimers);
248
+ if (typeof advanceTimers === "number") out.advanceTimeDelta = advanceTimers;
249
+ }
250
+ // Jest's `now ?? Date.now()`, read from the real clock on every install. Left out,
251
+ // Vitest would start the new clock where the last fake one stopped, so a date one
252
+ // test pinned with setSystemTime leaked into every later jest.useFakeTimers().
253
+ out.now = now ?? realNow();
254
+ if (doNotFake !== undefined) {
255
+ const real = new Set(doNotFake);
256
+ out.toFake = FAKEABLE_TIMER_APIS.filter((api) => !KEPT_REAL.has(api) && !real.has(api));
257
+ }
258
+ return out;
259
+ }
260
+
261
+ let warnedLegacy = false;
262
+ function warnLegacyOnce() {
263
+ if (warnedLegacy) return;
264
+ warnedLegacy = true;
265
+ console.warn(
266
+ "[vitest-native] jest.useFakeTimers({ legacyFakeTimers: true }) runs Vitest's modern " +
267
+ "fake timers over setTimeout/setInterval/setImmediate/requestAnimationFrame, with " +
268
+ "Date left real. The timer functions are not mock functions, so legacy-only " +
269
+ "assertions such as expect(setTimeout).toHaveBeenCalled() are not supported.",
270
+ );
271
+ }
272
+
273
+ /**
274
+ * The `jest` object: `vi` for everything except `fn`, `spyOn`, `useFakeTimers` and
275
+ * `useRealTimers`. A proxy rather than a copy, so members the compat setup installs
276
+ * on `vi` (requireActual, the timer guards), and anything a suite assigns to
277
+ * `jest.*`, stay one shared object.
148
278
  */
149
279
  export function createJestObject(vi, members = {}) {
150
280
  function fn(impl) {
@@ -160,16 +290,30 @@ export function createJestObject(vi, members = {}) {
160
290
  if (vi.isMockFunction(original) && spy === original) return spy;
161
291
  return withJestSemantics(spy, original);
162
292
  }
293
+ // Jest's options translated (see toVitestFakeTimersConfig), and Jest's return value:
294
+ // both return the `jest` object (jest-runtime's `useFakeTimers`/`useRealTimers`), so
295
+ // `jest.useFakeTimers().setSystemTime(…)` chains on `jest`, not on `vi`.
296
+ function useFakeTimers(config) {
297
+ if (config?.legacyFakeTimers) warnLegacyOnce();
298
+ vi.useFakeTimers(toVitestFakeTimersConfig(config, () => vi.getRealSystemTime()));
299
+ return jest;
300
+ }
301
+ function useRealTimers() {
302
+ vi.useRealTimers();
303
+ return jest;
304
+ }
163
305
  // Assigning `jest.fn`/`jest.spyOn` (a suite or library patching them) replaces them
164
306
  // here, as assigning to `jest` did when it was `vi`, without patching `vi` itself.
165
307
  // Any other assignment goes to `vi`, which the compat setup and suites share.
166
308
  const own = new Map([
167
309
  ["fn", fn],
168
310
  ["spyOn", spyOn],
311
+ ["useFakeTimers", useFakeTimers],
312
+ ["useRealTimers", useRealTimers],
169
313
  // Jest's module-registry members, supplied by the compat setup (setup.mjs).
170
314
  ...Object.entries(members),
171
315
  ]);
172
- return new Proxy(vi, {
316
+ const jest = new Proxy(vi, {
173
317
  get(target, prop, receiver) {
174
318
  if (own.has(prop)) return own.get(prop);
175
319
  return Reflect.get(target, prop, receiver);
@@ -182,6 +326,7 @@ export function createJestObject(vi, members = {}) {
182
326
  return Reflect.set(target, prop, value, receiver);
183
327
  },
184
328
  });
329
+ return jest;
185
330
  }
186
331
 
187
332
  // One `jest` per test file, shared by the global and `@jest/globals`, as in Jest where