vitest 3.2.0-beta.2 → 3.2.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 (72) hide show
  1. package/LICENSE.md +29 -0
  2. package/dist/browser.d.ts +3 -3
  3. package/dist/browser.js +2 -2
  4. package/dist/chunks/{base.DwtwORaC.js → base.Cg0miDlQ.js} +11 -14
  5. package/dist/chunks/{benchmark.BoF7jW0Q.js → benchmark.CYdenmiT.js} +4 -6
  6. package/dist/chunks/{cac.I9MLYfT-.js → cac.6rXCxFY1.js} +76 -143
  7. package/dist/chunks/{cli-api.d6IK1pnk.js → cli-api.Cej3MBjA.js} +1460 -1344
  8. package/dist/chunks/{config.d.UqE-KR0o.d.ts → config.d.D2ROskhv.d.ts} +2 -0
  9. package/dist/chunks/{console.K1NMVOSc.js → console.CtFJOzRO.js} +25 -45
  10. package/dist/chunks/{constants.BZZyIeIE.js → constants.DnKduX2e.js} +1 -0
  11. package/dist/chunks/{coverage.0iPg4Wrz.js → coverage.DVF1vEu8.js} +4 -12
  12. package/dist/chunks/{coverage.OGU09Jbh.js → coverage.EIiagJJP.js} +578 -993
  13. package/dist/chunks/{creator.DGAdZ4Hj.js → creator.GK6I-cL4.js} +39 -83
  14. package/dist/chunks/date.Bq6ZW5rf.js +73 -0
  15. package/dist/chunks/{defaults.DSxsTG0h.js → defaults.B7q_naMc.js} +2 -1
  16. package/dist/chunks/{env.Dq0hM4Xv.js → env.D4Lgay0q.js} +1 -1
  17. package/dist/chunks/{environment.d.D8YDy2v5.d.ts → environment.d.cL3nLXbE.d.ts} +1 -0
  18. package/dist/chunks/{execute.JlGHLJZT.js → execute.B7h3T_Hc.js} +126 -217
  19. package/dist/chunks/{git.DXfdBEfR.js → git.BVQ8w_Sw.js} +1 -3
  20. package/dist/chunks/{global.d.BPa1eL3O.d.ts → global.d.MAmajcmJ.d.ts} +5 -1
  21. package/dist/chunks/{globals.CpxW8ccg.js → globals.DEHgCU4V.js} +7 -6
  22. package/dist/chunks/{index.CV36oG_L.js → index.BZ0g1JD2.js} +430 -625
  23. package/dist/chunks/{index.DswW_LEs.js → index.BbB8_kAK.js} +25 -24
  24. package/dist/chunks/{index.CmC5OK9L.js → index.CIyJn3t1.js} +38 -82
  25. package/dist/chunks/{index.CfXMNXHg.js → index.CdQS2e2Q.js} +4 -2
  26. package/dist/chunks/{index.DFXFpH3w.js → index.CmSc2RE5.js} +85 -105
  27. package/dist/chunks/index.D3XRDfWc.js +213 -0
  28. package/dist/chunks/{inspector.DbDkSkFn.js → inspector.C914Efll.js} +4 -1
  29. package/dist/chunks/{node.3xsWotC9.js → node.fjCdwEIl.js} +1 -1
  30. package/dist/chunks/{reporters.d.CLC9rhKy.d.ts → reporters.d.C1ogPriE.d.ts} +47 -9
  31. package/dist/chunks/{rpc.D9_013TY.js → rpc.Iovn4oWe.js} +10 -19
  32. package/dist/chunks/{runBaseTests.Dn2vyej_.js → runBaseTests.Dd85QTll.js} +27 -31
  33. package/dist/chunks/{setup-common.CYo3Y0dD.js → setup-common.Dd054P77.js} +16 -42
  34. package/dist/chunks/{typechecker.DnTrplSJ.js → typechecker.DRKU1-1g.js} +163 -186
  35. package/dist/chunks/{utils.BfxieIyZ.js → utils.CAioKnHs.js} +9 -14
  36. package/dist/chunks/{utils.CgTj3MsC.js → utils.XdZDrNZV.js} +6 -13
  37. package/dist/chunks/{vi.BFR5YIgu.js → vi.bdSIJ99Y.js} +137 -263
  38. package/dist/chunks/{vite.d.CBZ3M_ru.d.ts → vite.d.DqE4-hhK.d.ts} +3 -1
  39. package/dist/chunks/{vm.C1HHjtNS.js → vm.BThCzidc.js} +164 -212
  40. package/dist/chunks/{worker.d.D5Xdi-Zr.d.ts → worker.d.DvqK5Vmu.d.ts} +1 -1
  41. package/dist/chunks/{worker.d.CoCI7hzP.d.ts → worker.d.tQu2eJQy.d.ts} +5 -3
  42. package/dist/cli.js +5 -5
  43. package/dist/config.cjs +3 -1
  44. package/dist/config.d.ts +7 -6
  45. package/dist/config.js +3 -3
  46. package/dist/coverage.d.ts +4 -4
  47. package/dist/coverage.js +7 -7
  48. package/dist/environments.d.ts +6 -2
  49. package/dist/environments.js +1 -1
  50. package/dist/execute.d.ts +9 -3
  51. package/dist/execute.js +1 -1
  52. package/dist/index.d.ts +28 -15
  53. package/dist/index.js +5 -5
  54. package/dist/node.d.ts +18 -10
  55. package/dist/node.js +17 -17
  56. package/dist/reporters.d.ts +4 -4
  57. package/dist/reporters.js +4 -4
  58. package/dist/runners.d.ts +6 -3
  59. package/dist/runners.js +59 -80
  60. package/dist/snapshot.js +2 -2
  61. package/dist/suite.js +2 -2
  62. package/dist/worker.js +39 -41
  63. package/dist/workers/forks.js +6 -4
  64. package/dist/workers/runVmTests.js +20 -21
  65. package/dist/workers/threads.js +4 -4
  66. package/dist/workers/vmForks.js +6 -6
  67. package/dist/workers/vmThreads.js +6 -6
  68. package/dist/workers.d.ts +4 -4
  69. package/dist/workers.js +10 -10
  70. package/package.json +21 -19
  71. package/dist/chunks/date.CDOsz-HY.js +0 -53
  72. package/dist/chunks/index.CK1YOQaa.js +0 -143
@@ -2,15 +2,16 @@ import { equals, iterableEquality, subsetEquality, JestExtend, JestChaiExpect, J
2
2
  import { getCurrentTest } from '@vitest/runner';
3
3
  import { getNames, getTestName } from '@vitest/runner/utils';
4
4
  import * as chai$1 from 'chai';
5
- import { g as getWorkerState, a as getCurrentEnvironment, i as isChildProcess, w as waitForImportsToResolve, r as resetModules } from './utils.CgTj3MsC.js';
5
+ import { g as getWorkerState, a as getCurrentEnvironment, i as isChildProcess, w as waitForImportsToResolve, r as resetModules } from './utils.XdZDrNZV.js';
6
6
  import { getSafeTimers, assertTypes, createSimpleStackTrace } from '@vitest/utils';
7
7
  import { g as getDefaultExportFromCjs, c as commonjsGlobal } from './_commonjsHelpers.BFTU3MAI.js';
8
8
  import { stripSnapshotIndentation, addSerializer, SnapshotClient } from '@vitest/snapshot';
9
9
  import '@vitest/utils/error';
10
10
  import { fn, spyOn, mocks, isMockFunction } from '@vitest/spy';
11
11
  import { parseSingleStack } from '@vitest/utils/source-map';
12
- import { R as RealDate, r as resetDate, m as mockDate } from './date.CDOsz-HY.js';
12
+ import { R as RealDate, r as resetDate, m as mockDate } from './date.Bq6ZW5rf.js';
13
13
 
14
+ // these matchers are not supported because they don't make sense with poll
14
15
  const unsupported = [
15
16
  "matchSnapshot",
16
17
  "toMatchSnapshot",
@@ -28,23 +29,16 @@ function createExpectPoll(expect) {
28
29
  const state = getWorkerState();
29
30
  const defaults = state.config.expect?.poll ?? {};
30
31
  const { interval = defaults.interval ?? 50, timeout = defaults.timeout ?? 1e3, message } = options;
32
+ // @ts-expect-error private poll access
31
33
  const assertion = expect(null, message).withContext({ poll: true });
32
34
  fn = fn.bind(assertion);
33
35
  const test = chai$1.util.flag(assertion, "vitest-test");
34
- if (!test) {
35
- throw new Error("expect.poll() must be called inside a test");
36
- }
36
+ if (!test) throw new Error("expect.poll() must be called inside a test");
37
37
  const proxy = new Proxy(assertion, { get(target, key, receiver) {
38
38
  const assertionFunction = Reflect.get(target, key, receiver);
39
- if (typeof assertionFunction !== "function") {
40
- return assertionFunction instanceof chai$1.Assertion ? proxy : assertionFunction;
41
- }
42
- if (key === "assert") {
43
- return assertionFunction;
44
- }
45
- if (typeof key === "string" && unsupported.includes(key)) {
46
- throw new SyntaxError(`expect.poll() is not supported in combination with .${key}(). Use vi.waitFor() if your assertion condition is unstable.`);
47
- }
39
+ if (typeof assertionFunction !== "function") return assertionFunction instanceof chai$1.Assertion ? proxy : assertionFunction;
40
+ if (key === "assert") return assertionFunction;
41
+ if (typeof key === "string" && unsupported.includes(key)) throw new SyntaxError(`expect.poll() is not supported in combination with .${key}(). Use vi.waitFor() if your assertion condition is unstable.`);
48
42
  return function(...args) {
49
43
  const STACK_TRACE_ERROR = new Error("STACK_TRACE_ERROR");
50
44
  const promise = () => new Promise((resolve, reject) => {
@@ -62,9 +56,7 @@ function createExpectPoll(expect) {
62
56
  clearTimeout(timeoutId);
63
57
  } catch (err) {
64
58
  lastError = err;
65
- if (!chai$1.util.flag(assertion, "_isLastPollAttempt")) {
66
- intervalId = setTimeout(check, interval);
67
- }
59
+ if (!chai$1.util.flag(assertion, "_isLastPollAttempt")) intervalId = setTimeout(check, interval);
68
60
  }
69
61
  };
70
62
  timeoutId = setTimeout(() => {
@@ -89,6 +81,8 @@ function createExpectPoll(expect) {
89
81
  }
90
82
  });
91
83
  let resultPromise;
84
+ // only .then is enough to check awaited, but we type this as `Promise<void>` in global types
85
+ // so let's follow it
92
86
  return {
93
87
  then(onFulfilled, onRejected) {
94
88
  awaited = true;
@@ -108,9 +102,7 @@ function createExpectPoll(expect) {
108
102
  };
109
103
  }
110
104
  function copyStackTrace$1(target, source) {
111
- if (source.stack !== undefined) {
112
- target.stack = source.stack.replace(source.message, target.message);
113
- }
105
+ if (source.stack !== void 0) target.stack = source.stack.replace(source.message, target.message);
114
106
  return target;
115
107
  }
116
108
 
@@ -222,19 +214,16 @@ function createAssertionMessage(util, assertion, hasArgs) {
222
214
  }
223
215
  function recordAsyncExpect(_test, promise, assertion, error) {
224
216
  const test = _test;
217
+ // record promise for test, that resolves before test ends
225
218
  if (test && promise instanceof Promise) {
219
+ // if promise is explicitly awaited, remove it from the list
226
220
  promise = promise.finally(() => {
227
- if (!test.promises) {
228
- return;
229
- }
221
+ if (!test.promises) return;
230
222
  const index = test.promises.indexOf(promise);
231
- if (index !== -1) {
232
- test.promises.splice(index, 1);
233
- }
223
+ if (index !== -1) test.promises.splice(index, 1);
234
224
  });
235
- if (!test.promises) {
236
- test.promises = [];
237
- }
225
+ // record promise
226
+ if (!test.promises) test.promises = [];
238
227
  test.promises.push(promise);
239
228
  let resolved = false;
240
229
  test.onFinished ??= [];
@@ -269,18 +258,15 @@ function recordAsyncExpect(_test, promise, assertion, error) {
269
258
 
270
259
  let _client;
271
260
  function getSnapshotClient() {
272
- if (!_client) {
273
- _client = new SnapshotClient({ isEqual: (received, expected) => {
274
- return equals(received, expected, [iterableEquality, subsetEquality]);
275
- } });
276
- }
261
+ if (!_client) _client = new SnapshotClient({ isEqual: (received, expected) => {
262
+ return equals(received, expected, [iterableEquality, subsetEquality]);
263
+ } });
277
264
  return _client;
278
265
  }
279
266
  function getError(expected, promise) {
280
267
  if (typeof expected !== "function") {
281
- if (!promise) {
282
- throw new Error(`expected must be a function, received ${typeof expected}`);
283
- }
268
+ if (!promise) throw new Error(`expected must be a function, received ${typeof expected}`);
269
+ // when "promised", it receives thrown error
284
270
  return expected;
285
271
  }
286
272
  try {
@@ -300,41 +286,33 @@ function getTestNames(test) {
300
286
  const SnapshotPlugin = (chai, utils) => {
301
287
  function getTest(assertionName, obj) {
302
288
  const test = utils.flag(obj, "vitest-test");
303
- if (!test) {
304
- throw new Error(`'${assertionName}' cannot be used without test context`);
305
- }
289
+ if (!test) throw new Error(`'${assertionName}' cannot be used without test context`);
306
290
  return test;
307
291
  }
308
- for (const key of ["matchSnapshot", "toMatchSnapshot"]) {
309
- utils.addMethod(chai.Assertion.prototype, key, function(properties, message) {
310
- utils.flag(this, "_name", key);
311
- const isNot = utils.flag(this, "negate");
312
- if (isNot) {
313
- throw new Error(`${key} cannot be used with "not"`);
314
- }
315
- const expected = utils.flag(this, "object");
316
- const test = getTest(key, this);
317
- if (typeof properties === "string" && typeof message === "undefined") {
318
- message = properties;
319
- properties = undefined;
320
- }
321
- const errorMessage = utils.flag(this, "message");
322
- getSnapshotClient().assert({
323
- received: expected,
324
- message,
325
- isInline: false,
326
- properties,
327
- errorMessage,
328
- ...getTestNames(test)
329
- });
292
+ for (const key of ["matchSnapshot", "toMatchSnapshot"]) utils.addMethod(chai.Assertion.prototype, key, function(properties, message) {
293
+ utils.flag(this, "_name", key);
294
+ const isNot = utils.flag(this, "negate");
295
+ if (isNot) throw new Error(`${key} cannot be used with "not"`);
296
+ const expected = utils.flag(this, "object");
297
+ const test = getTest(key, this);
298
+ if (typeof properties === "string" && typeof message === "undefined") {
299
+ message = properties;
300
+ properties = void 0;
301
+ }
302
+ const errorMessage = utils.flag(this, "message");
303
+ getSnapshotClient().assert({
304
+ received: expected,
305
+ message,
306
+ isInline: false,
307
+ properties,
308
+ errorMessage,
309
+ ...getTestNames(test)
330
310
  });
331
- }
311
+ });
332
312
  utils.addMethod(chai.Assertion.prototype, "toMatchFileSnapshot", function(file, message) {
333
313
  utils.flag(this, "_name", "toMatchFileSnapshot");
334
314
  const isNot = utils.flag(this, "negate");
335
- if (isNot) {
336
- throw new Error("toMatchFileSnapshot cannot be used with \"not\"");
337
- }
315
+ if (isNot) throw new Error("toMatchFileSnapshot cannot be used with \"not\"");
338
316
  const error = new Error("resolves");
339
317
  const expected = utils.flag(this, "object");
340
318
  const test = getTest("toMatchFileSnapshot", this);
@@ -352,24 +330,18 @@ const SnapshotPlugin = (chai, utils) => {
352
330
  utils.addMethod(chai.Assertion.prototype, "toMatchInlineSnapshot", function __INLINE_SNAPSHOT__(properties, inlineSnapshot, message) {
353
331
  utils.flag(this, "_name", "toMatchInlineSnapshot");
354
332
  const isNot = utils.flag(this, "negate");
355
- if (isNot) {
356
- throw new Error("toMatchInlineSnapshot cannot be used with \"not\"");
357
- }
333
+ if (isNot) throw new Error("toMatchInlineSnapshot cannot be used with \"not\"");
358
334
  const test = getTest("toMatchInlineSnapshot", this);
359
335
  const isInsideEach = test.each || test.suite?.each;
360
- if (isInsideEach) {
361
- throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
362
- }
336
+ if (isInsideEach) throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
363
337
  const expected = utils.flag(this, "object");
364
338
  const error = utils.flag(this, "error");
365
339
  if (typeof properties === "string") {
366
340
  message = inlineSnapshot;
367
341
  inlineSnapshot = properties;
368
- properties = undefined;
369
- }
370
- if (inlineSnapshot) {
371
- inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
342
+ properties = void 0;
372
343
  }
344
+ if (inlineSnapshot) inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
373
345
  const errorMessage = utils.flag(this, "message");
374
346
  getSnapshotClient().assert({
375
347
  received: expected,
@@ -385,9 +357,7 @@ const SnapshotPlugin = (chai, utils) => {
385
357
  utils.addMethod(chai.Assertion.prototype, "toThrowErrorMatchingSnapshot", function(message) {
386
358
  utils.flag(this, "_name", "toThrowErrorMatchingSnapshot");
387
359
  const isNot = utils.flag(this, "negate");
388
- if (isNot) {
389
- throw new Error("toThrowErrorMatchingSnapshot cannot be used with \"not\"");
390
- }
360
+ if (isNot) throw new Error("toThrowErrorMatchingSnapshot cannot be used with \"not\"");
391
361
  const expected = utils.flag(this, "object");
392
362
  const test = getTest("toThrowErrorMatchingSnapshot", this);
393
363
  const promise = utils.flag(this, "promise");
@@ -401,21 +371,15 @@ const SnapshotPlugin = (chai, utils) => {
401
371
  });
402
372
  utils.addMethod(chai.Assertion.prototype, "toThrowErrorMatchingInlineSnapshot", function __INLINE_SNAPSHOT__(inlineSnapshot, message) {
403
373
  const isNot = utils.flag(this, "negate");
404
- if (isNot) {
405
- throw new Error("toThrowErrorMatchingInlineSnapshot cannot be used with \"not\"");
406
- }
374
+ if (isNot) throw new Error("toThrowErrorMatchingInlineSnapshot cannot be used with \"not\"");
407
375
  const test = getTest("toThrowErrorMatchingInlineSnapshot", this);
408
376
  const isInsideEach = test.each || test.suite?.each;
409
- if (isInsideEach) {
410
- throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
411
- }
377
+ if (isInsideEach) throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
412
378
  const expected = utils.flag(this, "object");
413
379
  const error = utils.flag(this, "error");
414
380
  const promise = utils.flag(this, "promise");
415
381
  const errorMessage = utils.flag(this, "message");
416
- if (inlineSnapshot) {
417
- inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
418
- }
382
+ if (inlineSnapshot) inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
419
383
  getSnapshotClient().assert({
420
384
  received: getError(expected, promise),
421
385
  message,
@@ -441,16 +405,16 @@ function createExpect(test) {
441
405
  setState({ assertionCalls: assertionCalls + 1 }, expect);
442
406
  const assert = chai$1.expect(value, message);
443
407
  const _test = test || getCurrentTest();
444
- if (_test) {
445
- return assert.withTest(_test);
446
- } else {
447
- return assert;
448
- }
408
+ if (_test)
409
+ // @ts-expect-error internal
410
+ return assert.withTest(_test);
411
+ else return assert;
449
412
  };
450
413
  Object.assign(expect, chai$1.expect);
451
414
  Object.assign(expect, globalThis[ASYMMETRIC_MATCHERS_OBJECT]);
452
415
  expect.getState = () => getState(expect);
453
416
  expect.setState = (state) => setState(state, expect);
417
+ // @ts-expect-error global is not typed
454
418
  const globalState = getState(globalThis[GLOBAL_EXPECT]) || {};
455
419
  setState({
456
420
  ...globalState,
@@ -465,9 +429,11 @@ function createExpect(test) {
465
429
  },
466
430
  currentTestName: test ? getTestName(test) : globalState.currentTestName
467
431
  }, expect);
432
+ // @ts-expect-error untyped
468
433
  expect.extend = (matchers) => chai$1.expect.extend(expect, matchers);
469
434
  expect.addEqualityTesters = (customTesters) => addCustomEqualityTesters(customTesters);
470
435
  expect.soft = (...args) => {
436
+ // @ts-expect-error private soft access
471
437
  return expect(...args).withContext({ soft: true });
472
438
  };
473
439
  expect.poll = createExpectPoll(expect);
@@ -476,9 +442,7 @@ function createExpect(test) {
476
442
  };
477
443
  function assertions(expected) {
478
444
  const errorGen = () => new Error(`expected number of assertions to be ${expected}, but got ${expect.getState().assertionCalls}`);
479
- if (Error.captureStackTrace) {
480
- Error.captureStackTrace(errorGen(), assertions);
481
- }
445
+ if (Error.captureStackTrace) Error.captureStackTrace(errorGen(), assertions);
482
446
  expect.setState({
483
447
  expectedAssertionsNumber: expected,
484
448
  expectedAssertionsNumberErrorGen: errorGen
@@ -486,9 +450,7 @@ function createExpect(test) {
486
450
  }
487
451
  function hasAssertions() {
488
452
  const error = new Error("expected any number of assertion, but got none");
489
- if (Error.captureStackTrace) {
490
- Error.captureStackTrace(error, hasAssertions);
491
- }
453
+ if (Error.captureStackTrace) Error.captureStackTrace(error, hasAssertions);
492
454
  expect.setState({
493
455
  isExpectingAssertions: true,
494
456
  isExpectingAssertionsError: error
@@ -3611,6 +3573,12 @@ var fakeTimersSrcExports = requireFakeTimersSrc();
3611
3573
  class FakeTimers {
3612
3574
  _global;
3613
3575
  _clock;
3576
+ // | _fakingTime | _fakingDate |
3577
+ // +-------------+-------------+
3578
+ // | false | falsy | initial
3579
+ // | false | truthy | vi.setSystemTime called first (for mocking only Date without fake timers)
3580
+ // | true | falsy | vi.useFakeTimers called first
3581
+ // | true | truthy | unreachable
3614
3582
  _fakingTime;
3615
3583
  _fakingDate;
3616
3584
  _fakeTimers;
@@ -3624,74 +3592,52 @@ class FakeTimers {
3624
3592
  this._global = global;
3625
3593
  }
3626
3594
  clearAllTimers() {
3627
- if (this._fakingTime) {
3628
- this._clock.reset();
3629
- }
3595
+ if (this._fakingTime) this._clock.reset();
3630
3596
  }
3631
3597
  dispose() {
3632
3598
  this.useRealTimers();
3633
3599
  }
3634
3600
  runAllTimers() {
3635
- if (this._checkFakeTimers()) {
3636
- this._clock.runAll();
3637
- }
3601
+ if (this._checkFakeTimers()) this._clock.runAll();
3638
3602
  }
3639
3603
  async runAllTimersAsync() {
3640
- if (this._checkFakeTimers()) {
3641
- await this._clock.runAllAsync();
3642
- }
3604
+ if (this._checkFakeTimers()) await this._clock.runAllAsync();
3643
3605
  }
3644
3606
  runOnlyPendingTimers() {
3645
- if (this._checkFakeTimers()) {
3646
- this._clock.runToLast();
3647
- }
3607
+ if (this._checkFakeTimers()) this._clock.runToLast();
3648
3608
  }
3649
3609
  async runOnlyPendingTimersAsync() {
3650
- if (this._checkFakeTimers()) {
3651
- await this._clock.runToLastAsync();
3652
- }
3610
+ if (this._checkFakeTimers()) await this._clock.runToLastAsync();
3653
3611
  }
3654
3612
  advanceTimersToNextTimer(steps = 1) {
3655
- if (this._checkFakeTimers()) {
3656
- for (let i = steps; i > 0; i--) {
3657
- this._clock.next();
3658
- this._clock.tick(0);
3659
- if (this._clock.countTimers() === 0) {
3660
- break;
3661
- }
3662
- }
3613
+ if (this._checkFakeTimers()) for (let i = steps; i > 0; i--) {
3614
+ this._clock.next();
3615
+ // Fire all timers at this point: https://github.com/sinonjs/fake-timers/issues/250
3616
+ this._clock.tick(0);
3617
+ if (this._clock.countTimers() === 0) break;
3663
3618
  }
3664
3619
  }
3665
3620
  async advanceTimersToNextTimerAsync(steps = 1) {
3666
- if (this._checkFakeTimers()) {
3667
- for (let i = steps; i > 0; i--) {
3668
- await this._clock.nextAsync();
3669
- this._clock.tick(0);
3670
- if (this._clock.countTimers() === 0) {
3671
- break;
3672
- }
3673
- }
3621
+ if (this._checkFakeTimers()) for (let i = steps; i > 0; i--) {
3622
+ await this._clock.nextAsync();
3623
+ // Fire all timers at this point: https://github.com/sinonjs/fake-timers/issues/250
3624
+ this._clock.tick(0);
3625
+ if (this._clock.countTimers() === 0) break;
3674
3626
  }
3675
3627
  }
3676
3628
  advanceTimersByTime(msToRun) {
3677
- if (this._checkFakeTimers()) {
3678
- this._clock.tick(msToRun);
3679
- }
3629
+ if (this._checkFakeTimers()) this._clock.tick(msToRun);
3680
3630
  }
3681
3631
  async advanceTimersByTimeAsync(msToRun) {
3682
- if (this._checkFakeTimers()) {
3683
- await this._clock.tickAsync(msToRun);
3684
- }
3632
+ if (this._checkFakeTimers()) await this._clock.tickAsync(msToRun);
3685
3633
  }
3686
3634
  advanceTimersToNextFrame() {
3687
- if (this._checkFakeTimers()) {
3688
- this._clock.runToFrame();
3689
- }
3635
+ if (this._checkFakeTimers()) this._clock.runToFrame();
3690
3636
  }
3691
3637
  runAllTicks() {
3692
- if (this._checkFakeTimers()) {
3693
- this._clock.runMicrotasks();
3694
- }
3638
+ if (this._checkFakeTimers())
3639
+ // @ts-expect-error method not exposed
3640
+ this._clock.runMicrotasks();
3695
3641
  }
3696
3642
  useRealTimers() {
3697
3643
  if (this._fakingDate) {
@@ -3704,14 +3650,10 @@ class FakeTimers {
3704
3650
  }
3705
3651
  }
3706
3652
  useFakeTimers() {
3707
- if (this._fakingDate) {
3708
- throw new Error("\"setSystemTime\" was called already and date was mocked. Reset timers using `vi.useRealTimers()` if you want to use fake timers again.");
3709
- }
3653
+ if (this._fakingDate) throw new Error("\"setSystemTime\" was called already and date was mocked. Reset timers using `vi.useRealTimers()` if you want to use fake timers again.");
3710
3654
  if (!this._fakingTime) {
3711
3655
  const toFake = Object.keys(this._fakeTimers.timers).filter((timer) => timer !== "nextTick" && timer !== "queueMicrotask");
3712
- if (this._userConfig?.toFake?.includes("nextTick") && isChildProcess()) {
3713
- throw new Error("process.nextTick cannot be mocked inside child_process");
3714
- }
3656
+ if (this._userConfig?.toFake?.includes("nextTick") && isChildProcess()) throw new Error("process.nextTick cannot be mocked inside child_process");
3715
3657
  this._clock = this._fakeTimers.install({
3716
3658
  now: Date.now(),
3717
3659
  ...this._userConfig,
@@ -3730,9 +3672,8 @@ class FakeTimers {
3730
3672
  }
3731
3673
  setSystemTime(now) {
3732
3674
  const date = typeof now === "undefined" || now instanceof Date ? now : new Date(now);
3733
- if (this._fakingTime) {
3734
- this._clock.setSystemTime(date);
3735
- } else {
3675
+ if (this._fakingTime) this._clock.setSystemTime(date);
3676
+ else {
3736
3677
  this._fakingDate = date ?? new Date(this.getRealSystemTime());
3737
3678
  mockDate(this._fakingDate);
3738
3679
  }
@@ -3744,9 +3685,7 @@ class FakeTimers {
3744
3685
  return this._now();
3745
3686
  }
3746
3687
  getTimerCount() {
3747
- if (this._checkFakeTimers()) {
3748
- return this._clock.countTimers();
3749
- }
3688
+ if (this._checkFakeTimers()) return this._clock.countTimers();
3750
3689
  return 0;
3751
3690
  }
3752
3691
  configure(config) {
@@ -3756,17 +3695,13 @@ class FakeTimers {
3756
3695
  return this._fakingTime;
3757
3696
  }
3758
3697
  _checkFakeTimers() {
3759
- if (!this._fakingTime) {
3760
- throw new Error("Timers are not mocked. Try calling \"vi.useFakeTimers()\" first.");
3761
- }
3698
+ if (!this._fakingTime) throw new Error("Timers are not mocked. Try calling \"vi.useFakeTimers()\" first.");
3762
3699
  return this._fakingTime;
3763
3700
  }
3764
3701
  }
3765
3702
 
3766
3703
  function copyStackTrace(target, source) {
3767
- if (source.stack !== undefined) {
3768
- target.stack = source.stack.replace(source.message, target.message);
3769
- }
3704
+ if (source.stack !== void 0) target.stack = source.stack.replace(source.message, target.message);
3770
3705
  return target;
3771
3706
  }
3772
3707
  function waitFor(callback, options = {}) {
@@ -3779,31 +3714,19 @@ function waitFor(callback, options = {}) {
3779
3714
  let timeoutId;
3780
3715
  let intervalId;
3781
3716
  const onResolve = (result) => {
3782
- if (timeoutId) {
3783
- clearTimeout(timeoutId);
3784
- }
3785
- if (intervalId) {
3786
- clearInterval(intervalId);
3787
- }
3717
+ if (timeoutId) clearTimeout(timeoutId);
3718
+ if (intervalId) clearInterval(intervalId);
3788
3719
  resolve(result);
3789
3720
  };
3790
3721
  const handleTimeout = () => {
3791
- if (intervalId) {
3792
- clearInterval(intervalId);
3793
- }
3722
+ if (intervalId) clearInterval(intervalId);
3794
3723
  let error = lastError;
3795
- if (!error) {
3796
- error = copyStackTrace(new Error("Timed out in waitFor!"), STACK_TRACE_ERROR);
3797
- }
3724
+ if (!error) error = copyStackTrace(new Error("Timed out in waitFor!"), STACK_TRACE_ERROR);
3798
3725
  reject(error);
3799
3726
  };
3800
3727
  const checkCallback = () => {
3801
- if (vi.isFakeTimers()) {
3802
- vi.advanceTimersByTime(interval);
3803
- }
3804
- if (promiseStatus === "pending") {
3805
- return;
3806
- }
3728
+ if (vi.isFakeTimers()) vi.advanceTimersByTime(interval);
3729
+ if (promiseStatus === "pending") return;
3807
3730
  try {
3808
3731
  const result = callback();
3809
3732
  if (result !== null && typeof result === "object" && typeof result.then === "function") {
@@ -3824,9 +3747,7 @@ function waitFor(callback, options = {}) {
3824
3747
  lastError = error;
3825
3748
  }
3826
3749
  };
3827
- if (checkCallback() === true) {
3828
- return;
3829
- }
3750
+ if (checkCallback() === true) return;
3830
3751
  timeoutId = setTimeout(handleTimeout, timeout);
3831
3752
  intervalId = setInterval(checkCallback, interval);
3832
3753
  });
@@ -3840,34 +3761,20 @@ function waitUntil(callback, options = {}) {
3840
3761
  let timeoutId;
3841
3762
  let intervalId;
3842
3763
  const onReject = (error) => {
3843
- if (intervalId) {
3844
- clearInterval(intervalId);
3845
- }
3846
- if (!error) {
3847
- error = copyStackTrace(new Error("Timed out in waitUntil!"), STACK_TRACE_ERROR);
3848
- }
3764
+ if (intervalId) clearInterval(intervalId);
3765
+ if (!error) error = copyStackTrace(new Error("Timed out in waitUntil!"), STACK_TRACE_ERROR);
3849
3766
  reject(error);
3850
3767
  };
3851
3768
  const onResolve = (result) => {
3852
- if (!result) {
3853
- return;
3854
- }
3855
- if (timeoutId) {
3856
- clearTimeout(timeoutId);
3857
- }
3858
- if (intervalId) {
3859
- clearInterval(intervalId);
3860
- }
3769
+ if (!result) return;
3770
+ if (timeoutId) clearTimeout(timeoutId);
3771
+ if (intervalId) clearInterval(intervalId);
3861
3772
  resolve(result);
3862
3773
  return true;
3863
3774
  };
3864
3775
  const checkCallback = () => {
3865
- if (vi.isFakeTimers()) {
3866
- vi.advanceTimersByTime(interval);
3867
- }
3868
- if (promiseStatus === "pending") {
3869
- return;
3870
- }
3776
+ if (vi.isFakeTimers()) vi.advanceTimersByTime(interval);
3777
+ if (promiseStatus === "pending") return;
3871
3778
  try {
3872
3779
  const result = callback();
3873
3780
  if (result !== null && typeof result === "object" && typeof result.then === "function") {
@@ -3880,16 +3787,12 @@ function waitUntil(callback, options = {}) {
3880
3787
  promiseStatus = "rejected";
3881
3788
  onReject(rejectedValue);
3882
3789
  });
3883
- } else {
3884
- return onResolve(result);
3885
- }
3790
+ } else return onResolve(result);
3886
3791
  } catch (error) {
3887
3792
  onReject(error);
3888
3793
  }
3889
3794
  };
3890
- if (checkCallback() === true) {
3891
- return;
3892
- }
3795
+ if (checkCallback() === true) return;
3893
3796
  timeoutId = setTimeout(onReject, timeout);
3894
3797
  intervalId = setInterval(checkCallback, interval);
3895
3798
  });
@@ -3903,8 +3806,8 @@ function createVitest() {
3903
3806
  global: globalThis,
3904
3807
  config: workerState.config.fakeTimers
3905
3808
  });
3906
- const _stubsGlobal = new Map();
3907
- const _stubsEnv = new Map();
3809
+ const _stubsGlobal = /* @__PURE__ */ new Map();
3810
+ const _stubsEnv = /* @__PURE__ */ new Map();
3908
3811
  const _envBooleans = [
3909
3812
  "PROD",
3910
3813
  "DEV",
@@ -3913,18 +3816,13 @@ function createVitest() {
3913
3816
  const utils = {
3914
3817
  useFakeTimers(config) {
3915
3818
  if (isChildProcess()) {
3916
- if (config?.toFake?.includes("nextTick") || workerState.config?.fakeTimers?.toFake?.includes("nextTick")) {
3917
- throw new Error("vi.useFakeTimers({ toFake: [\"nextTick\"] }) is not supported in node:child_process. Use --pool=threads if mocking nextTick is required.");
3918
- }
3919
- }
3920
- if (config) {
3921
- timers().configure({
3922
- ...workerState.config.fakeTimers,
3923
- ...config
3924
- });
3925
- } else {
3926
- timers().configure(workerState.config.fakeTimers);
3819
+ if (config?.toFake?.includes("nextTick") || workerState.config?.fakeTimers?.toFake?.includes("nextTick")) throw new Error("vi.useFakeTimers({ toFake: [\"nextTick\"] }) is not supported in node:child_process. Use --pool=threads if mocking nextTick is required.");
3927
3820
  }
3821
+ if (config) timers().configure({
3822
+ ...workerState.config.fakeTimers,
3823
+ ...config
3824
+ });
3825
+ else timers().configure(workerState.config.fakeTimers);
3928
3826
  timers().useFakeTimers();
3929
3827
  return utils;
3930
3828
  },
@@ -4001,29 +3899,21 @@ function createVitest() {
4001
3899
  return factory();
4002
3900
  },
4003
3901
  mock(path, factory) {
4004
- if (typeof path !== "string") {
4005
- throw new TypeError(`vi.mock() expects a string path, but received a ${typeof path}`);
4006
- }
3902
+ if (typeof path !== "string") throw new TypeError(`vi.mock() expects a string path, but received a ${typeof path}`);
4007
3903
  const importer = getImporter("mock");
4008
3904
  _mocker().queueMock(path, importer, typeof factory === "function" ? () => factory(() => _mocker().importActual(path, importer, _mocker().getMockContext().callstack)) : factory);
4009
3905
  },
4010
3906
  unmock(path) {
4011
- if (typeof path !== "string") {
4012
- throw new TypeError(`vi.unmock() expects a string path, but received a ${typeof path}`);
4013
- }
3907
+ if (typeof path !== "string") throw new TypeError(`vi.unmock() expects a string path, but received a ${typeof path}`);
4014
3908
  _mocker().queueUnmock(path, getImporter("unmock"));
4015
3909
  },
4016
3910
  doMock(path, factory) {
4017
- if (typeof path !== "string") {
4018
- throw new TypeError(`vi.doMock() expects a string path, but received a ${typeof path}`);
4019
- }
3911
+ if (typeof path !== "string") throw new TypeError(`vi.doMock() expects a string path, but received a ${typeof path}`);
4020
3912
  const importer = getImporter("doMock");
4021
3913
  _mocker().queueMock(path, importer, typeof factory === "function" ? () => factory(() => _mocker().importActual(path, importer, _mocker().getMockContext().callstack)) : factory);
4022
3914
  },
4023
3915
  doUnmock(path) {
4024
- if (typeof path !== "string") {
4025
- throw new TypeError(`vi.doUnmock() expects a string path, but received a ${typeof path}`);
4026
- }
3916
+ if (typeof path !== "string") throw new TypeError(`vi.doUnmock() expects a string path, but received a ${typeof path}`);
4027
3917
  _mocker().queueUnmock(path, getImporter("doUnmock"));
4028
3918
  },
4029
3919
  async importActual(path) {
@@ -4054,9 +3944,7 @@ function createVitest() {
4054
3944
  return utils;
4055
3945
  },
4056
3946
  stubGlobal(name, value) {
4057
- if (!_stubsGlobal.has(name)) {
4058
- _stubsGlobal.set(name, Object.getOwnPropertyDescriptor(globalThis, name));
4059
- }
3947
+ if (!_stubsGlobal.has(name)) _stubsGlobal.set(name, Object.getOwnPropertyDescriptor(globalThis, name));
4060
3948
  Object.defineProperty(globalThis, name, {
4061
3949
  value,
4062
3950
  writable: true,
@@ -4066,36 +3954,24 @@ function createVitest() {
4066
3954
  return utils;
4067
3955
  },
4068
3956
  stubEnv(name, value) {
4069
- if (!_stubsEnv.has(name)) {
4070
- _stubsEnv.set(name, process.env[name]);
4071
- }
4072
- if (_envBooleans.includes(name)) {
4073
- process.env[name] = value ? "1" : "";
4074
- } else if (value === undefined) {
4075
- delete process.env[name];
4076
- } else {
4077
- process.env[name] = String(value);
4078
- }
3957
+ if (!_stubsEnv.has(name)) _stubsEnv.set(name, process.env[name]);
3958
+ if (_envBooleans.includes(name)) process.env[name] = value ? "1" : "";
3959
+ else if (value === void 0) delete process.env[name];
3960
+ else process.env[name] = String(value);
4079
3961
  return utils;
4080
3962
  },
4081
3963
  unstubAllGlobals() {
4082
3964
  _stubsGlobal.forEach((original, name) => {
4083
- if (!original) {
4084
- Reflect.deleteProperty(globalThis, name);
4085
- } else {
4086
- Object.defineProperty(globalThis, name, original);
4087
- }
3965
+ if (!original) Reflect.deleteProperty(globalThis, name);
3966
+ else Object.defineProperty(globalThis, name, original);
4088
3967
  });
4089
3968
  _stubsGlobal.clear();
4090
3969
  return utils;
4091
3970
  },
4092
3971
  unstubAllEnvs() {
4093
3972
  _stubsEnv.forEach((original, name) => {
4094
- if (original === undefined) {
4095
- delete process.env[name];
4096
- } else {
4097
- process.env[name] = original;
4098
- }
3973
+ if (original === void 0) delete process.env[name];
3974
+ else process.env[name] = original;
4099
3975
  });
4100
3976
  _stubsEnv.clear();
4101
3977
  return utils;
@@ -4108,15 +3984,11 @@ function createVitest() {
4108
3984
  return waitForImportsToResolve();
4109
3985
  },
4110
3986
  setConfig(config) {
4111
- if (!_config) {
4112
- _config = { ...workerState.config };
4113
- }
3987
+ if (!_config) _config = { ...workerState.config };
4114
3988
  Object.assign(workerState.config, config);
4115
3989
  },
4116
3990
  resetConfig() {
4117
- if (_config) {
4118
- Object.assign(workerState.config, _config);
4119
- }
3991
+ if (_config) Object.assign(workerState.config, _config);
4120
3992
  }
4121
3993
  };
4122
3994
  return utils;
@@ -4124,13 +3996,15 @@ function createVitest() {
4124
3996
  const vitest = createVitest();
4125
3997
  const vi = vitest;
4126
3998
  function _mocker() {
3999
+ // @ts-expect-error injected by vite-nide
4127
4000
  return typeof __vitest_mocker__ !== "undefined" ? __vitest_mocker__ : new Proxy({}, { get(_, name) {
4128
- throw new Error("Vitest mocker was not initialized in this environment. " + `vi.${String(name)}() is forbidden.`);
4001
+ throw new Error(`Vitest mocker was not initialized in this environment. vi.${String(name)}() is forbidden.`);
4129
4002
  } });
4130
4003
  }
4131
4004
  function getImporter(name) {
4132
4005
  const stackTrace = createSimpleStackTrace({ stackTraceLimit: 5 });
4133
4006
  const stackArray = stackTrace.split("\n");
4007
+ // if there is no message in a stack trace, use the item - 1
4134
4008
  const importerStackIndex = stackArray.findIndex((stack) => {
4135
4009
  return stack.includes(` at Object.${name}`) || stack.includes(`${name}@`);
4136
4010
  });