vitest 2.0.0-beta.1 → 2.0.0-beta.11

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 (66) hide show
  1. package/LICENSE.md +1 -1
  2. package/dist/browser.d.ts +22 -2
  3. package/dist/browser.js +5 -3
  4. package/dist/chunks/{integrations-globals.B5Jl0grA.js → integrations-globals.CC2ed6Py.js} +9 -9
  5. package/dist/chunks/{node-git.CCI8evVZ.js → node-git.ZtkbKc8u.js} +14 -15
  6. package/dist/chunks/{runtime-console.CUES-L8X.js → runtime-console.Ckl0vEQr.js} +76 -31
  7. package/dist/chunks/{runtime-runBaseTests._dXkRAZc.js → runtime-runBaseTests.BXW_BJeO.js} +40 -32
  8. package/dist/cli.js +3 -3
  9. package/dist/config.cjs +43 -20
  10. package/dist/config.d.ts +2 -2
  11. package/dist/config.js +43 -21
  12. package/dist/coverage.d.ts +4 -4
  13. package/dist/coverage.js +125 -37
  14. package/dist/environments.d.ts +1 -1
  15. package/dist/environments.js +1 -1
  16. package/dist/execute.d.ts +2 -2
  17. package/dist/execute.js +2 -2
  18. package/dist/index.d.ts +20 -30
  19. package/dist/index.js +9 -9
  20. package/dist/node.d.ts +22 -9
  21. package/dist/node.js +25 -15
  22. package/dist/path.js +4 -1
  23. package/dist/{reporters-MGvT5U9f.d.ts → reporters-fiIq_dT9.d.ts} +310 -148
  24. package/dist/reporters.d.ts +1 -1
  25. package/dist/reporters.js +8 -8
  26. package/dist/runners.d.ts +4 -2
  27. package/dist/runners.js +103 -57
  28. package/dist/{suite-8WAe-urM.d.ts → suite-D4aoU9rI.d.ts} +1 -1
  29. package/dist/suite.d.ts +2 -2
  30. package/dist/suite.js +2 -2
  31. package/dist/utils.d.ts +1 -5
  32. package/dist/utils.js +1 -6
  33. package/dist/vendor/{base.VFkIJ66g.js → base.C2DbLEfT.js} +4 -3
  34. package/dist/vendor/{base._gnK9Slw.js → base.CTYV4Gnz.js} +24 -17
  35. package/dist/vendor/{benchmark.BNLebNi5.js → benchmark.CMp8QfyL.js} +13 -14
  36. package/dist/vendor/{cac.DzKZaJu2.js → cac.BcJW7n2j.js} +127 -51
  37. package/dist/vendor/{cli-api.DTeni0Qq.js → cli-api.C8t8m4__.js} +2771 -1711
  38. package/dist/vendor/{constants.5SOfHUj0.js → constants.BWsVtsAj.js} +6 -22
  39. package/dist/vendor/{coverage.ChSqD-qS.js → coverage.BhYSDdTT.js} +27 -11
  40. package/dist/vendor/{date.BKM1wewY.js → date.W2xKR2qe.js} +5 -3
  41. package/dist/vendor/{execute.CLLNVNnK.js → execute.T3gg2ZK6.js} +177 -63
  42. package/dist/vendor/{index.BfoZyXD1.js → index.-dbR4KUi.js} +17 -9
  43. package/dist/vendor/{index.CRxYS9H3.js → index.BC5zhX9y.js} +1201 -491
  44. package/dist/vendor/{index.DP-km6lF.js → index.BMmMjLIQ.js} +64 -51
  45. package/dist/vendor/{index._7XLd8Kd.js → index.C9Thslzw.js} +2 -1
  46. package/dist/vendor/{index.CmILuxzC.js → index.CQJ2m700.js} +3 -3
  47. package/dist/vendor/{index.DeR1hhfY.js → index.D4nqnQWz.js} +71 -74
  48. package/dist/vendor/{rpc.DRDE9Pu1.js → rpc.BGx7q_k2.js} +30 -19
  49. package/dist/vendor/{run-once.DLomgGUH.js → run-once.Db8Hgq9X.js} +2 -1
  50. package/dist/vendor/{setup-common.XeoZAW8t.js → setup-common.uqZOEWuR.js} +30 -15
  51. package/dist/vendor/spy.Cf_4R5Oe.js +22 -0
  52. package/dist/vendor/{tasks.WC7M-K-v.js → tasks.DhVtQBtW.js} +3 -1
  53. package/dist/vendor/{utils.D5gGkwyH.js → utils.DSO2UK15.js} +41 -26
  54. package/dist/vendor/{utils.CUjzkRH7.js → utils.DkxLWvS1.js} +12 -5
  55. package/dist/vendor/{vi.ClD3hi7L.js → vi.BPjl8cAZ.js} +350 -166
  56. package/dist/vendor/{vm.Bi3bljci.js → vm.CycSoHnJ.js} +151 -86
  57. package/dist/worker.js +31 -15
  58. package/dist/workers/forks.js +4 -4
  59. package/dist/workers/runVmTests.js +14 -13
  60. package/dist/workers/threads.js +4 -4
  61. package/dist/workers/vmForks.js +6 -6
  62. package/dist/workers/vmThreads.js +6 -6
  63. package/dist/workers.d.ts +2 -2
  64. package/dist/workers.js +9 -9
  65. package/package.json +22 -22
  66. package/suppress-warnings.cjs +2 -4
@@ -1,16 +1,18 @@
1
1
  import * as chai$1 from 'chai';
2
2
  import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './_commonjsHelpers.BFTU3MAI.js';
3
- import { equals, iterableEquality, subsetEquality, JestExtend, JestChaiExpect, JestAsymmetricMatchers, GLOBAL_EXPECT as GLOBAL_EXPECT$1, ASYMMETRIC_MATCHERS_OBJECT as ASYMMETRIC_MATCHERS_OBJECT$1, getState, setState, addCustomEqualityTesters } from '@vitest/expect';
3
+ import { equals, iterableEquality, subsetEquality, JestExtend, JestChaiExpect, JestAsymmetricMatchers, GLOBAL_EXPECT, ASYMMETRIC_MATCHERS_OBJECT, getState, setState, addCustomEqualityTesters } from '@vitest/expect';
4
4
  import { stripSnapshotIndentation, addSerializer, SnapshotClient } from '@vitest/snapshot';
5
5
  import { getNames } from '@vitest/runner/utils';
6
6
  import '@vitest/utils/error';
7
7
  import { getCurrentTest } from '@vitest/runner';
8
- import { g as getTestName } from './tasks.WC7M-K-v.js';
8
+ import { g as getTestName } from './tasks.DhVtQBtW.js';
9
9
  import { g as getWorkerState, a as getCurrentEnvironment } from './global.7bFbnyXl.js';
10
10
  import { getSafeTimers, assertTypes, createSimpleStackTrace } from '@vitest/utils';
11
+ import 'pathe';
12
+ import './env.bmJgw1qP.js';
11
13
  import { parseSingleStack } from '@vitest/utils/source-map';
12
- import { i as isChildProcess } from './base._gnK9Slw.js';
13
- import { R as RealDate, r as resetDate, m as mockDate } from './date.BKM1wewY.js';
14
+ import { i as isChildProcess } from './base.CTYV4Gnz.js';
15
+ import { R as RealDate, r as resetDate, m as mockDate } from './date.W2xKR2qe.js';
14
16
  import { spyOn, fn, isMockFunction, mocks } from '@vitest/spy';
15
17
 
16
18
  function resetModules(modules, resetMocks = false) {
@@ -26,8 +28,9 @@ function resetModules(modules, resetMocks = false) {
26
28
  ...!resetMocks ? [/^mock:/] : []
27
29
  ];
28
30
  modules.forEach((mod, path) => {
29
- if (skipPaths.some((re) => re.test(path)))
31
+ if (skipPaths.some((re) => re.test(path))) {
30
32
  return;
33
+ }
31
34
  modules.invalidateModule(mod);
32
35
  });
33
36
  }
@@ -41,13 +44,16 @@ async function waitForImportsToResolve() {
41
44
  const promises = [];
42
45
  let resolvingCount = 0;
43
46
  for (const mod of state.moduleCache.values()) {
44
- if (mod.promise && !mod.evaluated)
47
+ if (mod.promise && !mod.evaluated) {
45
48
  promises.push(mod.promise);
46
- if (mod.resolving)
49
+ }
50
+ if (mod.resolving) {
47
51
  resolvingCount++;
52
+ }
48
53
  }
49
- if (!promises.length && !resolvingCount)
54
+ if (!promises.length && !resolvingCount) {
50
55
  return;
56
+ }
51
57
  await Promise.allSettled(promises);
52
58
  await waitForImportsToResolve();
53
59
  }
@@ -142,41 +148,17 @@ var chaiSubset = {exports: {}};
142
148
  var chaiSubsetExports = chaiSubset.exports;
143
149
  var Subset = /*@__PURE__*/getDefaultExportFromCjs(chaiSubsetExports);
144
150
 
145
- const MATCHERS_OBJECT = Symbol.for("matchers-object");
146
- const JEST_MATCHERS_OBJECT = Symbol.for("$$jest-matchers-object");
147
- const GLOBAL_EXPECT = Symbol.for("expect-global");
148
- const ASYMMETRIC_MATCHERS_OBJECT = Symbol.for("asymmetric-matchers-object");
149
-
150
- if (!Object.prototype.hasOwnProperty.call(globalThis, MATCHERS_OBJECT)) {
151
- const globalState = /* @__PURE__ */ new WeakMap();
152
- const matchers = /* @__PURE__ */ Object.create(null);
153
- const customEqualityTesters = [];
154
- const assymetricMatchers = /* @__PURE__ */ Object.create(null);
155
- Object.defineProperty(globalThis, MATCHERS_OBJECT, {
156
- get: () => globalState
157
- });
158
- Object.defineProperty(globalThis, JEST_MATCHERS_OBJECT, {
159
- configurable: true,
160
- get: () => ({
161
- state: globalState.get(globalThis[GLOBAL_EXPECT]),
162
- matchers,
163
- customEqualityTesters
164
- })
165
- });
166
- Object.defineProperty(globalThis, ASYMMETRIC_MATCHERS_OBJECT, {
167
- get: () => assymetricMatchers
168
- });
169
- }
170
-
171
151
  function recordAsyncExpect(test, promise) {
172
152
  if (test && promise instanceof Promise) {
173
153
  promise = promise.finally(() => {
174
154
  const index = test.promises.indexOf(promise);
175
- if (index !== -1)
155
+ if (index !== -1) {
176
156
  test.promises.splice(index, 1);
157
+ }
177
158
  });
178
- if (!test.promises)
159
+ if (!test.promises) {
179
160
  test.promises = [];
161
+ }
180
162
  test.promises.push(promise);
181
163
  }
182
164
  return promise;
@@ -195,8 +177,11 @@ function getSnapshotClient() {
195
177
  }
196
178
  function getError(expected, promise) {
197
179
  if (typeof expected !== "function") {
198
- if (!promise)
199
- throw new Error(`expected must be a function, received ${typeof expected}`);
180
+ if (!promise) {
181
+ throw new Error(
182
+ `expected must be a function, received ${typeof expected}`
183
+ );
184
+ }
200
185
  return expected;
201
186
  }
202
187
  try {
@@ -208,8 +193,9 @@ function getError(expected, promise) {
208
193
  }
209
194
  const SnapshotPlugin = (chai, utils) => {
210
195
  const getTestNames = (test) => {
211
- if (!test)
196
+ if (!test) {
212
197
  return {};
198
+ }
213
199
  return {
214
200
  filepath: test.file.filepath,
215
201
  name: getNames(test).slice(1).join(" > ")
@@ -221,8 +207,9 @@ const SnapshotPlugin = (chai, utils) => {
221
207
  key,
222
208
  function(properties, message) {
223
209
  const isNot = utils.flag(this, "negate");
224
- if (isNot)
210
+ if (isNot) {
225
211
  throw new Error(`${key} cannot be used with "not"`);
212
+ }
226
213
  const expected = utils.flag(this, "object");
227
214
  const test = utils.flag(this, "vitest-test");
228
215
  if (typeof properties === "string" && typeof message === "undefined") {
@@ -246,8 +233,9 @@ const SnapshotPlugin = (chai, utils) => {
246
233
  "toMatchFileSnapshot",
247
234
  function(file, message) {
248
235
  const isNot = utils.flag(this, "negate");
249
- if (isNot)
236
+ if (isNot) {
250
237
  throw new Error('toMatchFileSnapshot cannot be used with "not"');
238
+ }
251
239
  const expected = utils.flag(this, "object");
252
240
  const test = utils.flag(this, "vitest-test");
253
241
  const errorMessage = utils.flag(this, "message");
@@ -270,12 +258,16 @@ const SnapshotPlugin = (chai, utils) => {
270
258
  function __INLINE_SNAPSHOT__(properties, inlineSnapshot, message) {
271
259
  var _a;
272
260
  const isNot = utils.flag(this, "negate");
273
- if (isNot)
261
+ if (isNot) {
274
262
  throw new Error('toMatchInlineSnapshot cannot be used with "not"');
263
+ }
275
264
  const test = utils.flag(this, "vitest-test");
276
265
  const isInsideEach = test && (test.each || ((_a = test.suite) == null ? void 0 : _a.each));
277
- if (isInsideEach)
278
- throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
266
+ if (isInsideEach) {
267
+ throw new Error(
268
+ "InlineSnapshot cannot be used inside of test.each or describe.each"
269
+ );
270
+ }
279
271
  const expected = utils.flag(this, "object");
280
272
  const error = utils.flag(this, "error");
281
273
  if (typeof properties === "string") {
@@ -283,8 +275,9 @@ const SnapshotPlugin = (chai, utils) => {
283
275
  inlineSnapshot = properties;
284
276
  properties = void 0;
285
277
  }
286
- if (inlineSnapshot)
278
+ if (inlineSnapshot) {
287
279
  inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
280
+ }
288
281
  const errorMessage = utils.flag(this, "message");
289
282
  getSnapshotClient().assert({
290
283
  received: expected,
@@ -303,8 +296,11 @@ const SnapshotPlugin = (chai, utils) => {
303
296
  "toThrowErrorMatchingSnapshot",
304
297
  function(message) {
305
298
  const isNot = utils.flag(this, "negate");
306
- if (isNot)
307
- throw new Error('toThrowErrorMatchingSnapshot cannot be used with "not"');
299
+ if (isNot) {
300
+ throw new Error(
301
+ 'toThrowErrorMatchingSnapshot cannot be used with "not"'
302
+ );
303
+ }
308
304
  const expected = utils.flag(this, "object");
309
305
  const test = utils.flag(this, "vitest-test");
310
306
  const promise = utils.flag(this, "promise");
@@ -323,18 +319,25 @@ const SnapshotPlugin = (chai, utils) => {
323
319
  function __INLINE_SNAPSHOT__(inlineSnapshot, message) {
324
320
  var _a;
325
321
  const isNot = utils.flag(this, "negate");
326
- if (isNot)
327
- throw new Error('toThrowErrorMatchingInlineSnapshot cannot be used with "not"');
322
+ if (isNot) {
323
+ throw new Error(
324
+ 'toThrowErrorMatchingInlineSnapshot cannot be used with "not"'
325
+ );
326
+ }
328
327
  const test = utils.flag(this, "vitest-test");
329
328
  const isInsideEach = test && (test.each || ((_a = test.suite) == null ? void 0 : _a.each));
330
- if (isInsideEach)
331
- throw new Error("InlineSnapshot cannot be used inside of test.each or describe.each");
329
+ if (isInsideEach) {
330
+ throw new Error(
331
+ "InlineSnapshot cannot be used inside of test.each or describe.each"
332
+ );
333
+ }
332
334
  const expected = utils.flag(this, "object");
333
335
  const error = utils.flag(this, "error");
334
336
  const promise = utils.flag(this, "promise");
335
337
  const errorMessage = utils.flag(this, "message");
336
- if (inlineSnapshot)
338
+ if (inlineSnapshot) {
337
339
  inlineSnapshot = stripSnapshotIndentation(inlineSnapshot);
340
+ }
338
341
  getSnapshotClient().assert({
339
342
  received: getError(expected, promise),
340
343
  message,
@@ -346,11 +349,7 @@ const SnapshotPlugin = (chai, utils) => {
346
349
  });
347
350
  }
348
351
  );
349
- utils.addMethod(
350
- chai.expect,
351
- "addSnapshotSerializer",
352
- addSerializer
353
- );
352
+ utils.addMethod(chai.expect, "addSnapshotSerializer", addSerializer);
354
353
  };
355
354
 
356
355
  chai$1.use(JestExtend);
@@ -359,51 +358,148 @@ chai$1.use(Subset);
359
358
  chai$1.use(SnapshotPlugin);
360
359
  chai$1.use(JestAsymmetricMatchers);
361
360
 
361
+ const unsupported = [
362
+ // .poll is meant to retry matchers until they succeed, and
363
+ // snapshots will always succeed as long as the poll method doesn't thow an error
364
+ // in this case using the `vi.waitFor` method is more appropriate
365
+ "matchSnapshot",
366
+ "toMatchSnapshot",
367
+ "toMatchInlineSnapshot",
368
+ "toThrowErrorMatchingSnapshot",
369
+ "toThrowErrorMatchingInlineSnapshot",
370
+ // toThrow will never succeed because we call the poll callback until it doesn't throw
371
+ "throws",
372
+ "Throw",
373
+ "throw",
374
+ "toThrow",
375
+ "toThrowError"
376
+ // these are not supported because you can call them without `.poll`,
377
+ // we throw an error inside the rejects/resolves methods to prevent this
378
+ // rejects,
379
+ // resolves
380
+ ];
381
+ function createExpectPoll(expect) {
382
+ return function poll(fn, options = {}) {
383
+ var _a;
384
+ const state = getWorkerState();
385
+ const defaults = ((_a = state.config.expect) == null ? void 0 : _a.poll) ?? {};
386
+ const {
387
+ interval = defaults.interval ?? 50,
388
+ timeout = defaults.timeout ?? 1e3,
389
+ message
390
+ } = options;
391
+ const assertion = expect(null, message).withContext({
392
+ poll: true
393
+ });
394
+ const proxy = new Proxy(assertion, {
395
+ get(target, key, receiver) {
396
+ const assertionFunction = Reflect.get(target, key, receiver);
397
+ if (typeof assertionFunction !== "function") {
398
+ return assertionFunction instanceof chai$1.Assertion ? proxy : assertionFunction;
399
+ }
400
+ if (key === "assert") {
401
+ return assertionFunction;
402
+ }
403
+ if (typeof key === "string" && unsupported.includes(key)) {
404
+ throw new SyntaxError(
405
+ `expect.poll() is not supported in combination with .${key}(). Use vi.waitFor() if your assertion condition is unstable.`
406
+ );
407
+ }
408
+ return function(...args) {
409
+ const STACK_TRACE_ERROR = new Error("STACK_TRACE_ERROR");
410
+ return new Promise((resolve, reject) => {
411
+ let intervalId;
412
+ let lastError;
413
+ const { setTimeout, clearTimeout } = getSafeTimers();
414
+ const timeoutId = setTimeout(() => {
415
+ clearTimeout(intervalId);
416
+ reject(
417
+ copyStackTrace$1(
418
+ new Error(`Matcher did not succeed in ${timeout}ms`, {
419
+ cause: lastError
420
+ }),
421
+ STACK_TRACE_ERROR
422
+ )
423
+ );
424
+ }, timeout);
425
+ const check = async () => {
426
+ try {
427
+ const obj = await fn();
428
+ chai$1.util.flag(assertion, "object", obj);
429
+ resolve(await assertionFunction.call(assertion, ...args));
430
+ clearTimeout(intervalId);
431
+ clearTimeout(timeoutId);
432
+ } catch (err) {
433
+ lastError = err;
434
+ intervalId = setTimeout(check, interval);
435
+ }
436
+ };
437
+ check();
438
+ });
439
+ };
440
+ }
441
+ });
442
+ return proxy;
443
+ };
444
+ }
445
+ function copyStackTrace$1(target, source) {
446
+ if (source.stack !== void 0) {
447
+ target.stack = source.stack.replace(source.message, target.message);
448
+ }
449
+ return target;
450
+ }
451
+
362
452
  function createExpect(test) {
363
453
  const expect = (value, message) => {
364
454
  const { assertionCalls } = getState(expect);
365
- setState({ assertionCalls: assertionCalls + 1, soft: false }, expect);
455
+ setState({ assertionCalls: assertionCalls + 1 }, expect);
366
456
  const assert2 = chai$1.expect(value, message);
367
457
  const _test = test || getCurrentTest();
368
- if (_test)
458
+ if (_test) {
369
459
  return assert2.withTest(_test);
370
- else
460
+ } else {
371
461
  return assert2;
462
+ }
372
463
  };
373
464
  Object.assign(expect, chai$1.expect);
374
- Object.assign(expect, globalThis[ASYMMETRIC_MATCHERS_OBJECT$1]);
465
+ Object.assign(expect, globalThis[ASYMMETRIC_MATCHERS_OBJECT]);
375
466
  expect.getState = () => getState(expect);
376
467
  expect.setState = (state) => setState(state, expect);
377
- const globalState = getState(globalThis[GLOBAL_EXPECT$1]) || {};
468
+ const globalState = getState(globalThis[GLOBAL_EXPECT]) || {};
378
469
  const testPath = getTestFile(test);
379
- setState({
380
- // this should also add "snapshotState" that is added conditionally
381
- ...globalState,
382
- assertionCalls: 0,
383
- isExpectingAssertions: false,
384
- isExpectingAssertionsError: null,
385
- expectedAssertionsNumber: null,
386
- expectedAssertionsNumberErrorGen: null,
387
- environment: getCurrentEnvironment(),
388
- testPath,
389
- currentTestName: test ? getTestName(test) : globalState.currentTestName
390
- }, expect);
470
+ setState(
471
+ {
472
+ // this should also add "snapshotState" that is added conditionally
473
+ ...globalState,
474
+ assertionCalls: 0,
475
+ isExpectingAssertions: false,
476
+ isExpectingAssertionsError: null,
477
+ expectedAssertionsNumber: null,
478
+ expectedAssertionsNumberErrorGen: null,
479
+ environment: getCurrentEnvironment(),
480
+ testPath,
481
+ currentTestName: test ? getTestName(test) : globalState.currentTestName
482
+ },
483
+ expect
484
+ );
391
485
  expect.extend = (matchers) => chai$1.expect.extend(expect, matchers);
392
486
  expect.addEqualityTesters = (customTesters) => addCustomEqualityTesters(customTesters);
393
487
  expect.soft = (...args) => {
394
- const assert2 = expect(...args);
395
- expect.setState({
396
- soft: true
397
- });
398
- return assert2;
488
+ return expect(...args).withContext({ soft: true });
399
489
  };
490
+ expect.poll = createExpectPoll(expect);
400
491
  expect.unreachable = (message) => {
401
- chai$1.assert.fail(`expected${message ? ` "${message}" ` : " "}not to be reached`);
492
+ chai$1.assert.fail(
493
+ `expected${message ? ` "${message}" ` : " "}not to be reached`
494
+ );
402
495
  };
403
496
  function assertions(expected) {
404
- const errorGen = () => new Error(`expected number of assertions to be ${expected}, but got ${expect.getState().assertionCalls}`);
405
- if (Error.captureStackTrace)
497
+ const errorGen = () => new Error(
498
+ `expected number of assertions to be ${expected}, but got ${expect.getState().assertionCalls}`
499
+ );
500
+ if (Error.captureStackTrace) {
406
501
  Error.captureStackTrace(errorGen(), assertions);
502
+ }
407
503
  expect.setState({
408
504
  expectedAssertionsNumber: expected,
409
505
  expectedAssertionsNumberErrorGen: errorGen
@@ -411,8 +507,9 @@ function createExpect(test) {
411
507
  }
412
508
  function hasAssertions() {
413
509
  const error = new Error("expected any number of assertion, but got none");
414
- if (Error.captureStackTrace)
510
+ if (Error.captureStackTrace) {
415
511
  Error.captureStackTrace(error, hasAssertions);
512
+ }
416
513
  expect.setState({
417
514
  isExpectingAssertions: true,
418
515
  isExpectingAssertionsError: error
@@ -423,13 +520,14 @@ function createExpect(test) {
423
520
  return expect;
424
521
  }
425
522
  function getTestFile(test) {
426
- if (test)
523
+ if (test) {
427
524
  return test.file.filepath;
525
+ }
428
526
  const state = getWorkerState();
429
527
  return state.filepath;
430
528
  }
431
529
  const globalExpect = createExpect();
432
- Object.defineProperty(globalThis, GLOBAL_EXPECT$1, {
530
+ Object.defineProperty(globalThis, GLOBAL_EXPECT, {
433
531
  value: globalExpect,
434
532
  writable: true,
435
533
  configurable: true
@@ -3074,35 +3172,41 @@ class FakeTimers {
3074
3172
  this._global = global;
3075
3173
  }
3076
3174
  clearAllTimers() {
3077
- if (this._fakingTime)
3175
+ if (this._fakingTime) {
3078
3176
  this._clock.reset();
3177
+ }
3079
3178
  }
3080
3179
  dispose() {
3081
3180
  this.useRealTimers();
3082
3181
  }
3083
3182
  runAllTimers() {
3084
- if (this._checkFakeTimers())
3183
+ if (this._checkFakeTimers()) {
3085
3184
  this._clock.runAll();
3185
+ }
3086
3186
  }
3087
3187
  async runAllTimersAsync() {
3088
- if (this._checkFakeTimers())
3188
+ if (this._checkFakeTimers()) {
3089
3189
  await this._clock.runAllAsync();
3190
+ }
3090
3191
  }
3091
3192
  runOnlyPendingTimers() {
3092
- if (this._checkFakeTimers())
3193
+ if (this._checkFakeTimers()) {
3093
3194
  this._clock.runToLast();
3195
+ }
3094
3196
  }
3095
3197
  async runOnlyPendingTimersAsync() {
3096
- if (this._checkFakeTimers())
3198
+ if (this._checkFakeTimers()) {
3097
3199
  await this._clock.runToLastAsync();
3200
+ }
3098
3201
  }
3099
3202
  advanceTimersToNextTimer(steps = 1) {
3100
3203
  if (this._checkFakeTimers()) {
3101
3204
  for (let i = steps; i > 0; i--) {
3102
3205
  this._clock.next();
3103
3206
  this._clock.tick(0);
3104
- if (this._clock.countTimers() === 0)
3207
+ if (this._clock.countTimers() === 0) {
3105
3208
  break;
3209
+ }
3106
3210
  }
3107
3211
  }
3108
3212
  }
@@ -3111,18 +3215,21 @@ class FakeTimers {
3111
3215
  for (let i = steps; i > 0; i--) {
3112
3216
  await this._clock.nextAsync();
3113
3217
  this._clock.tick(0);
3114
- if (this._clock.countTimers() === 0)
3218
+ if (this._clock.countTimers() === 0) {
3115
3219
  break;
3220
+ }
3116
3221
  }
3117
3222
  }
3118
3223
  }
3119
3224
  advanceTimersByTime(msToRun) {
3120
- if (this._checkFakeTimers())
3225
+ if (this._checkFakeTimers()) {
3121
3226
  this._clock.tick(msToRun);
3227
+ }
3122
3228
  }
3123
3229
  async advanceTimersByTimeAsync(msToRun) {
3124
- if (this._checkFakeTimers())
3230
+ if (this._checkFakeTimers()) {
3125
3231
  await this._clock.tickAsync(msToRun);
3232
+ }
3126
3233
  }
3127
3234
  runAllTicks() {
3128
3235
  if (this._checkFakeTimers()) {
@@ -3147,18 +3254,25 @@ class FakeTimers {
3147
3254
  );
3148
3255
  }
3149
3256
  if (!this._fakingTime) {
3150
- const toFake = Object.keys(this._fakeTimers.timers).filter((timer) => timer !== "nextTick");
3151
- if (((_b = (_a = this._userConfig) == null ? void 0 : _a.toFake) == null ? void 0 : _b.includes("nextTick")) && isChildProcess())
3152
- throw new Error("process.nextTick cannot be mocked inside child_process");
3153
- const existingFakedMethods = (((_c = this._userConfig) == null ? void 0 : _c.toFake) || toFake).filter((method) => {
3154
- switch (method) {
3155
- case "setImmediate":
3156
- case "clearImmediate":
3157
- return method in this._global && this._global[method];
3158
- default:
3159
- return true;
3257
+ const toFake = Object.keys(this._fakeTimers.timers).filter(
3258
+ (timer) => timer !== "nextTick"
3259
+ );
3260
+ if (((_b = (_a = this._userConfig) == null ? void 0 : _a.toFake) == null ? void 0 : _b.includes("nextTick")) && isChildProcess()) {
3261
+ throw new Error(
3262
+ "process.nextTick cannot be mocked inside child_process"
3263
+ );
3264
+ }
3265
+ const existingFakedMethods = (((_c = this._userConfig) == null ? void 0 : _c.toFake) || toFake).filter(
3266
+ (method) => {
3267
+ switch (method) {
3268
+ case "setImmediate":
3269
+ case "clearImmediate":
3270
+ return method in this._global && this._global[method];
3271
+ default:
3272
+ return true;
3273
+ }
3160
3274
  }
3161
- });
3275
+ );
3162
3276
  this._clock = this._fakeTimers.install({
3163
3277
  now: Date.now(),
3164
3278
  ...this._userConfig,
@@ -3186,8 +3300,9 @@ class FakeTimers {
3186
3300
  return this._now();
3187
3301
  }
3188
3302
  getTimerCount() {
3189
- if (this._checkFakeTimers())
3303
+ if (this._checkFakeTimers()) {
3190
3304
  return this._clock.countTimers();
3305
+ }
3191
3306
  return 0;
3192
3307
  }
3193
3308
  configure(config) {
@@ -3207,8 +3322,9 @@ class FakeTimers {
3207
3322
  }
3208
3323
 
3209
3324
  function copyStackTrace(target, source) {
3210
- if (source.stack !== void 0)
3325
+ if (source.stack !== void 0) {
3211
3326
  target.stack = source.stack.replace(source.message, target.message);
3327
+ }
3212
3328
  return target;
3213
3329
  }
3214
3330
  function waitFor(callback, options = {}) {
@@ -3221,23 +3337,34 @@ function waitFor(callback, options = {}) {
3221
3337
  let timeoutId;
3222
3338
  let intervalId;
3223
3339
  const onResolve = (result) => {
3224
- if (timeoutId)
3340
+ if (timeoutId) {
3225
3341
  clearTimeout(timeoutId);
3226
- if (intervalId)
3342
+ }
3343
+ if (intervalId) {
3227
3344
  clearInterval(intervalId);
3345
+ }
3228
3346
  resolve(result);
3229
3347
  };
3230
3348
  const handleTimeout = () => {
3349
+ if (intervalId) {
3350
+ clearInterval(intervalId);
3351
+ }
3231
3352
  let error = lastError;
3232
- if (!error)
3233
- error = copyStackTrace(new Error("Timed out in waitFor!"), STACK_TRACE_ERROR);
3353
+ if (!error) {
3354
+ error = copyStackTrace(
3355
+ new Error("Timed out in waitFor!"),
3356
+ STACK_TRACE_ERROR
3357
+ );
3358
+ }
3234
3359
  reject(error);
3235
3360
  };
3236
3361
  const checkCallback = () => {
3237
- if (vi.isFakeTimers())
3362
+ if (vi.isFakeTimers()) {
3238
3363
  vi.advanceTimersByTime(interval);
3239
- if (promiseStatus === "pending")
3364
+ }
3365
+ if (promiseStatus === "pending") {
3240
3366
  return;
3367
+ }
3241
3368
  try {
3242
3369
  const result = callback();
3243
3370
  if (result !== null && typeof result === "object" && typeof result.then === "function") {
@@ -3261,8 +3388,9 @@ function waitFor(callback, options = {}) {
3261
3388
  lastError = error;
3262
3389
  }
3263
3390
  };
3264
- if (checkCallback() === true)
3391
+ if (checkCallback() === true) {
3265
3392
  return;
3393
+ }
3266
3394
  timeoutId = setTimeout(handleTimeout, timeout);
3267
3395
  intervalId = setInterval(checkCallback, interval);
3268
3396
  });
@@ -3276,25 +3404,37 @@ function waitUntil(callback, options = {}) {
3276
3404
  let timeoutId;
3277
3405
  let intervalId;
3278
3406
  const onReject = (error) => {
3279
- if (!error)
3280
- error = copyStackTrace(new Error("Timed out in waitUntil!"), STACK_TRACE_ERROR);
3407
+ if (intervalId) {
3408
+ clearInterval(intervalId);
3409
+ }
3410
+ if (!error) {
3411
+ error = copyStackTrace(
3412
+ new Error("Timed out in waitUntil!"),
3413
+ STACK_TRACE_ERROR
3414
+ );
3415
+ }
3281
3416
  reject(error);
3282
3417
  };
3283
3418
  const onResolve = (result) => {
3284
- if (!result)
3419
+ if (!result) {
3285
3420
  return;
3286
- if (timeoutId)
3421
+ }
3422
+ if (timeoutId) {
3287
3423
  clearTimeout(timeoutId);
3288
- if (intervalId)
3424
+ }
3425
+ if (intervalId) {
3289
3426
  clearInterval(intervalId);
3427
+ }
3290
3428
  resolve(result);
3291
3429
  return true;
3292
3430
  };
3293
3431
  const checkCallback = () => {
3294
- if (vi.isFakeTimers())
3432
+ if (vi.isFakeTimers()) {
3295
3433
  vi.advanceTimersByTime(interval);
3296
- if (promiseStatus === "pending")
3434
+ }
3435
+ if (promiseStatus === "pending") {
3297
3436
  return;
3437
+ }
3298
3438
  try {
3299
3439
  const result = callback();
3300
3440
  if (result !== null && typeof result === "object" && typeof result.then === "function") {
@@ -3317,23 +3457,29 @@ function waitUntil(callback, options = {}) {
3317
3457
  onReject(error);
3318
3458
  }
3319
3459
  };
3320
- if (checkCallback() === true)
3460
+ if (checkCallback() === true) {
3321
3461
  return;
3462
+ }
3322
3463
  timeoutId = setTimeout(onReject, timeout);
3323
3464
  intervalId = setInterval(checkCallback, interval);
3324
3465
  });
3325
3466
  }
3326
3467
 
3327
3468
  function createVitest() {
3328
- const _mocker = typeof __vitest_mocker__ !== "undefined" ? __vitest_mocker__ : new Proxy({}, {
3329
- get(_, name) {
3330
- throw new Error(
3331
- `Vitest mocker was not initialized in this environment. vi.${String(name)}() is forbidden.`
3332
- );
3333
- }
3334
- });
3335
3469
  let _mockedDate = null;
3336
3470
  let _config = null;
3471
+ function _mocker() {
3472
+ return typeof __vitest_mocker__ !== "undefined" ? __vitest_mocker__ : new Proxy(
3473
+ {},
3474
+ {
3475
+ get(_, name) {
3476
+ throw new Error(
3477
+ `Vitest mocker was not initialized in this environment. vi.${String(name)}() is forbidden.`
3478
+ );
3479
+ }
3480
+ }
3481
+ );
3482
+ }
3337
3483
  const workerState = getWorkerState();
3338
3484
  let _timers;
3339
3485
  const timers = () => _timers || (_timers = new FakeTimers({
@@ -3343,10 +3489,13 @@ function createVitest() {
3343
3489
  const _stubsGlobal = /* @__PURE__ */ new Map();
3344
3490
  const _stubsEnv = /* @__PURE__ */ new Map();
3345
3491
  const _envBooleans = ["PROD", "DEV", "SSR"];
3346
- const getImporter = () => {
3347
- const stackTrace = createSimpleStackTrace({ stackTraceLimit: 4 });
3348
- const importerStack = stackTrace.split("\n")[4];
3349
- const stack = parseSingleStack(importerStack);
3492
+ const getImporter = (name) => {
3493
+ const stackTrace = createSimpleStackTrace({ stackTraceLimit: 5 });
3494
+ const stackArray = stackTrace.split("\n");
3495
+ const importerStackIndex = stackArray.findIndex((stack2) => {
3496
+ return stack2.includes(` at Object.${name}`) || stack2.includes(`${name}@`);
3497
+ });
3498
+ const stack = parseSingleStack(stackArray[importerStackIndex + 1]);
3350
3499
  return (stack == null ? void 0 : stack.file) || "";
3351
3500
  };
3352
3501
  const utils = {
@@ -3359,10 +3508,11 @@ function createVitest() {
3359
3508
  );
3360
3509
  }
3361
3510
  }
3362
- if (config)
3511
+ if (config) {
3363
3512
  timers().configure({ ...workerState.config.fakeTimers, ...config });
3364
- else
3513
+ } else {
3365
3514
  timers().configure(workerState.config.fakeTimers);
3515
+ }
3366
3516
  timers().useFakeTimers();
3367
3517
  return utils;
3368
3518
  },
@@ -3439,46 +3589,70 @@ function createVitest() {
3439
3589
  return factory();
3440
3590
  },
3441
3591
  mock(path, factory) {
3442
- if (typeof path !== "string")
3443
- throw new Error(`vi.mock() expects a string path, but received a ${typeof path}`);
3444
- const importer = getImporter();
3445
- _mocker.queueMock(
3592
+ if (typeof path !== "string") {
3593
+ throw new TypeError(
3594
+ `vi.mock() expects a string path, but received a ${typeof path}`
3595
+ );
3596
+ }
3597
+ const importer = getImporter("mock");
3598
+ _mocker().queueMock(
3446
3599
  path,
3447
3600
  importer,
3448
- factory ? () => factory(() => _mocker.importActual(path, importer, _mocker.getMockContext().callstack)) : void 0,
3601
+ factory ? () => factory(
3602
+ () => _mocker().importActual(
3603
+ path,
3604
+ importer,
3605
+ _mocker().getMockContext().callstack
3606
+ )
3607
+ ) : void 0,
3449
3608
  true
3450
3609
  );
3451
3610
  },
3452
3611
  unmock(path) {
3453
- if (typeof path !== "string")
3454
- throw new Error(`vi.unmock() expects a string path, but received a ${typeof path}`);
3455
- _mocker.queueUnmock(path, getImporter());
3612
+ if (typeof path !== "string") {
3613
+ throw new TypeError(
3614
+ `vi.unmock() expects a string path, but received a ${typeof path}`
3615
+ );
3616
+ }
3617
+ _mocker().queueUnmock(path, getImporter("unmock"));
3456
3618
  },
3457
3619
  doMock(path, factory) {
3458
- if (typeof path !== "string")
3459
- throw new Error(`vi.doMock() expects a string path, but received a ${typeof path}`);
3460
- const importer = getImporter();
3461
- _mocker.queueMock(
3620
+ if (typeof path !== "string") {
3621
+ throw new TypeError(
3622
+ `vi.doMock() expects a string path, but received a ${typeof path}`
3623
+ );
3624
+ }
3625
+ const importer = getImporter("doMock");
3626
+ _mocker().queueMock(
3462
3627
  path,
3463
3628
  importer,
3464
- factory ? () => factory(() => _mocker.importActual(path, importer, _mocker.getMockContext().callstack)) : void 0,
3629
+ factory ? () => factory(
3630
+ () => _mocker().importActual(
3631
+ path,
3632
+ importer,
3633
+ _mocker().getMockContext().callstack
3634
+ )
3635
+ ) : void 0,
3465
3636
  false
3466
3637
  );
3467
3638
  },
3468
3639
  doUnmock(path) {
3469
- if (typeof path !== "string")
3470
- throw new Error(`vi.doUnmock() expects a string path, but received a ${typeof path}`);
3471
- _mocker.queueUnmock(path, getImporter());
3640
+ if (typeof path !== "string") {
3641
+ throw new TypeError(
3642
+ `vi.doUnmock() expects a string path, but received a ${typeof path}`
3643
+ );
3644
+ }
3645
+ _mocker().queueUnmock(path, getImporter("doUnmock"));
3472
3646
  },
3473
3647
  async importActual(path) {
3474
- return _mocker.importActual(
3648
+ return _mocker().importActual(
3475
3649
  path,
3476
- getImporter(),
3477
- _mocker.getMockContext().callstack
3650
+ getImporter("importActual"),
3651
+ _mocker().getMockContext().callstack
3478
3652
  );
3479
3653
  },
3480
3654
  async importMock(path) {
3481
- return _mocker.importMock(path, getImporter());
3655
+ return _mocker().importMock(path, getImporter("importMock"));
3482
3656
  },
3483
3657
  // this is typed in the interface so it's not necessary to type it here
3484
3658
  mocked(item, _options = {}) {
@@ -3500,8 +3674,12 @@ function createVitest() {
3500
3674
  return utils;
3501
3675
  },
3502
3676
  stubGlobal(name, value) {
3503
- if (!_stubsGlobal.has(name))
3504
- _stubsGlobal.set(name, Object.getOwnPropertyDescriptor(globalThis, name));
3677
+ if (!_stubsGlobal.has(name)) {
3678
+ _stubsGlobal.set(
3679
+ name,
3680
+ Object.getOwnPropertyDescriptor(globalThis, name)
3681
+ );
3682
+ }
3505
3683
  Object.defineProperty(globalThis, name, {
3506
3684
  value,
3507
3685
  writable: true,
@@ -3511,30 +3689,34 @@ function createVitest() {
3511
3689
  return utils;
3512
3690
  },
3513
3691
  stubEnv(name, value) {
3514
- if (!_stubsEnv.has(name))
3692
+ if (!_stubsEnv.has(name)) {
3515
3693
  _stubsEnv.set(name, process.env[name]);
3516
- if (_envBooleans.includes(name))
3694
+ }
3695
+ if (_envBooleans.includes(name)) {
3517
3696
  process.env[name] = value ? "1" : "";
3518
- else
3697
+ } else {
3519
3698
  process.env[name] = String(value);
3699
+ }
3520
3700
  return utils;
3521
3701
  },
3522
3702
  unstubAllGlobals() {
3523
3703
  _stubsGlobal.forEach((original, name) => {
3524
- if (!original)
3704
+ if (!original) {
3525
3705
  Reflect.deleteProperty(globalThis, name);
3526
- else
3706
+ } else {
3527
3707
  Object.defineProperty(globalThis, name, original);
3708
+ }
3528
3709
  });
3529
3710
  _stubsGlobal.clear();
3530
3711
  return utils;
3531
3712
  },
3532
3713
  unstubAllEnvs() {
3533
3714
  _stubsEnv.forEach((original, name) => {
3534
- if (original === void 0)
3715
+ if (original === void 0) {
3535
3716
  delete process.env[name];
3536
- else
3717
+ } else {
3537
3718
  process.env[name] = original;
3719
+ }
3538
3720
  });
3539
3721
  _stubsEnv.clear();
3540
3722
  return utils;
@@ -3547,13 +3729,15 @@ function createVitest() {
3547
3729
  return waitForImportsToResolve();
3548
3730
  },
3549
3731
  setConfig(config) {
3550
- if (!_config)
3732
+ if (!_config) {
3551
3733
  _config = { ...workerState.config };
3734
+ }
3552
3735
  Object.assign(workerState.config, config);
3553
3736
  },
3554
3737
  resetConfig() {
3555
- if (_config)
3738
+ if (_config) {
3556
3739
  Object.assign(workerState.config, _config);
3740
+ }
3557
3741
  }
3558
3742
  };
3559
3743
  return utils;