vitest 5.0.4 → 5.1.0-beta.1

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 (53) hide show
  1. package/LICENSE.md +1 -1
  2. package/dist/browser.d.ts +4 -4
  3. package/dist/browser.js +3 -3
  4. package/dist/chunks/{base.CYbb9pQW.js → base.PJyN9N0u.js} +13 -11
  5. package/dist/chunks/{browser.d.FGQ9_rK6.d.ts → browser.d.D7a15z82.d.ts} +4 -3
  6. package/dist/chunks/{cac.DTXWs6Ni.js → cac.Ccf5Je_F.js} +21 -9
  7. package/dist/chunks/{cli-api.Bx28WXzL.js → cli-api.DE22LIzq.js} +4584 -4369
  8. package/dist/chunks/{config.d.QBNYCgFU.d.ts → config.d.BgktgHfG.d.ts} +134 -93
  9. package/dist/chunks/{console.DtI4yxfN.js → console.DrMPSuFo.js} +5 -2
  10. package/dist/chunks/{constants.-juJ8b_4.js → constants.DLYM6cZY.js} +1 -1
  11. package/dist/chunks/{creator.CLL8QFMS.js → creator.BQ3onxbh.js} +5 -5
  12. package/dist/chunks/{doctor.Bqc8NDDb.js → doctor.CPlMs7-y.js} +1 -1
  13. package/dist/chunks/{environment.d.BY56MoGs.d.ts → environment.d.CbTLsZSj.d.ts} +1 -1
  14. package/dist/chunks/{globals.CTWMSIPL.js → globals.TipXUFcZ.js} +5 -5
  15. package/dist/chunks/{index.GYMZ12wH.js → index.B03wg4V0.js} +5 -3
  16. package/dist/chunks/{index.CPNMO3h-.js → index.BFXLpwll.js} +119 -43
  17. package/dist/chunks/{index.uwTEEBsB.js → index.BXttT2gP.js} +37 -18
  18. package/dist/chunks/{index.B8VfM7Mf.js → index.D14IAvnE.js} +149 -96
  19. package/dist/chunks/{init-forks.CbYYa5xq.js → init-forks.BL1E3__4.js} +1 -1
  20. package/dist/chunks/{init-threads.DtjdvxPl.js → init-threads.CYXAGzKQ.js} +1 -1
  21. package/dist/chunks/{init.DrHtV5zd.js → init.DVorxUyL.js} +3 -3
  22. package/dist/chunks/{nativeModuleMocker.D8f4cW4A.js → nativeModuleMocker.3uBcOd84.js} +26 -4
  23. package/dist/chunks/{plugin.d.C8mmycim.d.ts → plugin.d.olEn8IL9.d.ts} +108 -20
  24. package/dist/chunks/{rpc.DpkUyY1N.js → rpc.BV0XpJ5q.js} +1 -1
  25. package/dist/chunks/{rpc.d.Cft1biqy.d.ts → rpc.d.DGFP7vWr.d.ts} +1 -1
  26. package/dist/chunks/{run.Cm2sd7-x.js → run.BfrMbGVD.js} +59 -32
  27. package/dist/chunks/{setup-common.BABL3z3m.js → setup-common.llxJkJaD.js} +2 -2
  28. package/dist/chunks/{source-map.DdKI5IHB.js → source-map.BjWcsIL9.js} +32 -24
  29. package/dist/chunks/{source-map.d.kzFtk_dj.d.ts → source-map.d.C5A_Tr6V.d.ts} +2 -1
  30. package/dist/chunks/{task-utils.d.PFh2BV6Q.d.ts → task-utils.d.BUh7ylUT.d.ts} +2 -1
  31. package/dist/chunks/{vm.Uo_C9Uld.js → vm.Cwj01UFE.js} +16 -5
  32. package/dist/chunks/{worker.d.BH-r1bPu.d.ts → worker.d.gsA26mmR.d.ts} +3 -3
  33. package/dist/cli.js +2 -2
  34. package/dist/config.cjs +1 -1
  35. package/dist/config.d.ts +11 -11
  36. package/dist/config.js +1 -1
  37. package/dist/index.d.ts +14 -13
  38. package/dist/index.js +4 -4
  39. package/dist/module-evaluator.d.ts +3 -2
  40. package/dist/module-evaluator.js +16 -12
  41. package/dist/node.d.ts +29 -11
  42. package/dist/node.js +7 -7
  43. package/dist/runtime.d.ts +4 -4
  44. package/dist/runtime.js +3 -2
  45. package/dist/task-utils.js +3 -1
  46. package/dist/worker.d.ts +4 -4
  47. package/dist/worker.js +9 -9
  48. package/dist/workers/forks.js +10 -10
  49. package/dist/workers/runVmTests.js +10 -8
  50. package/dist/workers/threads.js +10 -10
  51. package/dist/workers/vmForks.js +9 -8
  52. package/dist/workers/vmThreads.js +9 -8
  53. package/package.json +14 -14
@@ -5,6 +5,7 @@ import { E as EnvironmentTeardownError, a as getSafeWorkerState } from './utils.
5
5
  import { pathToFileURL, URL as URL$1 } from 'node:url';
6
6
  import { distDir } from '../path.js';
7
7
  import { VitestModuleEvaluator, unwrapId } from '../module-evaluator.js';
8
+ import { AsyncLocalStorage } from 'node:async_hooks';
8
9
  import { isAbsolute, resolve } from 'node:path';
9
10
  import vm from 'node:vm';
10
11
  import { MockerRegistry, mockObject, RedirectedModule, AutomockedModule } from '@vitest/mocker';
@@ -48,7 +49,9 @@ class BareModuleMocker {
48
49
  return this.registries.get(suite);
49
50
  }
50
51
  reset() {
52
+ for (const registry of this.registries.values()) for (const url of registry.keys()) this.invalidateModuleById(registry.get(url).id);
51
53
  this.registries.clear();
54
+ BareModuleMocker.pendingIds = [];
52
55
  }
53
56
  invalidateModuleById(_id) {
54
57
  // implemented by mockers that control the module runner
@@ -102,10 +105,20 @@ class BareModuleMocker {
102
105
  };
103
106
  });
104
107
  }
108
+ registeringMocks;
105
109
  async resolveMocks() {
106
- if (!BareModuleMocker.pendingIds.length) return;
107
- const pendingIds = BareModuleMocker.pendingIds;
108
- BareModuleMocker.pendingIds = [];
110
+ if (BareModuleMocker.pendingIds.length) {
111
+ const pendingIds = BareModuleMocker.pendingIds;
112
+ BareModuleMocker.pendingIds = [];
113
+ const registration = this.registerMocks(pendingIds, this.registeringMocks).finally(() => {
114
+ if (this.registeringMocks === registration) this.registeringMocks = void 0;
115
+ });
116
+ this.registeringMocks = registration;
117
+ }
118
+ await this.registeringMocks;
119
+ }
120
+ async registerMocks(pendingIds, previous) {
121
+ await previous?.catch(() => {});
109
122
  const resolveMock = async (mock) => {
110
123
  return {
111
124
  mock,
@@ -273,6 +286,9 @@ class VitestMocker extends BareModuleMocker {
273
286
  moduleRunner;
274
287
  options;
275
288
  filterPublicKeys;
289
+ // factories that run in the current async context, the innermost is last
290
+ factoryContexts = new AsyncLocalStorage();
291
+ pendingFactories = /* @__PURE__ */ new Map();
276
292
  constructor(moduleRunner, options) {
277
293
  super(options);
278
294
  this.moduleRunner = moduleRunner;
@@ -304,17 +320,50 @@ class VitestMocker extends BareModuleMocker {
304
320
  if (this.spyModule) return;
305
321
  this.spyModule = await this.moduleRunner.import(spyModulePath);
306
322
  }
307
- reset() {
308
- this.registries.clear();
309
- }
310
323
  invalidateModuleById(id) {
311
324
  const mockId = this.getMockPath(id);
325
+ this.pendingFactories.delete(mockId);
312
326
  const node = this.evaluatedModules.getModuleById(mockId);
313
327
  if (node) {
314
328
  this.evaluatedModules.invalidateModule(node);
315
329
  node.mockedExports = void 0;
316
330
  }
317
331
  }
332
+ getMockContext() {
333
+ return { callstack: (this.factoryContexts.getStore()?.findLast((context) => this.pendingFactories.has(context.mockId)))?.callstack ?? null };
334
+ }
335
+ /**
336
+ * Whether the factory of this mock runs in the current async context,
337
+ * or waits for a factory that runs in it. Such imports get the original module.
338
+ * The callstack cannot tell it: all imports from one module share the same array.
339
+ */
340
+ isResolvingMock(mockId) {
341
+ if (!this.pendingFactories.has(mockId)) return false;
342
+ return (this.factoryContexts.getStore() ?? []).some((context) => context.mockId === mockId) || this.waitsForCurrentFactory(mockId);
343
+ }
344
+ /**
345
+ * Whether the module is still evaluating and waits for a factory
346
+ * that runs in the current async context.
347
+ */
348
+ isWaitingForFactory(mod) {
349
+ return !!mod.promise && !mod.evaluated && this.waitsForCurrentFactory(mod.id);
350
+ }
351
+ waitsForCurrentFactory(id) {
352
+ const factories = (this.factoryContexts.getStore() ?? []).map((context) => context.mockId);
353
+ const visited = new Set(factories);
354
+ // a factory that waits for the current one makes everything that waits for it wait too
355
+ while (factories.length) {
356
+ const waiting = this.pendingFactories.get(factories.pop())?.waiting;
357
+ if (waiting?.has(id)) return true;
358
+ waiting?.forEach((waiter) => {
359
+ if (this.pendingFactories.has(waiter) && !visited.has(waiter)) {
360
+ visited.add(waiter);
361
+ factories.push(waiter);
362
+ }
363
+ });
364
+ }
365
+ return false;
366
+ }
318
367
  ensureModule(id, url) {
319
368
  const node = this.evaluatedModules.ensureModule(id, url);
320
369
  // TODO
@@ -327,9 +376,53 @@ class VitestMocker extends BareModuleMocker {
327
376
  };
328
377
  return node;
329
378
  }
330
- async callFunctionMock(id, url, mock) {
379
+ callFunctionMock(id, url, mock, callstack) {
331
380
  const node = this.ensureModule(id, url);
332
- if (node.exports) return node.exports;
381
+ if (node.exports) return Promise.resolve(node.exports);
382
+ return this.resolveMockOnce(id, callstack, () => this.resolveFunctionMock(node, mock));
383
+ }
384
+ resolveMockOnce(id, callstack, resolve) {
385
+ const contexts = this.factoryContexts.getStore() ?? [];
386
+ const waiting = [...callstack, ...contexts.map((context) => context.mockId)];
387
+ const pending = this.pendingFactories.get(id);
388
+ if (pending) {
389
+ waiting.forEach((waiter) => pending.waiting.add(waiter));
390
+ return pending.promise;
391
+ }
392
+ const factory = {
393
+ promise: this.factoryContexts.run([...contexts, {
394
+ mockId: id,
395
+ callstack
396
+ }], resolve).finally(() => {
397
+ if (this.pendingFactories.get(id) === factory) this.pendingFactories.delete(id);
398
+ if (!this.pendingFactories.size)
399
+ // an enabled storage slows down every promise on Node 22
400
+ this.factoryContexts.disable();
401
+ }),
402
+ waiting: new Set(waiting)
403
+ };
404
+ this.pendingFactories.set(id, factory);
405
+ return factory.promise;
406
+ }
407
+ async resolveAutomock(url, mockId, evaluatedNode, callstack, type) {
408
+ const Object = this.primitives.Object;
409
+ // we have to define a separate object that will copy all properties into itself
410
+ // and can't just use the same `exports` define automatically by Vite before the evaluator
411
+ const exports = Object.create(null);
412
+ Object.defineProperty(exports, Symbol.toStringTag, {
413
+ value: "Module",
414
+ configurable: true,
415
+ writable: true
416
+ });
417
+ const node = this.ensureModule(mockId, this.getMockPath(evaluatedNode.url));
418
+ node.meta = evaluatedNode.meta;
419
+ node.file = evaluatedNode.file;
420
+ node.mockedExports = exports;
421
+ const mod = await this.moduleRunner.cachedRequest(url, node, callstack, void 0, true);
422
+ this.mockObject(mod, exports, type);
423
+ return exports;
424
+ }
425
+ async resolveFunctionMock(node, mock) {
333
426
  const exports = await mock.resolve();
334
427
  const moduleExports = new Proxy(exports, { get: (target, prop) => {
335
428
  const val = target[prop];
@@ -373,7 +466,10 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
373
466
  const Object = this.primitives.Object;
374
467
  return this.mockObject(mod, Object.create(Object.prototype), mock.type);
375
468
  }
376
- if (mock.type === "manual") return this.callFunctionMock(id, url, mock);
469
+ if (mock.type === "manual") {
470
+ const mockId = this.getMockPath(id);
471
+ return this.callFunctionMock(mockId, this.getMockPath(url), mock, [importer, mockId]);
472
+ }
377
473
  const node = await this.moduleRunner.fetchModule(mock.redirect);
378
474
  return this.moduleRunner.cachedRequest(mock.redirect, node, [importer], void 0, true);
379
475
  }
@@ -389,36 +485,14 @@ If you need to partially mock a module, you can use "importOriginal" helper insi
389
485
  });
390
486
  if (mock.type === "automock" || mock.type === "autospy") {
391
487
  const cache = this.evaluatedModules.getModuleById(mockId);
392
- if (cache && cache.mockedExports) return cache.mockedExports;
393
- const Object = this.primitives.Object;
394
- // we have to define a separate object that will copy all properties into itself
395
- // and can't just use the same `exports` define automatically by Vite before the evaluator
396
- const exports = Object.create(null);
397
- Object.defineProperty(exports, Symbol.toStringTag, {
398
- value: "Module",
399
- configurable: true,
400
- writable: true
401
- });
402
- const node = this.ensureModule(mockId, this.getMockPath(evaluatedNode.url));
403
- node.meta = evaluatedNode.meta;
404
- node.file = evaluatedNode.file;
405
- node.mockedExports = exports;
406
- const mod = await this.moduleRunner.cachedRequest(url, node, callstack, void 0, true);
407
- this.mockObject(mod, exports, mock.type);
408
- return exports;
409
- }
410
- if (mock.type === "manual" && !callstack.includes(mockId) && !callstack.includes(url)) try {
411
- callstack.push(mockId);
412
- // this will not work if user does Promise.all(import(), import())
413
- // we can also use AsyncLocalStorage to store callstack, but this won't work in the browser
414
- // maybe we should improve mock API in the future?
415
- this.mockContext.callstack = callstack;
416
- return await this.callFunctionMock(mockId, this.getMockPath(url), mock);
417
- } finally {
418
- this.mockContext.callstack = null;
419
- const indexMock = callstack.indexOf(mockId);
420
- callstack.splice(indexMock, 1);
488
+ if (cache?.mockedExports) {
489
+ // while the original module evaluates, only its own imports get the partial object, like in a cycle
490
+ const isSelfImport = callstack.includes(evaluatedNode.id) || this.isResolvingMock(mockId);
491
+ if (!this.pendingFactories.has(mockId) || isSelfImport) return cache.mockedExports;
492
+ }
493
+ return this.resolveMockOnce(mockId, [...callstack, mockId], () => this.resolveAutomock(url, mockId, evaluatedNode, callstack, mock.type));
421
494
  }
495
+ if (mock.type === "manual" && !callstack.includes(mockId) && !callstack.includes(url) && !this.isResolvingMock(mockId)) return this.callFunctionMock(mockId, this.getMockPath(url), mock, [...callstack, mockId]);
422
496
  else if (mock.type === "redirect" && !callstack.includes(mock.redirect)) {
423
497
  span.setAttribute("vitest.mock.redirect", mock.redirect);
424
498
  return mock.redirect;
@@ -575,10 +649,11 @@ class VitestModuleRunner extends viteModuleRunner.ModuleRunner {
575
649
  async cachedRequest(url, mod, callstack = [], metadata, ignoreMock = false) {
576
650
  // Track for a better error message if dynamic import is not resolved properly
577
651
  this._callstacks.set(mod, callstack);
652
+ // the factory imports a module that waits for this factory, treat it as a cycle
653
+ if (mod.exports && this.mocker.isWaitingForFactory(mod)) return mod.exports;
578
654
  if (ignoreMock) return this._cachedRequest(url, mod, callstack, metadata);
579
655
  let mocked;
580
656
  if (mod.meta && "mockedModule" in mod.meta) {
581
- const mockedModule = mod.meta.mockedModule;
582
657
  const mockId = this.mocker.getMockPath(mod.id);
583
658
  const currentMock = this.mocker.getDependencyMock(mod.id);
584
659
  // bypass mock and force "importActual" behavior when:
@@ -587,11 +662,11 @@ class VitestModuleRunner extends viteModuleRunner.ModuleRunner {
587
662
  const node = await this.fetchModule(injectQuery(url, "_vitest_original"));
588
663
  return this._cachedRequest(node.url, node, callstack, metadata);
589
664
  }
590
- if (callstack.includes(mockId) || callstack.includes(url) || "redirect" in currentMock && callstack.includes(currentMock.redirect)) {
665
+ if (callstack.includes(mockId) || this.mocker.isResolvingMock(mockId) || callstack.includes(url) || "redirect" in currentMock && callstack.includes(currentMock.redirect)) {
591
666
  const node = await this.fetchModule(injectQuery(url, "_vitest_original"));
592
667
  return this._cachedRequest(node.url, node, callstack, metadata);
593
668
  }
594
- if ((currentMock.type === "automock" || currentMock.type === "autospy") && currentMock !== mockedModule) {
669
+ if (currentMock.type === "automock" || currentMock.type === "autospy") {
595
670
  const freshNode = await this.fetchModule(injectQuery(url, "_vitest_original"));
596
671
  mocked = await this.mocker.requestWithMockedModule(url, freshNode, callstack, currentMock);
597
672
  } else mocked = await this.mocker.requestWithMockedModule(url, mod, callstack, currentMock);
@@ -760,7 +835,7 @@ function startVitestModuleRunner(options) {
760
835
  const rawId = unwrapId(id);
761
836
  resolvingModules.add(rawId);
762
837
  try {
763
- if (VitestMocker.pendingIds.length) await moduleRunner.mocker.resolveMocks();
838
+ if (VitestMocker.pendingIds.length || moduleRunner.mocker.registeringMocks) await moduleRunner.mocker.resolveMocks();
764
839
  if (!isImportActual) {
765
840
  const resolvedMock = moduleRunner.mocker.getDependencyMockByUrl(id);
766
841
  if (resolvedMock?.type === "manual" || resolvedMock?.type === "redirect") return {
@@ -769,7 +844,8 @@ function startVitestModuleRunner(options) {
769
844
  id: resolvedMock.id,
770
845
  url: resolvedMock.url,
771
846
  invalidate: false,
772
- mockedModule: resolvedMock
847
+ // the module node outlives the test file, so it must not keep the mock factory alive
848
+ mockedModule: true
773
849
  };
774
850
  }
775
851
  if (isBuiltin(rawId)) return {
@@ -1,6 +1,6 @@
1
- import { b as printDiffOrStringify, d as diff, a as processError, T as TestSyntaxError, g as getCurrentTest, c as createTaskCollector, e as getCurrentSuite, f as createChainable, h as getHooks, i as getFn, m as matchesTags, j as afterAll, k as afterEach, l as aroundAll, n as aroundEach, o as beforeAll, q as beforeEach, r as describe, t as it, u as onTestFailed, v as onTestFinished, w as recordArtifact, x as suite, y as test } from './run.Cm2sd7-x.js';
1
+ import { b as printDiffOrStringify, d as diff, a as processError, T as TestSyntaxError, g as getCurrentTest, c as createTaskCollector, e as getCurrentSuite, f as createChainable, h as getHooks, i as getFn, m as matchesTags, j as afterAll, k as afterEach, l as aroundAll, n as aroundEach, o as beforeAll, q as beforeEach, r as describe, t as it, u as onTestFailed, v as onTestFinished, w as recordArtifact, x as suite, y as test } from './run.BfrMbGVD.js';
2
2
  import { i as isChildProcess, r as resetModules, g as getWorkerState } from './utils.DYj33du9.js';
3
- import { e as parseErrorStacktrace, a as getSafeTimers, f as parseSingleStack, h as delay } from './source-map.DdKI5IHB.js';
3
+ import { e as parseErrorStacktrace, a as getSafeTimers, f as parseSingleStack, h as delay } from './source-map.BjWcsIL9.js';
4
4
  import { i as isMockFunction, f as fn, a as spyOn, r as restoreAllMocks, b as resetAllMocks, c as clearAllMocks } from './spy.DDMXghXv.js';
5
5
  import { u as isObject, B as getCallLastIndex, g as getType, D as noop, x as assertTypes, w as ordinal, E as createSimpleStackTrace, i as isAbsolute, a as relative, n as normalize } from './pathe.M-eThtNZ.Ba-7h1rE.js';
6
6
  import { g as getDefaultExportFromCjs, f as format, a as commonjsGlobal } from './index.D0heQG4v.js';
@@ -11,7 +11,7 @@ import { use, util } from 'chai';
11
11
  import { getNames, getTests, getTestName, createFileTask } from '../task-utils.js';
12
12
  import { g as getSerializers, a as addSerializer } from './plugins.DPQHVT0p.js';
13
13
  import { p as positionToOffset, o as offsetToLineNumber, l as lineSplitRE } from './offset.Dy-5Fdfn.js';
14
- import { r as rpc, V as VitestEvaluatedModules } from './rpc.DpkUyY1N.js';
14
+ import { r as rpc, V as VitestEvaluatedModules } from './rpc.BV0XpJ5q.js';
15
15
  import { Bench } from 'tinybench';
16
16
  import { expectTypeOf } from 'expect-type';
17
17
 
@@ -3643,19 +3643,6 @@ function requireFakeTimersSrc () {
3643
3643
  hasRequiredFakeTimersSrc = 1;
3644
3644
 
3645
3645
  const globalObject = requireLib().global;
3646
- let timersModule, timersPromisesModule;
3647
- if (typeof __vitest_required__ !== 'undefined') {
3648
- try {
3649
- timersModule = __vitest_required__.timers;
3650
- } catch {
3651
- // ignored
3652
- }
3653
- try {
3654
- timersPromisesModule = __vitest_required__.timersPromises;
3655
- } catch {
3656
- // ignored
3657
- }
3658
- }
3659
3646
 
3660
3647
  /**
3661
3648
  * @typedef {"nextAsync" | "manual" | "interval"} TickMode
@@ -4179,7 +4166,8 @@ function requireFakeTimersSrc () {
4179
4166
  isPresent.hrtime && typeof _global.process.hrtime.bigint === "function";
4180
4167
  isPresent.nextTick =
4181
4168
  _global.process && typeof _global.process.nextTick === "function";
4182
- const utilPromisify = _global.process && _global.__vitest_required__ && _global.__vitest_required__.util.promisify;
4169
+ const utilPromisify =
4170
+ _global.process && _global.__vitest_required__?.util.promisify;
4183
4171
  isPresent.performance =
4184
4172
  _global.performance && typeof _global.performance.now === "function";
4185
4173
  const hasPerformancePrototype =
@@ -6219,6 +6207,7 @@ function requireFakeTimersSrc () {
6219
6207
  clock.uninstall = function () {
6220
6208
  uninstalled = true;
6221
6209
  clock.setTickMode({ mode: "manual" });
6210
+ const { timersModule, timersPromisesModule } = clock;
6222
6211
 
6223
6212
  if (clock.methods) {
6224
6213
  const installedHrTime = "_hrtime";
@@ -6392,6 +6381,16 @@ function requireFakeTimersSrc () {
6392
6381
  const clock = createClock(config.now, config.loopLimit);
6393
6382
  clock.shouldClearNativeTimers = config.shouldClearNativeTimers;
6394
6383
 
6384
+ // Vitest injects Node builtins because this file is bundled without require.
6385
+ const vitestRequired =
6386
+ config.fakeNodeBuiltins && _global.__vitest_required__;
6387
+ const timersModule = vitestRequired?.timers;
6388
+ const timersPromisesModule = vitestRequired?.timersPromises;
6389
+ const syncBuiltinESMExports = vitestRequired?.syncBuiltinESMExports;
6390
+ // Keep the modules on the clock so uninstall can restore their methods.
6391
+ clock.timersModule = timersModule;
6392
+ clock.timersPromisesModule = timersPromisesModule;
6393
+
6395
6394
  clock.abortListenerMap = new Map();
6396
6395
 
6397
6396
  if (hasToFake) {
@@ -6733,6 +6732,21 @@ function requireFakeTimersSrc () {
6733
6732
  }
6734
6733
  }
6735
6734
 
6735
+ // Keep ESM named exports in sync with the patched CommonJS modules.
6736
+ if (
6737
+ syncBuiltinESMExports &&
6738
+ (clock.timersModuleMethods?.length ||
6739
+ clock.timersPromisesModuleMethods?.length)
6740
+ ) {
6741
+ const uninstall = clock.uninstall;
6742
+ clock.uninstall = () => {
6743
+ const result = uninstall();
6744
+ syncBuiltinESMExports();
6745
+ return result;
6746
+ };
6747
+ syncBuiltinESMExports();
6748
+ }
6749
+
6736
6750
  return clock;
6737
6751
  }
6738
6752
 
@@ -6872,7 +6886,8 @@ class FakeTimers {
6872
6886
  }
6873
6887
  }
6874
6888
  setSystemTime(now) {
6875
- const date = typeof now === "undefined" || now instanceof Date ? now : new Date(now);
6889
+ const normalized = normalizeSystemTime(now);
6890
+ const date = typeof normalized === "undefined" || normalized instanceof Date ? normalized : new Date(normalized);
6876
6891
  if (this._fakingTime) this._clock.setSystemTime(date);
6877
6892
  else {
6878
6893
  const newFakingDate = date ?? new Date(this.getRealSystemTime());
@@ -6921,6 +6936,10 @@ class FakeTimers {
6921
6936
  return this._fakingTime;
6922
6937
  }
6923
6938
  }
6939
+ // Like fake-timers, read epochMilliseconds from Instant or ZonedDateTime without requiring built-in Temporal types.
6940
+ function normalizeSystemTime(time) {
6941
+ return time && typeof time === "object" && "epochMilliseconds" in time ? time.epochMilliseconds : time;
6942
+ }
6924
6943
 
6925
6944
  const whenSymbol = Symbol.for("$$vitest:when");
6926
6945
  /**