@vitest/runner 2.0.5 → 2.1.0-beta.1

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.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { VitestRunner } from './types.js';
2
2
  export { CancelReason, VitestRunnerConfig, VitestRunnerConstructor, VitestRunnerImportSource } from './types.js';
3
- import { T as Task, F as File, d as SuiteAPI, e as TestAPI, f as SuiteCollector, g as CustomAPI, B as BeforeAllListener, A as AfterAllListener, h as BeforeEachListener, i as AfterEachListener, j as TaskHook, O as OnTestFailedHandler, k as OnTestFinishedHandler, a as Test, C as Custom, S as Suite, l as SuiteHooks } from './tasks-zB5uPauP.js';
4
- export { D as DoneCallback, L as ExtendedContext, w as Fixture, v as FixtureFn, u as FixtureOptions, x as Fixtures, y as HookCleanupCallback, H as HookListener, I as InferFixturesTypes, R as RunMode, G as RuntimeContext, M as SequenceHooks, N as SequenceSetupFiles, E as SuiteFactory, n as TaskBase, K as TaskContext, z as TaskCustomOptions, p as TaskMeta, o as TaskPopulated, q as TaskResult, r as TaskResultPack, m as TaskState, J as TestContext, s as TestFunction, t as TestOptions, U as Use } from './tasks-zB5uPauP.js';
3
+ import { T as Task, F as File, d as SuiteAPI, e as TestAPI, f as SuiteCollector, g as CustomAPI, B as BeforeAllListener, A as AfterAllListener, h as BeforeEachListener, i as AfterEachListener, j as TaskHook, O as OnTestFailedHandler, k as OnTestFinishedHandler, a as Test, C as Custom, S as Suite, l as SuiteHooks } from './tasks-C13WjyUB.js';
4
+ export { D as DoneCallback, L as ExtendedContext, w as Fixture, v as FixtureFn, u as FixtureOptions, x as Fixtures, y as HookCleanupCallback, H as HookListener, I as InferFixturesTypes, R as RunMode, G as RuntimeContext, M as SequenceHooks, N as SequenceSetupFiles, E as SuiteFactory, n as TaskBase, K as TaskContext, z as TaskCustomOptions, p as TaskMeta, o as TaskPopulated, q as TaskResult, r as TaskResultPack, m as TaskState, J as TestContext, s as TestFunction, t as TestOptions, U as Use } from './tasks-C13WjyUB.js';
5
5
  import { Awaitable } from '@vitest/utils';
6
6
  export { processError } from '@vitest/utils/error';
7
7
  import '@vitest/utils/diff';
package/dist/index.js CHANGED
@@ -307,13 +307,20 @@ const it = test;
307
307
  let runner;
308
308
  let defaultSuite;
309
309
  let currentTestFilepath;
310
+ function assert(condition, message) {
311
+ if (!condition) {
312
+ throw new Error(`Vitest failed to find ${message}. This is a bug in Vitest. Please, open an issue with reproduction.`);
313
+ }
314
+ }
310
315
  function getDefaultSuite() {
316
+ assert(defaultSuite, "the default suite");
311
317
  return defaultSuite;
312
318
  }
313
319
  function getTestFilepath() {
314
320
  return currentTestFilepath;
315
321
  }
316
322
  function getRunner() {
323
+ assert(runner, "the runner");
317
324
  return runner;
318
325
  }
319
326
  function createDefaultSuite(runner2) {
@@ -333,7 +340,9 @@ function clearCollectorContext(filepath, currentRunner) {
333
340
  collectorContext.currentSuite = defaultSuite;
334
341
  }
335
342
  function getCurrentSuite() {
336
- return collectorContext.currentSuite || defaultSuite;
343
+ const currentSuite = collectorContext.currentSuite || defaultSuite;
344
+ assert(currentSuite, "the current suite");
345
+ return currentSuite;
337
346
  }
338
347
  function createSuiteHooks() {
339
348
  return {
@@ -518,7 +527,7 @@ function createSuiteCollector(name, factory = () => {
518
527
  function createSuite() {
519
528
  function suiteFn(name, factoryOrOptions, optionsOrFactory = {}) {
520
529
  const mode = this.only ? "only" : this.skip ? "skip" : this.todo ? "todo" : "run";
521
- const currentSuite = getCurrentSuite();
530
+ const currentSuite = collectorContext.currentSuite || defaultSuite;
522
531
  let { options, handler: factory } = parseArguments(
523
532
  factoryOrOptions,
524
533
  optionsOrFactory
@@ -23,74 +23,216 @@ interface FixtureItem extends FixtureOptions {
23
23
  type RunMode = 'run' | 'skip' | 'only' | 'todo';
24
24
  type TaskState = RunMode | 'pass' | 'fail';
25
25
  interface TaskBase {
26
+ /**
27
+ * Unique task identifier. Based on the file id and the position of the task.
28
+ * The id of the file task is based on the file path relative to root and project name.
29
+ * It will not change between runs.
30
+ * @example `1201091390`, `1201091390_0`, `1201091390_0_1`
31
+ */
26
32
  id: string;
33
+ /**
34
+ * Task name provided by the user. If no name was provided, it will be an empty string.
35
+ */
27
36
  name: string;
37
+ /**
38
+ * Task mode.
39
+ * - **skip**: task is skipped
40
+ * - **only**: only this task and other tasks with `only` mode will run
41
+ * - **todo**: task is marked as a todo, alias for `skip`
42
+ * - **run**: task will run or already ran
43
+ */
28
44
  mode: RunMode;
45
+ /**
46
+ * Custom metadata for the task. JSON reporter will save this data.
47
+ */
29
48
  meta: TaskMeta;
49
+ /**
50
+ * Whether the task was produced with `.each()` method.
51
+ */
30
52
  each?: boolean;
53
+ /**
54
+ * Whether the task should run concurrently with other tasks.
55
+ */
31
56
  concurrent?: boolean;
57
+ /**
58
+ * Whether the tasks of the suite run in a random order.
59
+ */
32
60
  shuffle?: boolean;
61
+ /**
62
+ * Suite that this task is part of. File task or the global suite will have no parent.
63
+ */
33
64
  suite?: Suite;
65
+ /**
66
+ * Result of the task. Suite and file tasks will only have the result if there
67
+ * was an error during collection or inside `afterAll`/`beforeAll`.
68
+ */
34
69
  result?: TaskResult;
70
+ /**
71
+ * The amount of times the task should be retried if it fails.
72
+ * @default 0
73
+ */
35
74
  retry?: number;
75
+ /**
76
+ * The amount of times the task should be repeated after the successful run.
77
+ * If the task fails, it will not be retried unless `retry` is specified.
78
+ * @default 0
79
+ */
36
80
  repeats?: number;
81
+ /**
82
+ * Location of the task in the file. This field is populated only if
83
+ * `includeTaskLocation` option is set. It is generated by calling `new Error`
84
+ * and parsing the stack trace, so the location might differ depending on the runtime.
85
+ */
37
86
  location?: {
38
87
  line: number;
39
88
  column: number;
40
89
  };
41
90
  }
42
91
  interface TaskPopulated extends TaskBase {
92
+ /**
93
+ * File task. It's the root task of the file.
94
+ */
43
95
  file: File;
96
+ /**
97
+ * Whether the task was skipped by calling `t.skip()`.
98
+ */
44
99
  pending?: boolean;
45
- result?: TaskResult;
100
+ /**
101
+ * Whether the task should succeed if it fails. If the task fails, it will be marked as passed.
102
+ */
46
103
  fails?: boolean;
104
+ /**
105
+ * Hooks that will run if the task fails. The order depends on the `sequence.hooks` option.
106
+ */
47
107
  onFailed?: OnTestFailedHandler[];
108
+ /**
109
+ * Hooks that will run after the task finishes. The order depends on the `sequence.hooks` option.
110
+ */
48
111
  onFinished?: OnTestFinishedHandler[];
49
112
  /**
50
113
  * Store promises (from async expects) to wait for them before finishing the test
51
114
  */
52
115
  promises?: Promise<any>[];
53
116
  }
117
+ /**
118
+ * Custom metadata that can be used in reporters.
119
+ */
54
120
  interface TaskMeta {
55
121
  }
122
+ /**
123
+ * The result of calling a task.
124
+ */
56
125
  interface TaskResult {
126
+ /**
127
+ * State of the task. Inherits the `task.mode` during collection.
128
+ * When the task has finished, it will be changed to `pass` or `fail`.
129
+ * - **pass**: task ran successfully
130
+ * - **fail**: task failed
131
+ */
57
132
  state: TaskState;
133
+ /**
134
+ * Errors that occurred during the task execution. It is possible to have several errors
135
+ * if `expect.soft()` failed multiple times.
136
+ */
137
+ errors?: ErrorWithDiff[];
138
+ /**
139
+ * How long in milliseconds the task took to run.
140
+ */
58
141
  duration?: number;
142
+ /**
143
+ * Time in milliseconds when the task started running.
144
+ */
59
145
  startTime?: number;
146
+ /**
147
+ * Heap size in bytes after the task finished.
148
+ * Only available if `logHeapUsage` option is set and `process.memoryUsage` is defined.
149
+ */
60
150
  heap?: number;
61
- errors?: ErrorWithDiff[];
62
- htmlError?: string;
151
+ /**
152
+ * State of related to this task hooks. Useful during reporting.
153
+ */
63
154
  hooks?: Partial<Record<keyof SuiteHooks, TaskState>>;
155
+ /**
156
+ * The amount of times the task was retried. The task is retried only if it
157
+ * failed and `retry` option is set.
158
+ */
64
159
  retryCount?: number;
160
+ /**
161
+ * The amount of times the task was repeated. The task is repeated only if
162
+ * `repeats` option is set. This number also contains `retryCount`.
163
+ */
65
164
  repeatCount?: number;
66
165
  }
166
+ /**
167
+ * The tuple representing a single task update.
168
+ * Usually reported after the task finishes.
169
+ */
67
170
  type TaskResultPack = [
171
+ /**
172
+ * Unique task identifier from `task.id`.
173
+ */
68
174
  id: string,
175
+ /**
176
+ * The result of running the task from `task.result`.
177
+ */
69
178
  result: TaskResult | undefined,
179
+ /**
180
+ * Custom metadata from `task.meta`.
181
+ */
70
182
  meta: TaskMeta
71
183
  ];
72
184
  interface Suite extends TaskBase {
73
- file: File;
74
185
  type: 'suite';
186
+ /**
187
+ * File task. It's the root task of the file.
188
+ */
189
+ file: File;
190
+ /**
191
+ * An array of tasks that are part of the suite.
192
+ */
75
193
  tasks: Task[];
76
194
  }
77
195
  interface File extends Suite {
196
+ /**
197
+ * The name of the pool that the file belongs to.
198
+ * @default 'forks'
199
+ */
78
200
  pool?: string;
201
+ /**
202
+ * The path to the file in UNIX format.
203
+ */
79
204
  filepath: string;
205
+ /**
206
+ * The name of the workspace project the file belongs to.
207
+ */
80
208
  projectName: string | undefined;
209
+ /**
210
+ * The time it took to collect all tests in the file.
211
+ * This time also includes importing all the file dependencies.
212
+ */
81
213
  collectDuration?: number;
214
+ /**
215
+ * The time it took to import the setup file.
216
+ */
82
217
  setupDuration?: number;
83
218
  /**
84
219
  * Whether the file is initiated without running any tests.
220
+ * This is done to populate state on the server side by Vitest.
85
221
  */
86
222
  local?: boolean;
87
223
  }
88
224
  interface Test<ExtraContext = object> extends TaskPopulated {
89
225
  type: 'test';
226
+ /**
227
+ * Test context that will be passed to the test function.
228
+ */
90
229
  context: TaskContext<Test> & ExtraContext & TestContext;
91
230
  }
92
231
  interface Custom<ExtraContext = object> extends TaskPopulated {
93
232
  type: 'custom';
233
+ /**
234
+ * Task context that will be passed to the test function.
235
+ */
94
236
  context: TaskContext<Custom> & ExtraContext & TestContext;
95
237
  }
96
238
  type Task = Test | Suite | Custom | File;
@@ -156,7 +298,7 @@ interface TestOptions {
156
298
  */
157
299
  retry?: number;
158
300
  /**
159
- * How many times the test will run.
301
+ * How many times the test will run again.
160
302
  * Only inner tests will repeat if set on `describe()`, nested `describe()` will inherit parent's repeat by default.
161
303
  *
162
304
  * @default 0
@@ -198,11 +340,7 @@ type CustomAPI<ExtraContext = object> = ChainableTestAPI<ExtraContext> & Extende
198
340
  [K in keyof T | keyof ExtraContext]: K extends keyof T ? T[K] : K extends keyof ExtraContext ? ExtraContext[K] : never;
199
341
  }>;
200
342
  };
201
- type TestAPI<ExtraContext = object> = ChainableTestAPI<ExtraContext> & ExtendedAPI<ExtraContext> & {
202
- extend: <T extends Record<string, any> = object>(fixtures: Fixtures<T, ExtraContext>) => TestAPI<{
203
- [K in keyof T | keyof ExtraContext]: K extends keyof T ? T[K] : K extends keyof ExtraContext ? ExtraContext[K] : never;
204
- }>;
205
- };
343
+ type TestAPI<ExtraContext = object> = CustomAPI<ExtraContext>;
206
344
  interface FixtureOptions {
207
345
  /**
208
346
  * Whether to automatically set up current fixture, even though it's not being used in tests.
@@ -255,15 +393,23 @@ interface SuiteHooks<ExtraContext = object> {
255
393
  afterEach: AfterEachListener<ExtraContext>[];
256
394
  }
257
395
  interface TaskCustomOptions extends TestOptions {
258
- concurrent?: boolean;
259
- sequential?: boolean;
260
- skip?: boolean;
261
- only?: boolean;
262
- todo?: boolean;
263
- fails?: boolean;
396
+ /**
397
+ * Whether the task was produced with `.each()` method.
398
+ */
264
399
  each?: boolean;
400
+ /**
401
+ * Custom metadata for the task that will be assigned to `task.meta`.
402
+ */
265
403
  meta?: Record<string, unknown>;
404
+ /**
405
+ * Task fixtures.
406
+ */
266
407
  fixtures?: FixtureItem[];
408
+ /**
409
+ * Function that will be called when the task is executed.
410
+ * If nothing is provided, the runner will try to get the function using `getFn(task)`.
411
+ * If the runner cannot find the function, the task will be marked as failed.
412
+ */
267
413
  handler?: (context: TaskContext<Custom>) => Awaitable<void>;
268
414
  }
269
415
  interface SuiteCollector<ExtraContext = object> {
@@ -300,6 +446,7 @@ interface TaskContext<Task extends Custom | Test = Custom | Test> {
300
446
  onTestFinished: (fn: OnTestFinishedHandler) => void;
301
447
  /**
302
448
  * Mark tests as skipped. All execution after this call will be skipped.
449
+ * This function throws an error, so make sure you are not catching it accidentally.
303
450
  */
304
451
  skip: () => void;
305
452
  }
package/dist/types.d.ts CHANGED
@@ -1,8 +1,11 @@
1
- import { M as SequenceHooks, N as SequenceSetupFiles, F as File, T as Task, S as Suite, r as TaskResultPack, a as Test, C as Custom, K as TaskContext, L as ExtendedContext } from './tasks-zB5uPauP.js';
2
- export { A as AfterAllListener, i as AfterEachListener, B as BeforeAllListener, h as BeforeEachListener, g as CustomAPI, D as DoneCallback, w as Fixture, v as FixtureFn, u as FixtureOptions, x as Fixtures, y as HookCleanupCallback, H as HookListener, I as InferFixturesTypes, O as OnTestFailedHandler, k as OnTestFinishedHandler, R as RunMode, G as RuntimeContext, d as SuiteAPI, f as SuiteCollector, E as SuiteFactory, l as SuiteHooks, n as TaskBase, z as TaskCustomOptions, j as TaskHook, p as TaskMeta, o as TaskPopulated, q as TaskResult, m as TaskState, e as TestAPI, J as TestContext, s as TestFunction, t as TestOptions, U as Use } from './tasks-zB5uPauP.js';
1
+ import { M as SequenceHooks, N as SequenceSetupFiles, F as File, T as Task, a as Test, C as Custom, S as Suite, r as TaskResultPack, K as TaskContext, L as ExtendedContext } from './tasks-C13WjyUB.js';
2
+ export { A as AfterAllListener, i as AfterEachListener, B as BeforeAllListener, h as BeforeEachListener, g as CustomAPI, D as DoneCallback, w as Fixture, v as FixtureFn, u as FixtureOptions, x as Fixtures, y as HookCleanupCallback, H as HookListener, I as InferFixturesTypes, O as OnTestFailedHandler, k as OnTestFinishedHandler, R as RunMode, G as RuntimeContext, d as SuiteAPI, f as SuiteCollector, E as SuiteFactory, l as SuiteHooks, n as TaskBase, z as TaskCustomOptions, j as TaskHook, p as TaskMeta, o as TaskPopulated, q as TaskResult, m as TaskState, e as TestAPI, J as TestContext, s as TestFunction, t as TestOptions, U as Use } from './tasks-C13WjyUB.js';
3
3
  import { DiffOptions } from '@vitest/utils/diff';
4
4
  import '@vitest/utils';
5
5
 
6
+ /**
7
+ * This is a subset of Vitest config that's required for the runner to work.
8
+ */
6
9
  interface VitestRunnerConfig {
7
10
  root: string;
8
11
  setupFiles: string[];
@@ -65,7 +68,7 @@ interface VitestRunner {
65
68
  /**
66
69
  * When the task has finished running, but before cleanup hooks are called
67
70
  */
68
- onTaskFinished?: (test: Task) => unknown;
71
+ onTaskFinished?: (test: Test | Custom) => unknown;
69
72
  /**
70
73
  * Called after result and state are set.
71
74
  */
@@ -108,7 +111,7 @@ interface VitestRunner {
108
111
  */
109
112
  onAfterRunFiles?: (files: File[]) => unknown;
110
113
  /**
111
- * Called when new context for a test is defined. Useful, if you want to add custom properties to the context.
114
+ * Called when new context for a test is defined. Useful if you want to add custom properties to the context.
112
115
  * If you only want to define custom context, consider using "beforeAll" in "setupFiles" instead.
113
116
  *
114
117
  * This method is called for both "test" and "custom" handlers.
@@ -117,7 +120,7 @@ interface VitestRunner {
117
120
  */
118
121
  extendTaskContext?: <T extends Test | Custom>(context: TaskContext<T>) => ExtendedContext<T>;
119
122
  /**
120
- * Called, when files are imported. Can be called in two situations: when collecting tests and when importing setup files.
123
+ * Called when test and setup files are imported. Can be called in two situations: when collecting tests and when importing setup files.
121
124
  */
122
125
  importFile: (filepath: string, source: VitestRunnerImportSource) => unknown;
123
126
  /**
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { S as Suite, F as File, T as Task, a as Test, C as Custom } from './tasks-zB5uPauP.js';
2
- export { b as ChainableFunction, c as createChainable } from './tasks-zB5uPauP.js';
1
+ import { S as Suite, F as File, T as Task, a as Test, C as Custom } from './tasks-C13WjyUB.js';
2
+ export { b as ChainableFunction, c as createChainable } from './tasks-C13WjyUB.js';
3
3
  import { Arrayable } from '@vitest/utils';
4
4
 
5
5
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vitest/runner",
3
3
  "type": "module",
4
- "version": "2.0.5",
4
+ "version": "2.1.0-beta.1",
5
5
  "description": "Vitest test runner",
6
6
  "license": "MIT",
7
7
  "funding": "https://opencollective.com/vitest",
@@ -39,7 +39,7 @@
39
39
  ],
40
40
  "dependencies": {
41
41
  "pathe": "^1.1.2",
42
- "@vitest/utils": "2.0.5"
42
+ "@vitest/utils": "2.1.0-beta.1"
43
43
  },
44
44
  "scripts": {
45
45
  "build": "rimraf dist && rollup -c",