vitest 0.33.0 → 0.34.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 (49) hide show
  1. package/LICENSE.md +0 -83
  2. package/dist/browser.d.ts +3 -2
  3. package/dist/browser.js +4 -8
  4. package/dist/child.js +43 -29
  5. package/dist/{chunk-api-setup.8f785c4a.js → chunk-api-setup.644415c3.js} +13 -7
  6. package/dist/{chunk-install-pkg.23da664c.js → chunk-install-pkg.dd70081b.js} +14 -13
  7. package/dist/{chunk-integrations-globals.0093e2ed.js → chunk-integrations-globals.877c84db.js} +7 -6
  8. package/dist/chunk-runtime-console.ea222ffb.js +108 -0
  9. package/dist/cli.js +14 -15
  10. package/dist/config.cjs +4 -3
  11. package/dist/config.d.ts +15 -19
  12. package/dist/config.js +4 -3
  13. package/dist/coverage.d.ts +3 -2
  14. package/dist/entry-vm.js +60 -0
  15. package/dist/entry.js +34 -213
  16. package/dist/environments.d.ts +3 -2
  17. package/dist/environments.js +4 -2
  18. package/dist/execute.js +15 -0
  19. package/dist/index.d.ts +12 -7
  20. package/dist/index.js +8 -7
  21. package/dist/loader.js +6 -4
  22. package/dist/node.d.ts +6 -53
  23. package/dist/node.js +16 -19
  24. package/dist/runners.d.ts +3 -2
  25. package/dist/runners.js +6 -5
  26. package/dist/{types-198fd1d9.d.ts → types-3c7dbfa5.d.ts} +139 -121
  27. package/dist/vendor-base.9c08bbd0.js +96 -0
  28. package/dist/{vendor-coverage.2e41927a.js → vendor-coverage.78040316.js} +1 -2
  29. package/dist/vendor-date.6e993429.js +50 -0
  30. package/dist/{vendor-environments.392ddf08.js → vendor-environments.443ecd82.js} +199 -3
  31. package/dist/vendor-execute.9ab1c1a7.js +978 -0
  32. package/dist/{vendor-global.6795f91f.js → vendor-global.97e4527c.js} +2 -1
  33. package/dist/{vendor-index.23ac4e13.js → vendor-index.087d1af7.js} +2 -20
  34. package/dist/vendor-index.9378c9a4.js +89 -0
  35. package/dist/vendor-index.b271ebe4.js +92 -0
  36. package/dist/{vendor-index.2af39fbb.js → vendor-index.eff408fd.js} +2 -3
  37. package/dist/{vendor-cli-api.bf4b62a8.js → vendor-node.00226ab1.js} +538 -836
  38. package/dist/{vendor-rpc.ad5b08c7.js → vendor-rpc.cbd8e972.js} +7 -4
  39. package/dist/{vendor-run-once.1fa85ba7.js → vendor-run-once.3e5ef7d7.js} +1 -2
  40. package/dist/vendor-source-map.e6c1997b.js +747 -0
  41. package/dist/{vendor-vi.dd6706cb.js → vendor-vi.271667ef.js} +26 -59
  42. package/dist/vm.js +114 -0
  43. package/dist/worker.js +39 -27
  44. package/execute.d.ts +1 -0
  45. package/package.json +12 -9
  46. package/suppress-warnings.cjs +2 -0
  47. package/dist/vendor-execute.3576af13.js +0 -421
  48. package/dist/vendor-index.cc463d9e.js +0 -189
  49. package/dist/vendor-tasks.f9d75aed.js +0 -14
@@ -1,17 +1,36 @@
1
1
  import { getCurrentSuite, getCurrentTest } from '@vitest/runner';
2
2
  import { createChainable, getNames } from '@vitest/runner/utils';
3
- import { getSafeTimers, noop, assertTypes, createSimpleStackTrace, parseSingleStack } from '@vitest/utils';
4
- import { i as isRunningInBenchmark, r as resetModules } from './vendor-index.23ac4e13.js';
3
+ import { getSafeTimers, noop, assertTypes, createSimpleStackTrace } from '@vitest/utils';
4
+ import { i as isRunningInBenchmark } from './vendor-index.087d1af7.js';
5
5
  import * as chai$1 from 'chai';
6
6
  import { c as commonjsGlobal, g as getDefaultExportFromCjs } from './vendor-_commonjsHelpers.7d1333e8.js';
7
7
  import { equals, iterableEquality, subsetEquality, JestExtend, JestChaiExpect, JestAsymmetricMatchers, GLOBAL_EXPECT as GLOBAL_EXPECT$1, getState, setState } from '@vitest/expect';
8
8
  import { SnapshotClient, stripSnapshotIndentation, addSerializer } from '@vitest/snapshot';
9
9
  import '@vitest/utils/error';
10
- import { g as getWorkerState, a as getCurrentEnvironment } from './vendor-global.6795f91f.js';
11
- import { g as getFullName } from './vendor-tasks.f9d75aed.js';
10
+ import { g as getFullName, a as parseSingleStack } from './vendor-source-map.e6c1997b.js';
11
+ import { g as getWorkerState, a as getCurrentEnvironment } from './vendor-global.97e4527c.js';
12
12
  import require$$2 from 'util';
13
+ import { R as RealDate, r as resetDate, m as mockDate } from './vendor-date.6e993429.js';
13
14
  import { spyOn, fn, isMockFunction, spies } from '@vitest/spy';
14
15
 
16
+ function resetModules(modules, resetMocks = false) {
17
+ const skipPaths = [
18
+ // Vitest
19
+ /\/vitest\/dist\//,
20
+ /\/vite-node\/dist\//,
21
+ // yarn's .store folder
22
+ /vitest-virtual-\w+\/dist/,
23
+ // cnpm
24
+ /@vitest\/dist/,
25
+ // don't clear mocks
26
+ ...!resetMocks ? [/^mock:/] : []
27
+ ];
28
+ modules.forEach((mod, path) => {
29
+ if (skipPaths.some((re) => re.test(path)))
30
+ return;
31
+ modules.invalidateModule(mod);
32
+ });
33
+ }
15
34
  function waitNextTick() {
16
35
  const { setTimeout } = getSafeTimers();
17
36
  return new Promise((resolve) => setTimeout(resolve, 0));
@@ -423,9 +442,6 @@ Object.defineProperty(globalThis, GLOBAL_EXPECT$1, {
423
442
  writable: true,
424
443
  configurable: true
425
444
  });
426
- function setupChaiConfig(config) {
427
- Object.assign(chai$1.config, config);
428
- }
429
445
 
430
446
  var fakeTimersSrc = {exports: {}};
431
447
 
@@ -3027,55 +3043,6 @@ var lib = {
3027
3043
 
3028
3044
  var fakeTimersSrcExports = fakeTimersSrc.exports;
3029
3045
 
3030
- const RealDate = Date;
3031
- let now = null;
3032
- class MockDate extends RealDate {
3033
- constructor(y, m, d, h, M, s, ms) {
3034
- super();
3035
- let date;
3036
- switch (arguments.length) {
3037
- case 0:
3038
- if (now !== null)
3039
- date = new RealDate(now.valueOf());
3040
- else
3041
- date = new RealDate();
3042
- break;
3043
- case 1:
3044
- date = new RealDate(y);
3045
- break;
3046
- default:
3047
- d = typeof d === "undefined" ? 1 : d;
3048
- h = h || 0;
3049
- M = M || 0;
3050
- s = s || 0;
3051
- ms = ms || 0;
3052
- date = new RealDate(y, m, d, h, M, s, ms);
3053
- break;
3054
- }
3055
- return date;
3056
- }
3057
- }
3058
- MockDate.UTC = RealDate.UTC;
3059
- MockDate.now = function() {
3060
- return new MockDate().valueOf();
3061
- };
3062
- MockDate.parse = function(dateString) {
3063
- return RealDate.parse(dateString);
3064
- };
3065
- MockDate.toString = function() {
3066
- return RealDate.toString();
3067
- };
3068
- function mockDate(date) {
3069
- const dateObj = new RealDate(date.valueOf());
3070
- if (Number.isNaN(dateObj.getTime()))
3071
- throw new TypeError(`mockdate: The time set is an invalid date: ${date}`);
3072
- globalThis.Date = MockDate;
3073
- now = dateObj.valueOf();
3074
- }
3075
- function resetDate() {
3076
- globalThis.Date = RealDate;
3077
- }
3078
-
3079
3046
  class FakeTimers {
3080
3047
  _clock;
3081
3048
  _fakingTime;
@@ -3212,9 +3179,9 @@ class FakeTimers {
3212
3179
 
3213
3180
  function createVitest() {
3214
3181
  const _mocker = typeof __vitest_mocker__ !== "undefined" ? __vitest_mocker__ : new Proxy({}, {
3215
- get(name) {
3182
+ get(_, name) {
3216
3183
  throw new Error(
3217
- `Vitest mocker was not initialized in this environment. vi.${name}() is forbidden.`
3184
+ `Vitest mocker was not initialized in this environment. vi.${String(name)}() is forbidden.`
3218
3185
  );
3219
3186
  }
3220
3187
  });
@@ -3418,4 +3385,4 @@ function createVitest() {
3418
3385
  const vitest = createVitest();
3419
3386
  const vi = vitest;
3420
3387
 
3421
- export { RealDate as R, getBenchOptions as a, getBenchFn as b, createExpect as c, globalExpect as d, bench as e, vitest as f, getSnapshotClient as g, setupChaiConfig as s, vi as v };
3388
+ export { getBenchOptions as a, getBenchFn as b, createExpect as c, globalExpect as d, bench as e, vitest as f, getSnapshotClient as g, resetModules as r, vi as v };
package/dist/vm.js ADDED
@@ -0,0 +1,114 @@
1
+ import { pathToFileURL } from 'node:url';
2
+ import { performance } from 'node:perf_hooks';
3
+ import { isContext } from 'node:vm';
4
+ import { ModuleCacheMap } from 'vite-node/client';
5
+ import { workerId } from 'tinypool';
6
+ import { c as createBirpc } from './vendor-index.b271ebe4.js';
7
+ import { resolve } from 'pathe';
8
+ import { installSourcemapsSupport } from 'vite-node/source-map';
9
+ import { d as distDir } from './vendor-paths.84fc7a99.js';
10
+ import { l as loadEnvironment } from './vendor-environments.443ecd82.js';
11
+ import { b as startVitestExecutor } from './vendor-execute.9ab1c1a7.js';
12
+ import { createCustomConsole } from './chunk-runtime-console.ea222ffb.js';
13
+ import { c as createSafeRpc } from './vendor-rpc.cbd8e972.js';
14
+ import 'local-pkg';
15
+ import 'node:console';
16
+ import 'vite-node/utils';
17
+ import '@vitest/utils/error';
18
+ import './vendor-global.97e4527c.js';
19
+ import 'node:fs';
20
+ import '@vitest/utils';
21
+ import './vendor-base.9c08bbd0.js';
22
+ import 'node:path';
23
+ import 'node:module';
24
+ import 'vite-node/constants';
25
+ import 'node:stream';
26
+ import './vendor-date.6e993429.js';
27
+
28
+ const entryFile = pathToFileURL(resolve(distDir, "entry-vm.js")).href;
29
+ async function run(ctx) {
30
+ var _a;
31
+ const moduleCache = new ModuleCacheMap();
32
+ const mockMap = /* @__PURE__ */ new Map();
33
+ const { config, port } = ctx;
34
+ let setCancel = (_reason) => {
35
+ };
36
+ const onCancel = new Promise((resolve2) => {
37
+ setCancel = resolve2;
38
+ });
39
+ const rpc = createBirpc(
40
+ {
41
+ onCancel: setCancel
42
+ },
43
+ {
44
+ eventNames: ["onUserConsoleLog", "onFinished", "onCollected", "onWorkerExit"],
45
+ post(v) {
46
+ port.postMessage(v);
47
+ },
48
+ on(fn) {
49
+ port.addListener("message", fn);
50
+ }
51
+ }
52
+ );
53
+ const environment = await loadEnvironment(ctx.environment.name, ctx.config.root);
54
+ if (!environment.setupVM) {
55
+ const envName = ctx.environment.name;
56
+ const packageId = envName[0] === "." ? envName : `vitest-environment-${envName}`;
57
+ throw new TypeError(
58
+ `Environment "${ctx.environment.name}" is not a valid environment. Path "${packageId}" doesn't support vm environment because it doesn't provide "setupVM" method.`
59
+ );
60
+ }
61
+ const state = {
62
+ ctx,
63
+ moduleCache,
64
+ config,
65
+ mockMap,
66
+ onCancel,
67
+ environment,
68
+ durations: {
69
+ environment: performance.now(),
70
+ prepare: performance.now()
71
+ },
72
+ rpc: createSafeRpc(rpc)
73
+ };
74
+ installSourcemapsSupport({
75
+ getSourceMap: (source) => moduleCache.getSourceMap(source)
76
+ });
77
+ const vm = await environment.setupVM(ctx.environment.options || ctx.config.environmentOptions || {});
78
+ state.durations.environment = performance.now() - state.durations.environment;
79
+ process.env.VITEST_WORKER_ID = String(ctx.workerId);
80
+ process.env.VITEST_POOL_ID = String(workerId);
81
+ process.env.VITEST_VM_POOL = "1";
82
+ if (!vm.getVmContext)
83
+ throw new TypeError(`Environment ${ctx.environment.name} doesn't provide "getVmContext" method. It should return a context created by "vm.createContext" method.`);
84
+ const context = vm.getVmContext();
85
+ if (!isContext(context))
86
+ throw new TypeError(`Environment ${ctx.environment.name} doesn't provide a valid context. It should be created by "vm.createContext" method.`);
87
+ context.__vitest_worker__ = state;
88
+ context.process = process;
89
+ context.global = context;
90
+ context.console = createCustomConsole(state);
91
+ if (ctx.invalidates) {
92
+ ctx.invalidates.forEach((fsPath) => {
93
+ moduleCache.delete(fsPath);
94
+ moduleCache.delete(`mock:${fsPath}`);
95
+ });
96
+ }
97
+ ctx.files.forEach((i) => moduleCache.delete(i));
98
+ const executor = await startVitestExecutor({
99
+ context,
100
+ moduleCache,
101
+ mockMap,
102
+ state
103
+ });
104
+ context.__vitest_mocker__ = executor.mocker;
105
+ const { run: run2 } = await executor.importExternalModule(entryFile);
106
+ try {
107
+ await run2(ctx.files, ctx.config, executor);
108
+ } finally {
109
+ await ((_a = vm.teardown) == null ? void 0 : _a.call(vm));
110
+ state.environmentTeardownRun = true;
111
+ }
112
+ }
113
+
114
+ export { run };
package/dist/worker.js CHANGED
@@ -1,22 +1,28 @@
1
1
  import { performance } from 'node:perf_hooks';
2
- import { c as createBirpc } from './vendor-index.cc463d9e.js';
2
+ import { c as createBirpc } from './vendor-index.b271ebe4.js';
3
3
  import { workerId } from 'tinypool';
4
- import { g as getWorkerState } from './vendor-global.6795f91f.js';
5
- import { s as startViteNode, m as moduleCache, a as mockMap } from './vendor-execute.3576af13.js';
4
+ import { g as getWorkerState } from './vendor-global.97e4527c.js';
5
+ import { l as loadEnvironment } from './vendor-environments.443ecd82.js';
6
+ import { s as startViteNode, m as moduleCache, a as mockMap } from './vendor-execute.9ab1c1a7.js';
6
7
  import { s as setupInspect } from './vendor-inspector.47fc8cbb.js';
7
- import { r as rpcDone } from './vendor-rpc.ad5b08c7.js';
8
- import '@vitest/utils';
8
+ import { r as rpcDone, c as createSafeRpc } from './vendor-rpc.cbd8e972.js';
9
9
  import 'node:url';
10
+ import 'pathe';
11
+ import 'local-pkg';
12
+ import 'node:console';
13
+ import 'node:vm';
10
14
  import 'vite-node/client';
11
15
  import 'vite-node/utils';
12
- import 'pathe';
13
16
  import '@vitest/utils/error';
14
17
  import './vendor-paths.84fc7a99.js';
15
18
  import 'node:fs';
16
- import '@vitest/spy';
19
+ import '@vitest/utils';
20
+ import './vendor-base.9c08bbd0.js';
21
+ import 'node:path';
17
22
  import 'node:module';
23
+ import 'vite-node/constants';
18
24
 
19
- function init(ctx) {
25
+ async function init(ctx) {
20
26
  if (typeof __vitest_worker__ !== "undefined" && ctx.config.threads && ctx.config.isolate)
21
27
  throw new Error(`worker for ${ctx.files.join(",")} already initialized by ${getWorkerState().ctx.files.join(",")}. This is probably an internal bug of Vitest.`);
22
28
  const { config, port, workerId: workerId$1 } = ctx;
@@ -27,32 +33,37 @@ function init(ctx) {
27
33
  const onCancel = new Promise((resolve) => {
28
34
  setCancel = resolve;
29
35
  });
30
- globalThis.__vitest_environment__ = config.environment;
31
- globalThis.__vitest_worker__ = {
36
+ const rpc = createBirpc(
37
+ {
38
+ onCancel: setCancel
39
+ },
40
+ {
41
+ eventNames: ["onUserConsoleLog", "onFinished", "onCollected", "onWorkerExit", "onCancel"],
42
+ post(v) {
43
+ port.postMessage(v);
44
+ },
45
+ on(fn) {
46
+ port.addListener("message", fn);
47
+ }
48
+ }
49
+ );
50
+ const environment = await loadEnvironment(ctx.environment.name, ctx.config.root);
51
+ if (ctx.environment.transformMode)
52
+ environment.transformMode = ctx.environment.transformMode;
53
+ const state = {
32
54
  ctx,
33
55
  moduleCache,
34
56
  config,
35
57
  mockMap,
36
58
  onCancel,
59
+ environment,
37
60
  durations: {
38
61
  environment: 0,
39
62
  prepare: performance.now()
40
63
  },
41
- rpc: createBirpc(
42
- {
43
- onCancel: setCancel
44
- },
45
- {
46
- eventNames: ["onUserConsoleLog", "onFinished", "onCollected", "onWorkerExit", "onCancel"],
47
- post(v) {
48
- port.postMessage(v);
49
- },
50
- on(fn) {
51
- port.addListener("message", fn);
52
- }
53
- }
54
- )
64
+ rpc: createSafeRpc(rpc)
55
65
  };
66
+ globalThis.__vitest_worker__ = state;
56
67
  if (ctx.invalidates) {
57
68
  ctx.invalidates.forEach((fsPath) => {
58
69
  moduleCache.delete(fsPath);
@@ -60,13 +71,14 @@ function init(ctx) {
60
71
  });
61
72
  }
62
73
  ctx.files.forEach((i) => moduleCache.delete(i));
74
+ return state;
63
75
  }
64
76
  async function run(ctx) {
65
77
  const inspectorCleanup = setupInspect(ctx.config);
66
78
  try {
67
- init(ctx);
68
- const { run: run2, executor } = await startViteNode(ctx);
69
- await run2(ctx.files, ctx.config, ctx.environment, executor);
79
+ const state = await init(ctx);
80
+ const { run: run2, executor } = await startViteNode({ state });
81
+ await run2(ctx.files, ctx.config, { environment: state.environment, options: ctx.environment.options }, executor);
70
82
  await rpcDone();
71
83
  } finally {
72
84
  inspectorCleanup();
package/execute.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './dist/execute.js'
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "vitest",
3
3
  "type": "module",
4
- "version": "0.33.0",
4
+ "version": "0.34.0",
5
5
  "description": "A blazing fast unit test framework powered by Vite",
6
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
7
7
  "license": "MIT",
@@ -45,6 +45,10 @@
45
45
  "types": "./dist/node.d.ts",
46
46
  "import": "./dist/node.js"
47
47
  },
48
+ "./execute": {
49
+ "types": "./dist/execute.d.ts",
50
+ "import": "./dist/execute.js"
51
+ },
48
52
  "./browser": {
49
53
  "types": "./dist/browser.d.ts",
50
54
  "import": "./dist/browser.js"
@@ -144,21 +148,20 @@
144
148
  "std-env": "^3.3.3",
145
149
  "strip-literal": "^1.0.1",
146
150
  "tinybench": "^2.5.0",
147
- "tinypool": "^0.6.0",
151
+ "tinypool": "^0.7.0",
148
152
  "vite": "^3.0.0 || ^4.0.0",
149
153
  "why-is-node-running": "^2.2.2",
150
- "@vitest/expect": "0.33.0",
151
- "@vitest/runner": "0.33.0",
152
- "@vitest/snapshot": "0.33.0",
153
- "@vitest/spy": "0.33.0",
154
- "@vitest/utils": "0.33.0",
155
- "vite-node": "0.33.0"
154
+ "@vitest/runner": "0.34.0",
155
+ "@vitest/utils": "0.34.0",
156
+ "vite-node": "0.34.0",
157
+ "@vitest/snapshot": "0.34.0",
158
+ "@vitest/spy": "0.34.0",
159
+ "@vitest/expect": "0.34.0"
156
160
  },
157
161
  "devDependencies": {
158
162
  "@ampproject/remapping": "^2.2.1",
159
163
  "@antfu/install-pkg": "^0.1.1",
160
164
  "@edge-runtime/vm": "3.0.3",
161
- "@jridgewell/trace-mapping": "^0.3.18",
162
165
  "@sinonjs/fake-timers": "^11.0.0",
163
166
  "@types/diff": "^5.0.3",
164
167
  "@types/estree": "^1.0.1",
@@ -5,6 +5,8 @@ const ignoreWarnings = new Set([
5
5
  '--experimental-loader is an experimental feature. This feature could change at any time',
6
6
  'Custom ESM Loaders is an experimental feature. This feature could change at any time',
7
7
  'Custom ESM Loaders is an experimental feature and might change at any time',
8
+ 'VM Modules is an experimental feature and might change at any time',
9
+ 'VM Modules is an experimental feature. This feature could change at any time',
8
10
  ])
9
11
 
10
12
  const { emit } = process