vitest 0.17.1 → 0.19.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 (37) hide show
  1. package/LICENSE.md +6 -6
  2. package/dist/browser.d.ts +1850 -0
  3. package/dist/browser.mjs +20 -0
  4. package/dist/{chunk-api-setup.c728e251.mjs → chunk-api-setup.0cf2c96a.mjs} +37 -11
  5. package/dist/{chunk-constants.27550afb.mjs → chunk-constants.38b43a44.mjs} +3 -3
  6. package/dist/{chunk-env-node.aa51c4cc.mjs → chunk-defaults.408a0cfe.mjs} +457 -455
  7. package/dist/{chunk-install-pkg.6f5930c3.mjs → chunk-install-pkg.6c6dc0c2.mjs} +11 -10
  8. package/dist/chunk-integrations-globals.803277be.mjs +24 -0
  9. package/dist/chunk-node-git.9058b82a.mjs +1139 -0
  10. package/dist/chunk-runtime-chain.1e1aabb3.mjs +2025 -0
  11. package/dist/{vendor-entry.1ad8a08d.mjs → chunk-runtime-error.d82dd2cf.mjs} +167 -183
  12. package/dist/{chunk-runtime-chain.6d23d202.mjs → chunk-runtime-hooks.db398170.mjs} +33 -2012
  13. package/dist/{chunk-runtime-mocker.34b9d585.mjs → chunk-runtime-mocker.dfdfd57b.mjs} +70 -22
  14. package/dist/{chunk-runtime-rpc.d986adb9.mjs → chunk-runtime-rpc.45d8ee19.mjs} +1 -1
  15. package/dist/{chunk-utils-global.4828c2e2.mjs → chunk-utils-global.2aa95025.mjs} +14 -9
  16. package/dist/{chunk-utils-source-map.a9047343.mjs → chunk-utils-source-map.8b066ce2.mjs} +2 -2
  17. package/dist/{chunk-vite-node-externalize.0fc8ed68.mjs → chunk-vite-node-externalize.a2813ad7.mjs} +1202 -2178
  18. package/dist/chunk-vite-node-utils.ad73f2ab.mjs +1433 -0
  19. package/dist/cli.mjs +11 -13
  20. package/dist/config.cjs +4 -1
  21. package/dist/config.d.ts +1 -0
  22. package/dist/config.mjs +4 -1
  23. package/dist/entry.mjs +54 -10
  24. package/dist/index.d.ts +126 -58
  25. package/dist/index.mjs +12 -9
  26. package/dist/node.d.ts +109 -58
  27. package/dist/node.mjs +12 -14
  28. package/dist/spy.mjs +102 -2
  29. package/dist/suite.mjs +13 -0
  30. package/dist/vendor-index.61438b77.mjs +335 -0
  31. package/dist/{vendor-index.a2a385d8.mjs → vendor-index.62ce5c33.mjs} +6 -338
  32. package/dist/{vendor-index.98e769c1.mjs → vendor-index.de788b6a.mjs} +7 -7
  33. package/dist/worker.mjs +6 -6
  34. package/package.json +20 -12
  35. package/dist/chunk-integrations-globals.3df36e26.mjs +0 -26
  36. package/dist/chunk-integrations-spy.674b628e.mjs +0 -102
  37. package/dist/chunk-vite-node-utils.0f776286.mjs +0 -9195
@@ -1,35 +1,118 @@
1
- import { promises } from 'fs';
2
- import { v as isNode, a as getWorkerState, R as RealDate, h as safeClearTimeout, f as safeSetTimeout, F as toArray, Q as deepClone, O as getType, u as relative, S as partitionSuiteChildren, E as shuffle, T as hasTests, y as hasFailed, e as getFullName, k as resetModules } from './chunk-utils-global.4828c2e2.mjs';
3
- import { d as environments, t as takeCoverage, p as pLimit, f as envs } from './chunk-env-node.aa51c4cc.mjs';
4
- import { s as suite, t as test, d as describe, i as it, r as runOnce, a as isFirstRun, b as beforeAll, c as afterAll, e as beforeEach, f as afterEach, w as withCallback, g as createExpect, h as globalExpect, v as vitest, j as vi, k as getRunningMode, l as isWatchMode, m as resetRunOnceCounter, n as clearCollectorContext, o as defaultSuite, p as setHooks, q as getHooks, u as collectorContext, x as getSnapshotClient, y as setState, G as GLOBAL_EXPECT, z as getFn, A as getState } from './chunk-runtime-chain.6d23d202.mjs';
5
- import chai, { assert, should, util } from 'chai';
6
- import { r as rpc } from './chunk-runtime-rpc.d986adb9.mjs';
7
- import { format } from 'util';
8
- import { s as stringify } from './chunk-utils-source-map.a9047343.mjs';
1
+ import { e as environments, t as takeCoverage, p as pLimit } from './chunk-defaults.408a0cfe.mjs';
2
+ import { r as resetRunOnceCounter, i as index, v as vi } from './chunk-runtime-hooks.db398170.mjs';
3
+ import { o as deepClone, q as getType, t as isNode, g as getWorkerState, R as RealDate, k as safeClearTimeout, s as safeSetTimeout, u as toArray, v as relativePath, x as isBrowser, y as partitionSuiteChildren, z as shuffle, A as hasTests, B as hasFailed, h as getFullName } from './chunk-utils-global.2aa95025.mjs';
4
+ import { e as clearCollectorContext, f as defaultSuite, h as setHooks, j as getHooks, k as collectorContext, l as setState, G as GLOBAL_EXPECT, m as getFn, n as getState } from './chunk-runtime-chain.1e1aabb3.mjs';
5
+ import { r as rpc } from './chunk-runtime-rpc.45d8ee19.mjs';
6
+ import util$1 from 'util';
7
+ import { util } from 'chai';
8
+ import { s as stringify } from './chunk-utils-source-map.8b066ce2.mjs';
9
9
 
10
- var index = /*#__PURE__*/Object.freeze({
11
- __proto__: null,
12
- suite: suite,
13
- test: test,
14
- describe: describe,
15
- it: it,
16
- runOnce: runOnce,
17
- isFirstRun: isFirstRun,
18
- beforeAll: beforeAll,
19
- afterAll: afterAll,
20
- beforeEach: beforeEach,
21
- afterEach: afterEach,
22
- withCallback: withCallback,
23
- assert: assert,
24
- should: should,
25
- createExpect: createExpect,
26
- chai: chai,
27
- expect: globalExpect,
28
- vitest: vitest,
29
- vi: vi,
30
- getRunningMode: getRunningMode,
31
- isWatchMode: isWatchMode
32
- });
10
+ const OBJECT_PROTO = Object.getPrototypeOf({});
11
+ function serializeError(val, seen = /* @__PURE__ */ new WeakMap()) {
12
+ if (!val || typeof val === "string")
13
+ return val;
14
+ if (typeof val === "function")
15
+ return `Function<${val.name}>`;
16
+ if (typeof val !== "object")
17
+ return val;
18
+ if (val instanceof Promise || "then" in val || val.constructor && val.constructor.prototype === "AsyncFunction")
19
+ return "Promise";
20
+ if (typeof Element !== "undefined" && val instanceof Element)
21
+ return val.tagName;
22
+ if (typeof val.asymmetricMatch === "function")
23
+ return `${val.toString()} ${util$1.format(val.sample)}`;
24
+ if (seen.has(val))
25
+ return seen.get(val);
26
+ if (Array.isArray(val)) {
27
+ const clone = new Array(val.length);
28
+ seen.set(val, clone);
29
+ val.forEach((e, i) => {
30
+ clone[i] = serializeError(e, seen);
31
+ });
32
+ return clone;
33
+ } else {
34
+ const clone = /* @__PURE__ */ Object.create(null);
35
+ seen.set(val, clone);
36
+ let obj = val;
37
+ while (obj && obj !== OBJECT_PROTO) {
38
+ Object.getOwnPropertyNames(obj).forEach((key) => {
39
+ if (!(key in clone))
40
+ clone[key] = serializeError(obj[key], seen);
41
+ });
42
+ obj = Object.getPrototypeOf(obj);
43
+ }
44
+ return clone;
45
+ }
46
+ }
47
+ function normalizeErrorMessage(message) {
48
+ return message.replace(/__vite_ssr_import_\d+__\./g, "");
49
+ }
50
+ function processError(err) {
51
+ if (!err)
52
+ return err;
53
+ if (err.stack)
54
+ err.stackStr = String(err.stack);
55
+ if (err.name)
56
+ err.nameStr = String(err.name);
57
+ const clonedActual = deepClone(err.actual);
58
+ const clonedExpected = deepClone(err.expected);
59
+ const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected);
60
+ err.actual = replacedActual;
61
+ err.expected = replacedExpected;
62
+ if (typeof err.expected !== "string")
63
+ err.expected = stringify(err.expected);
64
+ if (typeof err.actual !== "string")
65
+ err.actual = stringify(err.actual);
66
+ try {
67
+ if (typeof err.message === "string")
68
+ err.message = normalizeErrorMessage(err.message);
69
+ if (typeof err.cause === "object" && typeof err.cause.message === "string")
70
+ err.cause.message = normalizeErrorMessage(err.cause.message);
71
+ } catch {
72
+ }
73
+ try {
74
+ return serializeError(err);
75
+ } catch (e) {
76
+ return serializeError(new Error(`Failed to fully serialize error: ${e == null ? void 0 : e.message}
77
+ Inner error message: ${err == null ? void 0 : err.message}`));
78
+ }
79
+ }
80
+ function isAsymmetricMatcher(data) {
81
+ const type = getType(data);
82
+ return type === "Object" && typeof data.asymmetricMatch === "function";
83
+ }
84
+ function isReplaceable(obj1, obj2) {
85
+ const obj1Type = getType(obj1);
86
+ const obj2Type = getType(obj2);
87
+ return obj1Type === obj2Type && obj1Type === "Object";
88
+ }
89
+ function replaceAsymmetricMatcher(actual, expected, actualReplaced = /* @__PURE__ */ new WeakMap(), expectedReplaced = /* @__PURE__ */ new WeakMap()) {
90
+ if (!isReplaceable(actual, expected))
91
+ return { replacedActual: actual, replacedExpected: expected };
92
+ if (actualReplaced.has(actual) || expectedReplaced.has(expected))
93
+ return { replacedActual: actual, replacedExpected: expected };
94
+ actualReplaced.set(actual, true);
95
+ expectedReplaced.set(expected, true);
96
+ util.getOwnEnumerableProperties(expected).forEach((key) => {
97
+ const expectedValue = expected[key];
98
+ const actualValue = actual[key];
99
+ if (isAsymmetricMatcher(expectedValue)) {
100
+ if (expectedValue.asymmetricMatch(actualValue))
101
+ actual[key] = expectedValue;
102
+ } else if (isAsymmetricMatcher(actualValue)) {
103
+ if (actualValue.asymmetricMatch(expectedValue))
104
+ expected[key] = actualValue;
105
+ } else if (isReplaceable(actualValue, expectedValue)) {
106
+ const replaced = replaceAsymmetricMatcher(actualValue, expectedValue, actualReplaced, expectedReplaced);
107
+ actual[key] = replaced.replacedActual;
108
+ expected[key] = replaced.replacedExpected;
109
+ }
110
+ });
111
+ return {
112
+ replacedActual: actual,
113
+ replacedExpected: expected
114
+ };
115
+ }
33
116
 
34
117
  let globalSetup = false;
35
118
  async function setupGlobalEnv(config) {
@@ -45,7 +128,7 @@ async function setupGlobalEnv(config) {
45
128
  if (isNode)
46
129
  await setupConsoleLogSpy();
47
130
  if (config.globals)
48
- (await import('./chunk-integrations-globals.3df36e26.mjs')).registerApiGlobally();
131
+ (await import('./chunk-integrations-globals.803277be.mjs')).registerApiGlobally();
49
132
  }
50
133
  function setupDefines(defines) {
51
134
  for (const key in defines)
@@ -173,113 +256,6 @@ async function runSetupFiles(config) {
173
256
  }));
174
257
  }
175
258
 
176
- const OBJECT_PROTO = Object.getPrototypeOf({});
177
- function serializeError(val, seen = /* @__PURE__ */ new WeakMap()) {
178
- if (!val || typeof val === "string")
179
- return val;
180
- if (typeof val === "function")
181
- return `Function<${val.name}>`;
182
- if (typeof val !== "object")
183
- return val;
184
- if (val instanceof Promise || "then" in val || val.constructor && val.constructor.prototype === "AsyncFunction")
185
- return "Promise";
186
- if (typeof Element !== "undefined" && val instanceof Element)
187
- return val.tagName;
188
- if (typeof val.asymmetricMatch === "function")
189
- return `${val.toString()} ${format(val.sample)}`;
190
- if (seen.has(val))
191
- return seen.get(val);
192
- if (Array.isArray(val)) {
193
- const clone = new Array(val.length);
194
- seen.set(val, clone);
195
- val.forEach((e, i) => {
196
- clone[i] = serializeError(e, seen);
197
- });
198
- return clone;
199
- } else {
200
- const clone = /* @__PURE__ */ Object.create(null);
201
- seen.set(val, clone);
202
- let obj = val;
203
- while (obj && obj !== OBJECT_PROTO) {
204
- Object.getOwnPropertyNames(obj).forEach((key) => {
205
- if (!(key in clone))
206
- clone[key] = serializeError(obj[key], seen);
207
- });
208
- obj = Object.getPrototypeOf(obj);
209
- }
210
- return clone;
211
- }
212
- }
213
- function normalizeErrorMessage(message) {
214
- return message.replace(/__vite_ssr_import_\d+__\./g, "");
215
- }
216
- function processError(err) {
217
- if (!err)
218
- return err;
219
- if (err.stack)
220
- err.stackStr = String(err.stack);
221
- if (err.name)
222
- err.nameStr = String(err.name);
223
- const clonedActual = deepClone(err.actual);
224
- const clonedExpected = deepClone(err.expected);
225
- const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected);
226
- err.actual = replacedActual;
227
- err.expected = replacedExpected;
228
- if (typeof err.expected !== "string")
229
- err.expected = stringify(err.expected);
230
- if (typeof err.actual !== "string")
231
- err.actual = stringify(err.actual);
232
- try {
233
- if (typeof err.message === "string")
234
- err.message = normalizeErrorMessage(err.message);
235
- if (typeof err.cause === "object" && err.cause.message === "string")
236
- err.cause.message = normalizeErrorMessage(err.cause.message);
237
- } catch {
238
- }
239
- try {
240
- return serializeError(err);
241
- } catch (e) {
242
- return serializeError(new Error(`Failed to fully serialize error: ${e == null ? void 0 : e.message}
243
- Inner error message: ${err == null ? void 0 : err.message}`));
244
- }
245
- }
246
- function isAsymmetricMatcher(data) {
247
- const type = getType(data);
248
- return type === "Object" && typeof data.asymmetricMatch === "function";
249
- }
250
- function isReplaceable(obj1, obj2) {
251
- const obj1Type = getType(obj1);
252
- const obj2Type = getType(obj2);
253
- return obj1Type === obj2Type && obj1Type === "Object";
254
- }
255
- function replaceAsymmetricMatcher(actual, expected, actualReplaced = /* @__PURE__ */ new WeakMap(), expectedReplaced = /* @__PURE__ */ new WeakMap()) {
256
- if (!isReplaceable(actual, expected))
257
- return { replacedActual: actual, replacedExpected: expected };
258
- if (actualReplaced.has(actual) || expectedReplaced.has(expected))
259
- return { replacedActual: actual, replacedExpected: expected };
260
- actualReplaced.set(actual, true);
261
- expectedReplaced.set(expected, true);
262
- util.getOwnEnumerableProperties(expected).forEach((key) => {
263
- const expectedValue = expected[key];
264
- const actualValue = actual[key];
265
- if (isAsymmetricMatcher(expectedValue)) {
266
- if (expectedValue.asymmetricMatch(actualValue))
267
- actual[key] = expectedValue;
268
- } else if (isAsymmetricMatcher(actualValue)) {
269
- if (actualValue.asymmetricMatch(expectedValue))
270
- expected[key] = actualValue;
271
- } else if (isReplaceable(actualValue, expectedValue)) {
272
- const replaced = replaceAsymmetricMatcher(actualValue, expectedValue, actualReplaced, expectedReplaced);
273
- actual[key] = replaced.replacedActual;
274
- expected[key] = replaced.replacedExpected;
275
- }
276
- });
277
- return {
278
- replacedActual: actual,
279
- replacedExpected: expected
280
- };
281
- }
282
-
283
259
  const now$1 = Date.now;
284
260
  function hash(str) {
285
261
  let hash2 = 0;
@@ -294,8 +270,17 @@ function hash(str) {
294
270
  }
295
271
  async function collectTests(paths, config) {
296
272
  const files = [];
273
+ const browserHashMap = getWorkerState().browserHashMap;
274
+ async function importFromBrowser(filepath) {
275
+ const match = filepath.match(/^(\w:\/)/);
276
+ const hash2 = browserHashMap.get(filepath);
277
+ if (match)
278
+ return await import(`/@fs/${filepath.slice(match[1].length)}?v=${hash2}`);
279
+ else
280
+ return await import(`${filepath}?v=${hash2}`);
281
+ }
297
282
  for (const filepath of paths) {
298
- const path = relative(config.root, filepath);
283
+ const path = relativePath(config.root, filepath);
299
284
  const file = {
300
285
  id: hash(path),
301
286
  name: path,
@@ -307,7 +292,10 @@ async function collectTests(paths, config) {
307
292
  clearCollectorContext();
308
293
  try {
309
294
  await runSetupFiles(config);
310
- await import(filepath);
295
+ if (config.browser && isBrowser)
296
+ await importFromBrowser(filepath);
297
+ else
298
+ await import(filepath);
311
299
  const defaultTasks = await defaultSuite.collect(file);
312
300
  setHooks(file, getHooks(defaultTasks));
313
301
  for (const c of [...defaultTasks.tasks, ...collectorContext.tasks]) {
@@ -328,6 +316,8 @@ async function collectTests(paths, config) {
328
316
  state: "fail",
329
317
  error: processError(e)
330
318
  };
319
+ if (config.browser)
320
+ console.error(e);
331
321
  }
332
322
  calculateHash(file);
333
323
  const hasOnlyTasks = someTasksAreOnly(file);
@@ -447,6 +437,7 @@ async function sendTasksUpdate() {
447
437
  async function runTest(test) {
448
438
  var _a, _b;
449
439
  if (test.mode !== "run") {
440
+ const { getSnapshotClient } = await import('./chunk-runtime-chain.1e1aabb3.mjs').then(function (n) { return n.p; });
450
441
  getSnapshotClient().skipTestSnapshots(test);
451
442
  return;
452
443
  }
@@ -461,7 +452,10 @@ async function runTest(test) {
461
452
  };
462
453
  updateTask(test);
463
454
  clearModuleMocks();
464
- await getSnapshotClient().setTest(test);
455
+ if (isNode) {
456
+ const { getSnapshotClient } = await import('./chunk-runtime-chain.1e1aabb3.mjs').then(function (n) { return n.p; });
457
+ await getSnapshotClient().setTest(test);
458
+ }
465
459
  const workerState = getWorkerState();
466
460
  workerState.current = test;
467
461
  let beforeEachCleanups = [];
@@ -509,9 +503,14 @@ async function runTest(test) {
509
503
  test.result.error = void 0;
510
504
  }
511
505
  }
512
- getSnapshotClient().clearTest();
506
+ if (isBrowser && test.result.error)
507
+ console.error(test.result.error.message, test.result.error.stackStr);
508
+ if (isNode) {
509
+ const { getSnapshotClient } = await import('./chunk-runtime-chain.1e1aabb3.mjs').then(function (n) { return n.p; });
510
+ getSnapshotClient().clearTest();
511
+ }
513
512
  test.result.duration = now() - start;
514
- if (workerState.config.logHeapUsage)
513
+ if (workerState.config.logHeapUsage && isNode)
515
514
  test.result.heap = process.memoryUsage().heapUsed;
516
515
  workerState.current = void 0;
517
516
  updateTask(test);
@@ -570,7 +569,7 @@ async function runSuite(suite) {
570
569
  }
571
570
  }
572
571
  suite.result.duration = now() - start;
573
- if (workerState.config.logHeapUsage)
572
+ if (workerState.config.logHeapUsage && isNode)
574
573
  suite.result.heap = process.memoryUsage().heapUsed;
575
574
  if (suite.mode === "run") {
576
575
  if (!hasTests(suite)) {
@@ -588,6 +587,10 @@ async function runSuite(suite) {
588
587
  async function runSuiteChild(c) {
589
588
  return c.type === "test" ? runTest(c) : runSuite(c);
590
589
  }
590
+ async function runSuites(suites) {
591
+ for (const suite of suites)
592
+ await runSuite(suite);
593
+ }
591
594
  async function runFiles(files, config) {
592
595
  var _a;
593
596
  for (const file of files) {
@@ -602,15 +605,32 @@ async function runFiles(files, config) {
602
605
  await runSuite(file);
603
606
  }
604
607
  }
605
- async function startTests(paths, config) {
608
+ async function startTestsBrowser(paths, config) {
609
+ if (isNode) {
610
+ rpc().onPathsCollected(paths);
611
+ } else {
612
+ const files = await collectTests(paths, config);
613
+ await rpc().onCollected(files);
614
+ await runSuites(files);
615
+ await sendTasksUpdate();
616
+ }
617
+ }
618
+ async function startTestsNode(paths, config) {
606
619
  const files = await collectTests(paths, config);
607
620
  rpc().onCollected(files);
621
+ const { getSnapshotClient } = await import('./chunk-runtime-chain.1e1aabb3.mjs').then(function (n) { return n.p; });
608
622
  getSnapshotClient().clear();
609
623
  await runFiles(files, config);
610
624
  takeCoverage();
611
625
  await getSnapshotClient().saveCurrent();
612
626
  await sendTasksUpdate();
613
627
  }
628
+ async function startTests(paths, config) {
629
+ if (config.browser)
630
+ return startTestsBrowser(paths, config);
631
+ else
632
+ return startTestsNode(paths, config);
633
+ }
614
634
  function clearModuleMocks() {
615
635
  const { clearMocks, mockReset, restoreMocks } = getWorkerState().config;
616
636
  if (restoreMocks)
@@ -621,40 +641,4 @@ function clearModuleMocks() {
621
641
  vi.clearAllMocks();
622
642
  }
623
643
 
624
- async function run(files, config) {
625
- await setupGlobalEnv(config);
626
- const workerState = getWorkerState();
627
- const filesWithEnv = await Promise.all(files.map(async (file) => {
628
- var _a;
629
- const code = await promises.readFile(file, "utf-8");
630
- const env = ((_a = code.match(/@(?:vitest|jest)-environment\s+?([\w-]+)\b/)) == null ? void 0 : _a[1]) || config.environment || "node";
631
- if (!envs.includes(env))
632
- throw new Error(`Unsupported environment: "${env}" in ${file}`);
633
- return {
634
- file,
635
- env
636
- };
637
- }));
638
- const filesByEnv = filesWithEnv.reduce((acc, { file, env }) => {
639
- acc[env] || (acc[env] = []);
640
- acc[env].push(file);
641
- return acc;
642
- }, {});
643
- for (const env of envs) {
644
- const environment = env;
645
- const files2 = filesByEnv[environment];
646
- if (!files2 || !files2.length)
647
- continue;
648
- await withEnv(environment, config.environmentOptions || {}, async () => {
649
- for (const file of files2) {
650
- workerState.mockMap.clear();
651
- resetModules();
652
- workerState.filepath = file;
653
- await startTests([file], config);
654
- workerState.filepath = void 0;
655
- }
656
- });
657
- }
658
- }
659
-
660
- export { index as i, run as r };
644
+ export { setupGlobalEnv as a, startTests as s, withEnv as w };