@vitest/runner 0.31.0 → 0.31.2

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.
@@ -85,13 +85,13 @@ function normalizeErrorMessage(message) {
85
85
  }
86
86
  function processError(err, options = {}) {
87
87
  if (!err || typeof err !== "object")
88
- return err;
88
+ return { message: err };
89
89
  if (err.stack)
90
90
  err.stackStr = String(err.stack);
91
91
  if (err.name)
92
92
  err.nameStr = String(err.name);
93
- const clonedActual = deepClone(err.actual);
94
- const clonedExpected = deepClone(err.expected);
93
+ const clonedActual = deepClone(err.actual, { forceWritable: true });
94
+ const clonedExpected = deepClone(err.expected, { forceWritable: true });
95
95
  const { replacedActual, replacedExpected } = replaceAsymmetricMatcher(clonedActual, clonedExpected);
96
96
  if (err.showDiff || err.showDiff === void 0 && err.expected !== void 0 && err.actual !== void 0)
97
97
  err.diff = unifiedDiff(replacedActual, replacedExpected, options);
@@ -238,6 +238,9 @@ function createChainable(keys, fn) {
238
238
  };
239
239
  Object.assign(chain2, fn);
240
240
  chain2.withContext = () => chain2.bind(context);
241
+ chain2.setContext = (key, value) => {
242
+ context[key] = value;
243
+ };
241
244
  for (const key of keys) {
242
245
  Object.defineProperty(chain2, key, {
243
246
  get() {
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { V as VitestRunner } from './runner-7aa38127.js';
2
- export { C as CancelReason, a as VitestRunnerConfig, c as VitestRunnerConstructor, b as VitestRunnerImportSource } from './runner-7aa38127.js';
3
- import { T as Task, F as File, S as SuiteAPI, a as TestAPI, b as SuiteCollector, c as SuiteHooks, O as OnTestFailedHandler, d as Test } from './tasks-2a410173.js';
4
- export { D as DoneCallback, m as HookCleanupCallback, H as HookListener, R as RunMode, o as RuntimeContext, q as SequenceHooks, r as SequenceSetupFiles, j as Suite, n as SuiteFactory, f as TaskBase, g as TaskCustom, h as TaskResult, i as TaskResultPack, e as TaskState, p as TestContext, k as TestFunction, l as TestOptions } from './tasks-2a410173.js';
1
+ import { V as VitestRunner } from './runner-6af7d148.js';
2
+ export { C as CancelReason, a as VitestRunnerConfig, c as VitestRunnerConstructor, b as VitestRunnerImportSource } from './runner-6af7d148.js';
3
+ import { T as Task, F as File, S as SuiteAPI, a as TestAPI, b as SuiteCollector, c as SuiteHooks, O as OnTestFailedHandler, d as Test } from './tasks-900f5000.js';
4
+ export { D as DoneCallback, n as HookCleanupCallback, H as HookListener, R as RunMode, p as RuntimeContext, r as SequenceHooks, s as SequenceSetupFiles, k as Suite, o as SuiteFactory, f as TaskBase, h as TaskCustom, g as TaskMeta, i as TaskResult, j as TaskResultPack, e as TaskState, q as TestContext, l as TestFunction, m as TestOptions } from './tasks-900f5000.js';
5
5
  import { Awaitable } from '@vitest/utils';
6
6
 
7
7
  declare function updateTask(task: Task, runner: VitestRunner): void;
package/dist/index.js CHANGED
@@ -54,6 +54,7 @@ function createTestContext(test, runner) {
54
54
  throw new Error("done() callback is deprecated, use promise instead");
55
55
  };
56
56
  context.meta = test;
57
+ context.task = test;
57
58
  context.onTestFailed = (fn) => {
58
59
  test.onFailed || (test.onFailed = []);
59
60
  test.onFailed.push(fn);
@@ -65,9 +66,29 @@ function makeTimeoutMsg(isHook, timeout) {
65
66
  If this is a long-running ${isHook ? "hook" : "test"}, pass a timeout value as the last argument or configure it globally with "${isHook ? "hookTimeout" : "testTimeout"}".`;
66
67
  }
67
68
 
69
+ var version = "0.31.2";
70
+
71
+ function getVersion() {
72
+ return globalThis.__vitest_runner_version__ || version;
73
+ }
74
+ function markVersion() {
75
+ globalThis.__vitest_runner_version__ = version;
76
+ }
77
+ function checkVersion() {
78
+ const collectVersion = getVersion();
79
+ if (collectVersion !== version) {
80
+ const error = `Version mismatch: Vitest started as ${collectVersion}, but tests are collected with ${version} version.
81
+
82
+ - If you are using global Vitest, make sure your package has the same version.
83
+ - If you have a monorepo setup, make sure your main package has the same version as your test packages.`;
84
+ throw new Error(error);
85
+ }
86
+ }
87
+
68
88
  const suite = createSuite();
69
89
  const test = createTest(
70
90
  function(name, fn, options) {
91
+ checkVersion();
71
92
  getCurrentSuite().test.fn.call(this, name, fn, options);
72
93
  }
73
94
  );
@@ -101,18 +122,20 @@ function createSuiteHooks() {
101
122
  };
102
123
  }
103
124
  function createSuiteCollector(name, factory = () => {
104
- }, mode, concurrent, shuffle, suiteOptions) {
125
+ }, mode, concurrent, shuffle, each, suiteOptions) {
105
126
  const tasks = [];
106
127
  const factoryQueue = [];
107
128
  let suite2;
108
129
  initSuite();
109
- const test2 = createTest(function(name2, fn = noop, options = suiteOptions) {
130
+ const test2 = createTest(function(name2, fn = noop, options) {
110
131
  const mode2 = this.only ? "only" : this.skip ? "skip" : this.todo ? "todo" : "run";
111
132
  if (typeof options === "number")
112
133
  options = { timeout: options };
113
134
  if (typeof suiteOptions === "object") {
114
135
  options = {
115
- ...suiteOptions,
136
+ repeats: suiteOptions.repeats,
137
+ retry: suiteOptions.retry,
138
+ timeout: suiteOptions.timeout,
116
139
  ...options
117
140
  };
118
141
  }
@@ -120,11 +143,13 @@ function createSuiteCollector(name, factory = () => {
120
143
  id: "",
121
144
  type: "test",
122
145
  name: name2,
146
+ each: this.each,
123
147
  mode: mode2,
124
148
  suite: void 0,
125
149
  fails: this.fails,
126
150
  retry: options == null ? void 0 : options.retry,
127
- repeats: options == null ? void 0 : options.repeats
151
+ repeats: options == null ? void 0 : options.repeats,
152
+ meta: /* @__PURE__ */ Object.create(null)
128
153
  };
129
154
  if (this.concurrent || concurrent)
130
155
  test3.concurrent = true;
@@ -147,7 +172,8 @@ function createSuiteCollector(name, factory = () => {
147
172
  id: "",
148
173
  name: name2,
149
174
  type: "custom",
150
- mode: self.only ? "only" : self.skip ? "skip" : self.todo ? "todo" : "run"
175
+ mode: self.only ? "only" : self.skip ? "skip" : self.todo ? "todo" : "run",
176
+ meta: /* @__PURE__ */ Object.create(null)
151
177
  };
152
178
  tasks.push(task);
153
179
  return task;
@@ -156,6 +182,7 @@ function createSuiteCollector(name, factory = () => {
156
182
  type: "collector",
157
183
  name,
158
184
  mode,
185
+ options: suiteOptions,
159
186
  test: test2,
160
187
  tasks,
161
188
  collect,
@@ -174,8 +201,10 @@ function createSuiteCollector(name, factory = () => {
174
201
  type: "suite",
175
202
  name,
176
203
  mode,
204
+ each,
177
205
  shuffle,
178
- tasks: []
206
+ tasks: [],
207
+ meta: /* @__PURE__ */ Object.create(null)
179
208
  };
180
209
  setHooks(suite2, createSuiteHooks());
181
210
  }
@@ -205,11 +234,20 @@ function createSuiteCollector(name, factory = () => {
205
234
  }
206
235
  function createSuite() {
207
236
  function suiteFn(name, factory, options) {
237
+ var _a;
238
+ checkVersion();
208
239
  const mode = this.only ? "only" : this.skip ? "skip" : this.todo ? "todo" : "run";
209
- return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, options);
240
+ const currentSuite = getCurrentSuite();
241
+ if (typeof options === "number")
242
+ options = { timeout: options };
243
+ if (currentSuite && typeof ((_a = currentSuite.options) == null ? void 0 : _a.repeats) === "number") {
244
+ options = { repeats: currentSuite.options.repeats, ...options };
245
+ }
246
+ return createSuiteCollector(name, factory, mode, this.concurrent, this.shuffle, this.each, options);
210
247
  }
211
248
  suiteFn.each = function(cases, ...args) {
212
249
  const suite2 = this.withContext();
250
+ this.setContext("each", true);
213
251
  if (Array.isArray(cases) && args.length)
214
252
  cases = formatTemplateString(cases, args);
215
253
  return (name, fn, options) => {
@@ -218,6 +256,7 @@ function createSuite() {
218
256
  const items = Array.isArray(i) ? i : [i];
219
257
  arrayOnlyCases ? suite2(formatTitle(name, items, idx), () => fn(...items), options) : suite2(formatTitle(name, items, idx), () => fn(i), options);
220
258
  });
259
+ this.setContext("each", void 0);
221
260
  };
222
261
  };
223
262
  suiteFn.skipIf = (condition) => condition ? suite.skip : suite;
@@ -231,6 +270,7 @@ function createTest(fn) {
231
270
  const testFn = fn;
232
271
  testFn.each = function(cases, ...args) {
233
272
  const test2 = this.withContext();
273
+ this.setContext("each", true);
234
274
  if (Array.isArray(cases) && args.length)
235
275
  cases = formatTemplateString(cases, args);
236
276
  return (name, fn2, options) => {
@@ -239,6 +279,7 @@ function createTest(fn) {
239
279
  const items = Array.isArray(i) ? i : [i];
240
280
  arrayOnlyCases ? test2(formatTitle(name, items, idx), () => fn2(...items), options) : test2(formatTitle(name, items, idx), () => fn2(i), options);
241
281
  });
282
+ this.setContext("each", void 0);
242
283
  };
243
284
  };
244
285
  testFn.skipIf = (condition) => condition ? test.skip : test;
@@ -305,6 +346,7 @@ async function collectTests(paths, runner) {
305
346
  mode: "run",
306
347
  filepath,
307
348
  tasks: [],
349
+ meta: /* @__PURE__ */ Object.create(null),
308
350
  projectName: config.name
309
351
  };
310
352
  clearCollectorContext(runner);
@@ -401,7 +443,7 @@ const packs = /* @__PURE__ */ new Map();
401
443
  let updateTimer;
402
444
  let previousUpdate;
403
445
  function updateTask(task, runner) {
404
- packs.set(task.id, task.result);
446
+ packs.set(task.id, [task.result, task.meta]);
405
447
  const { clearTimeout, setTimeout } = getSafeTimers();
406
448
  clearTimeout(updateTimer);
407
449
  updateTimer = setTimeout(() => {
@@ -414,7 +456,14 @@ async function sendTasksUpdate(runner) {
414
456
  clearTimeout(updateTimer);
415
457
  await previousUpdate;
416
458
  if (packs.size) {
417
- const p = (_a = runner.onTaskUpdate) == null ? void 0 : _a.call(runner, Array.from(packs));
459
+ const taskPacks = Array.from(packs).map(([id, task]) => {
460
+ return [
461
+ id,
462
+ task[0],
463
+ task[1]
464
+ ];
465
+ });
466
+ const p = (_a = runner.onTaskUpdate) == null ? void 0 : _a.call(runner, taskPacks);
418
467
  packs.clear();
419
468
  return p;
420
469
  }
@@ -617,6 +666,7 @@ async function runFiles(files, runner) {
617
666
  }
618
667
  async function startTests(paths, runner) {
619
668
  var _a, _b, _c, _d;
669
+ markVersion();
620
670
  await ((_a = runner.onBeforeCollect) == null ? void 0 : _a.call(runner, paths));
621
671
  const files = await collectTests(paths, runner);
622
672
  (_b = runner.onCollected) == null ? void 0 : _b.call(runner, files);
@@ -1,4 +1,4 @@
1
- import { q as SequenceHooks, r as SequenceSetupFiles, F as File, d as Test, j as Suite, h as TaskResult, p as TestContext } from './tasks-2a410173.js';
1
+ import { r as SequenceHooks, s as SequenceSetupFiles, F as File, d as Test, k as Suite, j as TaskResultPack, q as TestContext } from './tasks-900f5000.js';
2
2
 
3
3
  interface VitestRunnerConfig {
4
4
  root: string;
@@ -83,7 +83,7 @@ interface VitestRunner {
83
83
  /**
84
84
  * Called, when a task is updated. The same as "onTaskUpdate" in a reporter, but this is running in the same thread as tests.
85
85
  */
86
- onTaskUpdate?(task: [string, TaskResult | undefined][]): Promise<void>;
86
+ onTaskUpdate?(task: TaskResultPack[]): Promise<void>;
87
87
  /**
88
88
  * Called before running all tests in collected paths.
89
89
  */
@@ -15,15 +15,18 @@ interface TaskBase {
15
15
  id: string;
16
16
  name: string;
17
17
  mode: RunMode;
18
+ meta: TaskMeta;
19
+ each?: boolean;
18
20
  concurrent?: boolean;
19
21
  shuffle?: boolean;
20
22
  suite?: Suite;
21
23
  file?: File;
22
24
  result?: TaskResult;
23
25
  retry?: number;
24
- meta?: any;
25
26
  repeats?: number;
26
27
  }
28
+ interface TaskMeta {
29
+ }
27
30
  interface TaskCustom extends TaskBase {
28
31
  type: 'custom';
29
32
  }
@@ -42,7 +45,7 @@ interface TaskResult {
42
45
  retryCount?: number;
43
46
  repeatCount?: number;
44
47
  }
45
- type TaskResultPack = [id: string, result: TaskResult | undefined];
48
+ type TaskResultPack = [id: string, result: TaskResult | undefined, meta: TaskMeta];
46
49
  interface Suite extends TaskBase {
47
50
  type: 'suite';
48
51
  tasks: Task[];
@@ -114,9 +117,11 @@ interface TestOptions {
114
117
  */
115
118
  retry?: number;
116
119
  /**
117
- * How many times the test will repeat.
120
+ * How many times the test will run.
121
+ * Only inner tests will repeat if set on `describe()`, nested `describe()` will inherit parent's repeat by default.
122
+ *
123
+ * @default 1
118
124
  *
119
- * @default 5
120
125
  */
121
126
  repeats?: number;
122
127
  }
@@ -141,14 +146,15 @@ type SuiteAPI<ExtraContext = {}> = ChainableSuiteAPI<ExtraContext> & {
141
146
  type HookListener<T extends any[], Return = void> = (...args: T) => Awaitable<Return>;
142
147
  type HookCleanupCallback = (() => Awaitable<unknown>) | void;
143
148
  interface SuiteHooks<ExtraContext = {}> {
144
- beforeAll: HookListener<[Suite | File], HookCleanupCallback>[];
145
- afterAll: HookListener<[Suite | File]>[];
146
- beforeEach: HookListener<[TestContext & ExtraContext, Suite], HookCleanupCallback>[];
147
- afterEach: HookListener<[TestContext & ExtraContext, Suite]>[];
149
+ beforeAll: HookListener<[Readonly<Suite | File>], HookCleanupCallback>[];
150
+ afterAll: HookListener<[Readonly<Suite | File>]>[];
151
+ beforeEach: HookListener<[TestContext & ExtraContext, Readonly<Suite>], HookCleanupCallback>[];
152
+ afterEach: HookListener<[TestContext & ExtraContext, Readonly<Suite>]>[];
148
153
  }
149
154
  interface SuiteCollector<ExtraContext = {}> {
150
155
  readonly name: string;
151
156
  readonly mode: RunMode;
157
+ options?: TestOptions;
152
158
  type: 'collector';
153
159
  test: TestAPI<ExtraContext>;
154
160
  tasks: (Suite | TaskCustom | Test | SuiteCollector<ExtraContext>)[];
@@ -165,8 +171,14 @@ interface RuntimeContext {
165
171
  interface TestContext {
166
172
  /**
167
173
  * Metadata of the current test
174
+ *
175
+ * @deprecated Use `task` instead
168
176
  */
169
177
  meta: Readonly<Test>;
178
+ /**
179
+ * Metadata of the current test
180
+ */
181
+ task: Readonly<Test>;
170
182
  /**
171
183
  * Extract hooks on test failed
172
184
  */
@@ -176,4 +188,4 @@ type OnTestFailedHandler = (result: TaskResult) => Awaitable<void>;
176
188
  type SequenceHooks = 'stack' | 'list' | 'parallel';
177
189
  type SequenceSetupFiles = 'list' | 'parallel';
178
190
 
179
- export { ChainableFunction as C, DoneCallback as D, File as F, HookListener as H, OnTestFailedHandler as O, RunMode as R, SuiteAPI as S, Task as T, TestAPI as a, SuiteCollector as b, SuiteHooks as c, Test as d, TaskState as e, TaskBase as f, TaskCustom as g, TaskResult as h, TaskResultPack as i, Suite as j, TestFunction as k, TestOptions as l, HookCleanupCallback as m, SuiteFactory as n, RuntimeContext as o, TestContext as p, SequenceHooks as q, SequenceSetupFiles as r, createChainable as s };
191
+ export { ChainableFunction as C, DoneCallback as D, File as F, HookListener as H, OnTestFailedHandler as O, RunMode as R, SuiteAPI as S, Task as T, TestAPI as a, SuiteCollector as b, SuiteHooks as c, Test as d, TaskState as e, TaskBase as f, TaskMeta as g, TaskCustom as h, TaskResult as i, TaskResultPack as j, Suite as k, TestFunction as l, TestOptions as m, HookCleanupCallback as n, SuiteFactory as o, RuntimeContext as p, TestContext as q, SequenceHooks as r, SequenceSetupFiles as s, createChainable as t };
package/dist/types.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { D as DoneCallback, F as File, m as HookCleanupCallback, H as HookListener, O as OnTestFailedHandler, R as RunMode, o as RuntimeContext, q as SequenceHooks, r as SequenceSetupFiles, j as Suite, S as SuiteAPI, b as SuiteCollector, n as SuiteFactory, c as SuiteHooks, T as Task, f as TaskBase, g as TaskCustom, h as TaskResult, i as TaskResultPack, e as TaskState, d as Test, a as TestAPI, p as TestContext, k as TestFunction, l as TestOptions } from './tasks-2a410173.js';
2
- export { C as CancelReason, V as VitestRunner, a as VitestRunnerConfig, c as VitestRunnerConstructor, b as VitestRunnerImportSource } from './runner-7aa38127.js';
1
+ export { D as DoneCallback, F as File, n as HookCleanupCallback, H as HookListener, O as OnTestFailedHandler, R as RunMode, p as RuntimeContext, r as SequenceHooks, s as SequenceSetupFiles, k as Suite, S as SuiteAPI, b as SuiteCollector, o as SuiteFactory, c as SuiteHooks, T as Task, f as TaskBase, h as TaskCustom, g as TaskMeta, i as TaskResult, j as TaskResultPack, e as TaskState, d as Test, a as TestAPI, q as TestContext, l as TestFunction, m as TestOptions } from './tasks-900f5000.js';
2
+ export { C as CancelReason, V as VitestRunner, a as VitestRunnerConfig, c as VitestRunnerConstructor, b as VitestRunnerImportSource } from './runner-6af7d148.js';
3
3
  import '@vitest/utils';
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { j as Suite, T as Task, d as Test, g as TaskCustom } from './tasks-2a410173.js';
2
- export { C as ChainableFunction, s as createChainable } from './tasks-2a410173.js';
1
+ import { k as Suite, T as Task, d as Test, h as TaskCustom } from './tasks-900f5000.js';
2
+ export { C as ChainableFunction, t as createChainable } from './tasks-900f5000.js';
3
3
  import { Arrayable } from '@vitest/utils';
4
4
  export { ErrorWithDiff, ParsedStack } from '@vitest/utils';
5
5
  import { DiffOptions } from '@vitest/utils/diff';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/runner",
3
3
  "type": "module",
4
- "version": "0.31.0",
4
+ "version": "0.31.2",
5
5
  "description": "Vitest test runner",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -41,7 +41,7 @@
41
41
  "concordance": "^5.0.4",
42
42
  "p-limit": "^4.0.0",
43
43
  "pathe": "^1.1.0",
44
- "@vitest/utils": "0.31.0"
44
+ "@vitest/utils": "0.31.2"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "rimraf dist && rollup -c",