@vitest-agent/mcp 1.1.0 → 1.2.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.
- package/README.md +1 -2
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/package.json +1 -1
- package/server.js +2 -4
- package/patterns/_meta.json +0 -67
- package/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
- package/patterns/known-issues-and-caveats.md +0 -52
- package/patterns/operating-vitest-agent-as-an-agent.md +0 -62
- package/patterns/running-tests-via-mcp.md +0 -103
- package/patterns/silencing-leaking-output-in-tests.md +0 -91
- package/patterns/testing-effect-schema-definitions.md +0 -71
- package/patterns/testing-effect-services-with-mock-layers.md +0 -63
- package/resources/index.js +0 -166
- package/resources/indexes.js +0 -77
- package/resources/manifest-schema.js +0 -46
- package/resources/paths.js +0 -20
- package/resources/patterns.js +0 -22
- package/resources/upstream-docs.js +0 -22
- package/vendor/vitest-docs/ATTRIBUTION.md +0 -5
- package/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
- package/vendor/vitest-docs/api/advanced/metadata.md +0 -68
- package/vendor/vitest-docs/api/advanced/plugin.md +0 -168
- package/vendor/vitest-docs/api/advanced/reporters.md +0 -342
- package/vendor/vitest-docs/api/advanced/runner.md +0 -334
- package/vendor/vitest-docs/api/advanced/test-case.md +0 -302
- package/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
- package/vendor/vitest-docs/api/advanced/test-module.md +0 -140
- package/vendor/vitest-docs/api/advanced/test-project.md +0 -321
- package/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
- package/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
- package/vendor/vitest-docs/api/advanced/vitest.md +0 -684
- package/vendor/vitest-docs/api/assert-type.md +0 -22
- package/vendor/vitest-docs/api/assert.md +0 -1960
- package/vendor/vitest-docs/api/browser/assertions.md +0 -1277
- package/vendor/vitest-docs/api/browser/commands.md +0 -154
- package/vendor/vitest-docs/api/browser/context.md +0 -338
- package/vendor/vitest-docs/api/browser/interactivity.md +0 -681
- package/vendor/vitest-docs/api/browser/locators.md +0 -1171
- package/vendor/vitest-docs/api/browser/react.md +0 -346
- package/vendor/vitest-docs/api/browser/svelte.md +0 -292
- package/vendor/vitest-docs/api/browser/vue.md +0 -222
- package/vendor/vitest-docs/api/describe.md +0 -374
- package/vendor/vitest-docs/api/expect-typeof.md +0 -571
- package/vendor/vitest-docs/api/expect.md +0 -2304
- package/vendor/vitest-docs/api/hooks.md +0 -463
- package/vendor/vitest-docs/api/mock.md +0 -701
- package/vendor/vitest-docs/api/test.md +0 -926
- package/vendor/vitest-docs/api/vi.md +0 -1372
- package/vendor/vitest-docs/config/alias.md +0 -13
- package/vendor/vitest-docs/config/allowonly.md +0 -32
- package/vendor/vitest-docs/config/api.md +0 -27
- package/vendor/vitest-docs/config/attachmentsdir.md +0 -6
- package/vendor/vitest-docs/config/bail.md +0 -9
- package/vendor/vitest-docs/config/benchmark.md +0 -65
- package/vendor/vitest-docs/config/browser/api.md +0 -23
- package/vendor/vitest-docs/config/browser/commands.md +0 -6
- package/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
- package/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
- package/vendor/vitest-docs/config/browser/enabled.md +0 -40
- package/vendor/vitest-docs/config/browser/expect.md +0 -250
- package/vendor/vitest-docs/config/browser/headless.md +0 -7
- package/vendor/vitest-docs/config/browser/instances.md +0 -47
- package/vendor/vitest-docs/config/browser/isolate.md +0 -11
- package/vendor/vitest-docs/config/browser/locators.md +0 -24
- package/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
- package/vendor/vitest-docs/config/browser/playwright.md +0 -214
- package/vendor/vitest-docs/config/browser/preview.md +0 -32
- package/vendor/vitest-docs/config/browser/provider.md +0 -79
- package/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
- package/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
- package/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
- package/vendor/vitest-docs/config/browser/trace.md +0 -43
- package/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
- package/vendor/vitest-docs/config/browser/ui.md +0 -7
- package/vendor/vitest-docs/config/browser/viewport.md +0 -6
- package/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
- package/vendor/vitest-docs/config/cache.md +0 -26
- package/vendor/vitest-docs/config/chaiconfig.md +0 -29
- package/vendor/vitest-docs/config/clearmocks.md +0 -22
- package/vendor/vitest-docs/config/coverage.md +0 -455
- package/vendor/vitest-docs/config/css.md +0 -47
- package/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
- package/vendor/vitest-docs/config/deps.md +0 -127
- package/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
- package/vendor/vitest-docs/config/diff.md +0 -96
- package/vendor/vitest-docs/config/dir.md +0 -7
- package/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
- package/vendor/vitest-docs/config/env.md +0 -5
- package/vendor/vitest-docs/config/environment.md +0 -96
- package/vendor/vitest-docs/config/environmentoptions.md +0 -30
- package/vendor/vitest-docs/config/exclude.md +0 -49
- package/vendor/vitest-docs/config/execargv.md +0 -10
- package/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
- package/vendor/vitest-docs/config/expect.md +0 -38
- package/vendor/vitest-docs/config/experimental.md +0 -510
- package/vendor/vitest-docs/config/faketimers.md +0 -51
- package/vendor/vitest-docs/config/fileparallelism.md +0 -11
- package/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
- package/vendor/vitest-docs/config/globals.md +0 -42
- package/vendor/vitest-docs/config/globalsetup.md +0 -72
- package/vendor/vitest-docs/config/hideskippedtests.md +0 -7
- package/vendor/vitest-docs/config/hooktimeout.md +0 -7
- package/vendor/vitest-docs/config/include-source.md +0 -115
- package/vendor/vitest-docs/config/include.md +0 -71
- package/vendor/vitest-docs/config/includetasklocation.md +0 -17
- package/vendor/vitest-docs/config/index.md +0 -85
- package/vendor/vitest-docs/config/isolate.md +0 -13
- package/vendor/vitest-docs/config/logheapusage.md +0 -7
- package/vendor/vitest-docs/config/maxconcurrency.md +0 -9
- package/vendor/vitest-docs/config/maxworkers.md +0 -49
- package/vendor/vitest-docs/config/mockreset.md +0 -22
- package/vendor/vitest-docs/config/mode.md +0 -7
- package/vendor/vitest-docs/config/name.md +0 -111
- package/vendor/vitest-docs/config/onconsolelog.md +0 -25
- package/vendor/vitest-docs/config/onstacktrace.md +0 -32
- package/vendor/vitest-docs/config/onunhandlederror.md +0 -35
- package/vendor/vitest-docs/config/open.md +0 -7
- package/vendor/vitest-docs/config/outputfile.md +0 -7
- package/vendor/vitest-docs/config/passwithnotests.md +0 -7
- package/vendor/vitest-docs/config/pool.md +0 -45
- package/vendor/vitest-docs/config/printconsoletrace.md +0 -6
- package/vendor/vitest-docs/config/projects.md +0 -6
- package/vendor/vitest-docs/config/provide.md +0 -45
- package/vendor/vitest-docs/config/reporters.md +0 -69
- package/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
- package/vendor/vitest-docs/config/restoremocks.md +0 -22
- package/vendor/vitest-docs/config/retry.md +0 -140
- package/vendor/vitest-docs/config/root.md +0 -6
- package/vendor/vitest-docs/config/runner.md +0 -6
- package/vendor/vitest-docs/config/sequence.md +0 -158
- package/vendor/vitest-docs/config/server.md +0 -68
- package/vendor/vitest-docs/config/setupfiles.md +0 -40
- package/vendor/vitest-docs/config/silent.md +0 -9
- package/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
- package/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
- package/vendor/vitest-docs/config/snapshotformat.md +0 -28
- package/vendor/vitest-docs/config/snapshotserializers.md +0 -6
- package/vendor/vitest-docs/config/stricttags.md +0 -30
- package/vendor/vitest-docs/config/tags.md +0 -141
- package/vendor/vitest-docs/config/teardowntimeout.md +0 -7
- package/vendor/vitest-docs/config/testnamepattern.md +0 -21
- package/vendor/vitest-docs/config/testtimeout.md +0 -7
- package/vendor/vitest-docs/config/typecheck.md +0 -77
- package/vendor/vitest-docs/config/ui.md +0 -15
- package/vendor/vitest-docs/config/unstubenvs.md +0 -20
- package/vendor/vitest-docs/config/unstubglobals.md +0 -20
- package/vendor/vitest-docs/config/update.md +0 -16
- package/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
- package/vendor/vitest-docs/config/watch.md +0 -11
- package/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
- package/vendor/vitest-docs/guide/advanced/index.md +0 -147
- package/vendor/vitest-docs/guide/advanced/pool.md +0 -148
- package/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
- package/vendor/vitest-docs/guide/advanced/tests.md +0 -125
- package/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
- package/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
- package/vendor/vitest-docs/guide/browser/index.md +0 -630
- package/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
- package/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
- package/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
- package/vendor/vitest-docs/guide/cli-generated.md +0 -972
- package/vendor/vitest-docs/guide/cli.md +0 -234
- package/vendor/vitest-docs/guide/common-errors.md +0 -163
- package/vendor/vitest-docs/guide/coverage.md +0 -515
- package/vendor/vitest-docs/guide/debugging.md +0 -127
- package/vendor/vitest-docs/guide/environment.md +0 -101
- package/vendor/vitest-docs/guide/extending-matchers.md +0 -160
- package/vendor/vitest-docs/guide/features.md +0 -310
- package/vendor/vitest-docs/guide/filtering.md +0 -175
- package/vendor/vitest-docs/guide/ide.md +0 -43
- package/vendor/vitest-docs/guide/improving-performance.md +0 -245
- package/vendor/vitest-docs/guide/in-source.md +0 -159
- package/vendor/vitest-docs/guide/index.md +0 -128
- package/vendor/vitest-docs/guide/learn/async.md +0 -147
- package/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
- package/vendor/vitest-docs/guide/learn/matchers.md +0 -277
- package/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
- package/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
- package/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
- package/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
- package/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
- package/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
- package/vendor/vitest-docs/guide/lifecycle.md +0 -379
- package/vendor/vitest-docs/guide/migration.md +0 -863
- package/vendor/vitest-docs/guide/mocking/classes.md +0 -158
- package/vendor/vitest-docs/guide/mocking/dates.md +0 -52
- package/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
- package/vendor/vitest-docs/guide/mocking/functions.md +0 -61
- package/vendor/vitest-docs/guide/mocking/globals.md +0 -20
- package/vendor/vitest-docs/guide/mocking/modules.md +0 -414
- package/vendor/vitest-docs/guide/mocking/requests.md +0 -114
- package/vendor/vitest-docs/guide/mocking/timers.md +0 -48
- package/vendor/vitest-docs/guide/mocking.md +0 -239
- package/vendor/vitest-docs/guide/open-telemetry.md +0 -156
- package/vendor/vitest-docs/guide/parallelism.md +0 -82
- package/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
- package/vendor/vitest-docs/guide/projects.md +0 -291
- package/vendor/vitest-docs/guide/recipes.md +0 -59
- package/vendor/vitest-docs/guide/reporters.md +0 -723
- package/vendor/vitest-docs/guide/snapshot.md +0 -620
- package/vendor/vitest-docs/guide/test-annotations.md +0 -103
- package/vendor/vitest-docs/guide/test-context.md +0 -902
- package/vendor/vitest-docs/guide/test-tags.md +0 -314
- package/vendor/vitest-docs/guide/testing-types.md +0 -149
- package/vendor/vitest-docs/guide/ui.md +0 -160
- package/vendor/vitest-docs/guide/using-plugins.md +0 -5
- package/vendor/vitest-docs/manifest.json +0 -1691
|
@@ -1,926 +0,0 @@
|
|
|
1
|
-
# Test
|
|
2
|
-
|
|
3
|
-
- **Alias:** `it`
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
function test(
|
|
7
|
-
name: string | Function,
|
|
8
|
-
body?: () => unknown,
|
|
9
|
-
timeout?: number
|
|
10
|
-
): void
|
|
11
|
-
function test(
|
|
12
|
-
name: string | Function,
|
|
13
|
-
options: TestOptions,
|
|
14
|
-
body?: () => unknown,
|
|
15
|
-
): void
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
`test` or `it` defines a set of related expectations. It receives the test name and a function that holds the expectations to test.
|
|
19
|
-
|
|
20
|
-
Optionally, you can provide a timeout (in milliseconds) for specifying how long to wait before terminating, or a set of [additional options](#test-options). The default timeout is 5 seconds, and can be configured globally with [`testTimeout`](/config/testtimeout).
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
import { expect, test } from 'vitest'
|
|
24
|
-
|
|
25
|
-
test('should work as expected', () => {
|
|
26
|
-
expect(Math.sqrt(4)).toBe(2)
|
|
27
|
-
})
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
::: warning
|
|
31
|
-
If the first argument is a function, its `name` property will be used as the name of the test. The function itself will not be called.
|
|
32
|
-
|
|
33
|
-
If test body is not provided, the test is marked as `todo`.
|
|
34
|
-
:::
|
|
35
|
-
|
|
36
|
-
When a test function returns a promise, the runner will wait until it is resolved to collect async expectations. If the promise is rejected, the test will fail.
|
|
37
|
-
|
|
38
|
-
::: tip
|
|
39
|
-
In Jest, `TestFunction` can also be of type `(done: DoneCallback) => void`. If this form is used, the test will not be concluded until `done` is called. You can achieve the same using an `async` function, see the [Migration guide Done Callback section](/guide/migration#done-callback).
|
|
40
|
-
:::
|
|
41
|
-
|
|
42
|
-
## Test Options
|
|
43
|
-
|
|
44
|
-
You can define boolean options by chaining properties on a function:
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
import { test } from 'vitest'
|
|
48
|
-
|
|
49
|
-
test.skip('skipped test', () => {
|
|
50
|
-
// some logic that fails right now
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
test.concurrent.skip('skipped concurrent test', () => {
|
|
54
|
-
// some logic that fails right now
|
|
55
|
-
})
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
But you can also provide an object as a second argument instead:
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
import { test } from 'vitest'
|
|
62
|
-
|
|
63
|
-
test('skipped test', { skip: true }, () => {
|
|
64
|
-
// some logic that fails right now
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
test('skipped concurrent test', { skip: true, concurrent: true }, () => {
|
|
68
|
-
// some logic that fails right now
|
|
69
|
-
})
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
They both work in exactly the same way. To use either one is purely a stylistic choice.
|
|
73
|
-
|
|
74
|
-
### timeout
|
|
75
|
-
|
|
76
|
-
- **Type:** `number`
|
|
77
|
-
- **Default:** `5_000` (configured by [`testTimeout`](/config/testtimeout))
|
|
78
|
-
|
|
79
|
-
Test timeout in milliseconds.
|
|
80
|
-
|
|
81
|
-
::: warning
|
|
82
|
-
Note that if you are providing timeout as the last argument, you cannot use options anymore:
|
|
83
|
-
|
|
84
|
-
```ts
|
|
85
|
-
import { test } from 'vitest'
|
|
86
|
-
|
|
87
|
-
// ✅ this works
|
|
88
|
-
test.skip('heavy test', () => {
|
|
89
|
-
// ...
|
|
90
|
-
}, 10_000)
|
|
91
|
-
|
|
92
|
-
// ❌ this doesn't work
|
|
93
|
-
test('heavy test', { skip: true }, () => {
|
|
94
|
-
// ...
|
|
95
|
-
}, 10_000)
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
However, you can provide a timeout inside the object:
|
|
99
|
-
|
|
100
|
-
```ts
|
|
101
|
-
import { test } from 'vitest'
|
|
102
|
-
|
|
103
|
-
// ✅ this works
|
|
104
|
-
test('heavy test', { skip: true, timeout: 10_000 }, () => {
|
|
105
|
-
// ...
|
|
106
|
-
})
|
|
107
|
-
```
|
|
108
|
-
:::
|
|
109
|
-
|
|
110
|
-
### retry
|
|
111
|
-
|
|
112
|
-
- **Default:** `0` (configured by [`retry`](/config/retry))
|
|
113
|
-
- **Type:**
|
|
114
|
-
|
|
115
|
-
```ts
|
|
116
|
-
type Retry = number | {
|
|
117
|
-
/**
|
|
118
|
-
* The number of times to retry the test if it fails.
|
|
119
|
-
* @default 0
|
|
120
|
-
*/
|
|
121
|
-
count?: number
|
|
122
|
-
/**
|
|
123
|
-
* Delay in milliseconds between retry attempts.
|
|
124
|
-
* @default 0
|
|
125
|
-
*/
|
|
126
|
-
delay?: number
|
|
127
|
-
/**
|
|
128
|
-
* Condition to determine if a test should be retried based on the error.
|
|
129
|
-
* - If a RegExp, it is tested against the error message
|
|
130
|
-
* - If a function, called with the TestError object; return true to retry
|
|
131
|
-
*
|
|
132
|
-
* NOTE: Functions can only be used in test files, not in vitest.config.ts,
|
|
133
|
-
* because the configuration is serialized when passed to worker threads.
|
|
134
|
-
*
|
|
135
|
-
* @default undefined (retry on all errors)
|
|
136
|
-
*/
|
|
137
|
-
condition?: RegExp | ((error: TestError) => boolean)
|
|
138
|
-
}
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
Retry configuration for the test. If a number, specifies how many times to retry. If an object, allows fine-grained retry control.
|
|
142
|
-
|
|
143
|
-
Note that the object configuration is available only since Vitest 4.1.
|
|
144
|
-
|
|
145
|
-
### repeats
|
|
146
|
-
|
|
147
|
-
- **Type:** `number`
|
|
148
|
-
- **Default:** `0`
|
|
149
|
-
|
|
150
|
-
How many times the test will run again. If set to `0` (the default), the test will run only one time.
|
|
151
|
-
|
|
152
|
-
This can be useful for debugging flaky tests.
|
|
153
|
-
|
|
154
|
-
### tags <Version>4.1.0</Version> {#tags}
|
|
155
|
-
|
|
156
|
-
- **Type:** `string[]`
|
|
157
|
-
- **Default:** `[]`
|
|
158
|
-
|
|
159
|
-
Custom user [tags](/guide/test-tags). If the tag is not specified in the [configuration](/config/tags), the test will fail before it starts, unless [`strictTags`](/config/stricttags) is disabled manually.
|
|
160
|
-
|
|
161
|
-
```ts
|
|
162
|
-
import { it } from 'vitest'
|
|
163
|
-
|
|
164
|
-
it('user returns data from db', { tags: ['db', 'flaky'] }, () => {
|
|
165
|
-
// ...
|
|
166
|
-
})
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
### meta <Version>4.1.0</Version> {#meta}
|
|
170
|
-
|
|
171
|
-
- **Type:** `TaskMeta`
|
|
172
|
-
|
|
173
|
-
Attaches custom [metadata](/api/advanced/metadata) available in reporters.
|
|
174
|
-
|
|
175
|
-
::: warning
|
|
176
|
-
Vitest merges top-level properties inherited from suites or tags. However, it does not perform a deep merge of nested objects.
|
|
177
|
-
|
|
178
|
-
```ts
|
|
179
|
-
import { describe, test } from 'vitest'
|
|
180
|
-
|
|
181
|
-
describe(
|
|
182
|
-
'nested meta',
|
|
183
|
-
{
|
|
184
|
-
meta: {
|
|
185
|
-
nested: { object: true, array: false },
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
() => {
|
|
189
|
-
test(
|
|
190
|
-
'overrides part of meta',
|
|
191
|
-
{
|
|
192
|
-
meta: {
|
|
193
|
-
nested: { object: false }
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
({ task }) => {
|
|
197
|
-
// task.meta === { nested: { object: false } }
|
|
198
|
-
// notice array got lost because "nested" object was overridden
|
|
199
|
-
}
|
|
200
|
-
)
|
|
201
|
-
}
|
|
202
|
-
)
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
Prefer using non-nested meta, if possible.
|
|
206
|
-
:::
|
|
207
|
-
|
|
208
|
-
### concurrent
|
|
209
|
-
|
|
210
|
-
- **Type:** `boolean`
|
|
211
|
-
- **Default:** `false` (configured by [`sequence.concurrent`](/config/sequence#sequence-concurrent))
|
|
212
|
-
- **Alias:** [`test.concurrent`](#test-concurrent)
|
|
213
|
-
|
|
214
|
-
Whether this test run concurrently with other concurrent tests in the suite.
|
|
215
|
-
|
|
216
|
-
### sequential
|
|
217
|
-
|
|
218
|
-
- **Type:** `boolean`
|
|
219
|
-
- **Default:** `true`
|
|
220
|
-
- **Alias:** [`test.sequential`](#test-sequential)
|
|
221
|
-
|
|
222
|
-
Whether tests run sequentially. When both `concurrent` and `sequential` are specified, `concurrent` takes precedence.
|
|
223
|
-
|
|
224
|
-
### skip
|
|
225
|
-
|
|
226
|
-
- **Type:** `boolean`
|
|
227
|
-
- **Default:** `false`
|
|
228
|
-
- **Alias:** [`test.skip`](#test-skip)
|
|
229
|
-
|
|
230
|
-
Whether the test should be skipped.
|
|
231
|
-
|
|
232
|
-
### only
|
|
233
|
-
|
|
234
|
-
- **Type:** `boolean`
|
|
235
|
-
- **Default:** `false`
|
|
236
|
-
- **Alias:** [`test.only`](#test-only)
|
|
237
|
-
|
|
238
|
-
Should this test be the only one running in a suite.
|
|
239
|
-
|
|
240
|
-
### todo
|
|
241
|
-
|
|
242
|
-
- **Type:** `boolean`
|
|
243
|
-
- **Default:** `false`
|
|
244
|
-
- **Alias:** [`test.todo`](#test-todo)
|
|
245
|
-
|
|
246
|
-
Whether the test should be skipped and marked as a todo.
|
|
247
|
-
|
|
248
|
-
### fails
|
|
249
|
-
|
|
250
|
-
- **Type:** `boolean`
|
|
251
|
-
- **Default:** `false`
|
|
252
|
-
- **Alias:** [`test.fails`](#test-fails)
|
|
253
|
-
|
|
254
|
-
Whether the test is expected to fail. If it does, the test will pass, otherwise it will fail.
|
|
255
|
-
|
|
256
|
-
## test.extend
|
|
257
|
-
|
|
258
|
-
- **Alias:** `it.extend`
|
|
259
|
-
|
|
260
|
-
Use `test.extend` to extend the test context with custom fixtures. This will return a new `test` and it's also extendable, so you can compose more fixtures or override existing ones by extending it as you need. See [Extend Test Context](/guide/test-context#extend-test-context) for more information.
|
|
261
|
-
|
|
262
|
-
```ts
|
|
263
|
-
import { test as baseTest, expect } from 'vitest'
|
|
264
|
-
|
|
265
|
-
export const test = baseTest
|
|
266
|
-
// Simple value - type is inferred as { port: number; host: string }
|
|
267
|
-
.extend('config', { port: 3000, host: 'localhost' })
|
|
268
|
-
// Function fixture - type is inferred from return value
|
|
269
|
-
.extend('server', async ({ config }) => {
|
|
270
|
-
// TypeScript knows config is { port: number; host: string }
|
|
271
|
-
return `http://${config.host}:${config.port}`
|
|
272
|
-
})
|
|
273
|
-
|
|
274
|
-
test('server uses correct port', ({ config, server }) => {
|
|
275
|
-
// TypeScript knows the types:
|
|
276
|
-
// - config is { port: number; host: string }
|
|
277
|
-
// - server is string
|
|
278
|
-
expect(server).toBe('http://localhost:3000')
|
|
279
|
-
expect(config.port).toBe(3000)
|
|
280
|
-
})
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
## test.override <Version>4.1.0</Version> {#test-override}
|
|
284
|
-
|
|
285
|
-
Use `test.override` to override fixture values for all tests within the current suite and its nested suites. This must be called at the top level of a `describe` block. See [Overriding Fixture Values](/guide/test-context.html#overriding-fixture-values) for more information.
|
|
286
|
-
|
|
287
|
-
```ts
|
|
288
|
-
import { test as baseTest, describe, expect } from 'vitest'
|
|
289
|
-
|
|
290
|
-
const test = baseTest
|
|
291
|
-
.extend('dependency', 'default')
|
|
292
|
-
.extend('dependant', ({ dependency }) => dependency)
|
|
293
|
-
|
|
294
|
-
describe('use scoped values', () => {
|
|
295
|
-
test.override({ dependency: 'new' })
|
|
296
|
-
|
|
297
|
-
test('uses scoped value', ({ dependant }) => {
|
|
298
|
-
// `dependant` uses the new overridden value that is scoped
|
|
299
|
-
// to all tests in this suite
|
|
300
|
-
expect(dependant).toEqual({ dependency: 'new' })
|
|
301
|
-
})
|
|
302
|
-
})
|
|
303
|
-
```
|
|
304
|
-
|
|
305
|
-
## test.scoped <Version>3.1.0</Version> <Deprecated /> {#test-scoped}
|
|
306
|
-
|
|
307
|
-
- **Alias:** `it.scoped`
|
|
308
|
-
|
|
309
|
-
::: danger DEPRECATED
|
|
310
|
-
`test.scoped` is deprecated in favor of [`test.override`](#test-override) and will be removed in a future major version.
|
|
311
|
-
:::
|
|
312
|
-
|
|
313
|
-
Alias of [`test.override`](#test-override)
|
|
314
|
-
|
|
315
|
-
## test.skip
|
|
316
|
-
|
|
317
|
-
- **Alias:** `it.skip`
|
|
318
|
-
|
|
319
|
-
If you want to skip running certain tests, but you don't want to delete the code due to any reason, you can use `test.skip` to avoid running them.
|
|
320
|
-
|
|
321
|
-
```ts
|
|
322
|
-
import { assert, test } from 'vitest'
|
|
323
|
-
|
|
324
|
-
test.skip('skipped test', () => {
|
|
325
|
-
// Test skipped, no error
|
|
326
|
-
assert.equal(Math.sqrt(4), 3)
|
|
327
|
-
})
|
|
328
|
-
```
|
|
329
|
-
|
|
330
|
-
You can also skip test by calling `skip` on its [context](/guide/test-context) dynamically:
|
|
331
|
-
|
|
332
|
-
```ts
|
|
333
|
-
import { assert, test } from 'vitest'
|
|
334
|
-
|
|
335
|
-
test('skipped test', (context) => {
|
|
336
|
-
context.skip()
|
|
337
|
-
// Test skipped, no error
|
|
338
|
-
assert.equal(Math.sqrt(4), 3)
|
|
339
|
-
})
|
|
340
|
-
```
|
|
341
|
-
|
|
342
|
-
If the condition is unknown, you can provide it to the `skip` method as the first arguments:
|
|
343
|
-
|
|
344
|
-
```ts
|
|
345
|
-
import { assert, test } from 'vitest'
|
|
346
|
-
|
|
347
|
-
test('skipped test', (context) => {
|
|
348
|
-
context.skip(Math.random() < 0.5, 'optional message')
|
|
349
|
-
// Test skipped, no error
|
|
350
|
-
assert.equal(Math.sqrt(4), 3)
|
|
351
|
-
})
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
## test.skipIf
|
|
355
|
-
|
|
356
|
-
- **Alias:** `it.skipIf`
|
|
357
|
-
|
|
358
|
-
In some cases you might run tests multiple times with different environments, and some of the tests might be environment-specific. Instead of wrapping the test code with `if`, you can use `test.skipIf` to skip the test whenever the condition is truthy.
|
|
359
|
-
|
|
360
|
-
```ts
|
|
361
|
-
import { assert, test } from 'vitest'
|
|
362
|
-
|
|
363
|
-
const isDev = process.env.NODE_ENV === 'development'
|
|
364
|
-
|
|
365
|
-
test.skipIf(isDev)('prod only test', () => {
|
|
366
|
-
// this test only runs in production
|
|
367
|
-
})
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
## test.runIf
|
|
371
|
-
|
|
372
|
-
- **Alias:** `it.runIf`
|
|
373
|
-
|
|
374
|
-
Opposite of [test.skipIf](#test-skipif).
|
|
375
|
-
|
|
376
|
-
```ts
|
|
377
|
-
import { assert, test } from 'vitest'
|
|
378
|
-
|
|
379
|
-
const isDev = process.env.NODE_ENV === 'development'
|
|
380
|
-
|
|
381
|
-
test.runIf(isDev)('dev only test', () => {
|
|
382
|
-
// this test only runs in development
|
|
383
|
-
})
|
|
384
|
-
```
|
|
385
|
-
|
|
386
|
-
## test.only
|
|
387
|
-
|
|
388
|
-
- **Alias:** `it.only`
|
|
389
|
-
|
|
390
|
-
Use `test.only` to only run certain tests in a given suite. This is useful when debugging.
|
|
391
|
-
|
|
392
|
-
```ts
|
|
393
|
-
import { assert, test } from 'vitest'
|
|
394
|
-
|
|
395
|
-
test.only('test', () => {
|
|
396
|
-
// Only this test (and others marked with only) are run
|
|
397
|
-
assert.equal(Math.sqrt(4), 2)
|
|
398
|
-
})
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
Sometimes it is very useful to run `only` tests in a certain file, ignoring all other tests from the whole test suite, which pollute the output.
|
|
402
|
-
|
|
403
|
-
In order to do that, run `vitest` with specific file containing the tests in question:
|
|
404
|
-
|
|
405
|
-
```shell
|
|
406
|
-
vitest interesting.test.ts
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
::: warning
|
|
410
|
-
Vitest detects when tests are running in CI and will throw an error if any test has `only` flag. You can configure this behaviour via [`allowOnly`](/config/allowonly) option.
|
|
411
|
-
:::
|
|
412
|
-
|
|
413
|
-
## test.concurrent
|
|
414
|
-
|
|
415
|
-
- **Alias:** `it.concurrent`
|
|
416
|
-
|
|
417
|
-
`test.concurrent` marks consecutive tests to be run in parallel. It receives the test name, an async function with the tests to collect, and an optional timeout (in milliseconds).
|
|
418
|
-
|
|
419
|
-
```ts
|
|
420
|
-
import { describe, test } from 'vitest'
|
|
421
|
-
|
|
422
|
-
// The two tests marked with concurrent will be run in parallel
|
|
423
|
-
describe('suite', () => {
|
|
424
|
-
test('serial test', async () => { /* ... */ })
|
|
425
|
-
test.concurrent('concurrent test 1', async () => { /* ... */ })
|
|
426
|
-
test.concurrent('concurrent test 2', async () => { /* ... */ })
|
|
427
|
-
})
|
|
428
|
-
```
|
|
429
|
-
|
|
430
|
-
`test.skip`, `test.only`, and `test.todo` works with concurrent tests. All the following combinations are valid:
|
|
431
|
-
|
|
432
|
-
```ts
|
|
433
|
-
test.concurrent(/* ... */)
|
|
434
|
-
test.skip.concurrent(/* ... */) // or test.concurrent.skip(/* ... */)
|
|
435
|
-
test.only.concurrent(/* ... */) // or test.concurrent.only(/* ... */)
|
|
436
|
-
test.todo.concurrent(/* ... */) // or test.concurrent.todo(/* ... */)
|
|
437
|
-
```
|
|
438
|
-
|
|
439
|
-
When running concurrent tests, Snapshots and Assertions must use `expect` from the local [Test Context](/guide/test-context.md) to ensure the right test is detected.
|
|
440
|
-
|
|
441
|
-
```ts
|
|
442
|
-
test.concurrent('test 1', async ({ expect }) => {
|
|
443
|
-
expect(foo).toMatchSnapshot()
|
|
444
|
-
})
|
|
445
|
-
test.concurrent('test 2', async ({ expect }) => {
|
|
446
|
-
expect(foo).toMatchSnapshot()
|
|
447
|
-
})
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
Note that if tests are synchronous, Vitest will still run them sequentially.
|
|
451
|
-
|
|
452
|
-
## test.sequential
|
|
453
|
-
|
|
454
|
-
- **Alias:** `it.sequential`
|
|
455
|
-
|
|
456
|
-
`test.sequential` marks a test as sequential. This is useful if you want to run tests in sequence within `describe.concurrent` or with the `--sequence.concurrent` command option.
|
|
457
|
-
|
|
458
|
-
```ts
|
|
459
|
-
import { describe, test } from 'vitest'
|
|
460
|
-
|
|
461
|
-
// with config option { sequence: { concurrent: true } }
|
|
462
|
-
test('concurrent test 1', async () => { /* ... */ })
|
|
463
|
-
test('concurrent test 2', async () => { /* ... */ })
|
|
464
|
-
|
|
465
|
-
test.sequential('sequential test 1', async () => { /* ... */ })
|
|
466
|
-
test.sequential('sequential test 2', async () => { /* ... */ })
|
|
467
|
-
|
|
468
|
-
// within concurrent suite
|
|
469
|
-
describe.concurrent('suite', () => {
|
|
470
|
-
test('concurrent test 1', async () => { /* ... */ })
|
|
471
|
-
test('concurrent test 2', async () => { /* ... */ })
|
|
472
|
-
|
|
473
|
-
test.sequential('sequential test 1', async () => { /* ... */ })
|
|
474
|
-
test.sequential('sequential test 2', async () => { /* ... */ })
|
|
475
|
-
})
|
|
476
|
-
```
|
|
477
|
-
|
|
478
|
-
## test.todo
|
|
479
|
-
|
|
480
|
-
- **Alias:** `it.todo`
|
|
481
|
-
|
|
482
|
-
Use `test.todo` to stub tests to be implemented later. An entry will be shown in the report for the tests so you know how many tests you still need to implement.
|
|
483
|
-
|
|
484
|
-
```ts
|
|
485
|
-
// An entry will be shown in the report for this test
|
|
486
|
-
test.todo('unimplemented test', () => {
|
|
487
|
-
// failing implementation...
|
|
488
|
-
})
|
|
489
|
-
```
|
|
490
|
-
|
|
491
|
-
::: tip
|
|
492
|
-
Vitest will automatically mark test as `todo` if test has no body.
|
|
493
|
-
:::
|
|
494
|
-
|
|
495
|
-
## test.fails
|
|
496
|
-
|
|
497
|
-
- **Alias:** `it.fails`
|
|
498
|
-
|
|
499
|
-
Use `test.fails` to indicate that an assertion will fail explicitly.
|
|
500
|
-
|
|
501
|
-
```ts
|
|
502
|
-
import { expect, test } from 'vitest'
|
|
503
|
-
|
|
504
|
-
test.fails('repro #1234', () => {
|
|
505
|
-
expect(add(1, 2)).toBe(4)
|
|
506
|
-
})
|
|
507
|
-
```
|
|
508
|
-
|
|
509
|
-
This flag is useful to track difference in behaviour of your library over time. For example, you can define a failing test without fixing the issue yet due to time constraints. Tests marked with `fails` are tracked in the test summary since Vitest 4.1.
|
|
510
|
-
|
|
511
|
-
## test.each
|
|
512
|
-
|
|
513
|
-
- **Alias:** `it.each`
|
|
514
|
-
|
|
515
|
-
::: tip
|
|
516
|
-
While `test.each` is provided for Jest compatibility,
|
|
517
|
-
Vitest also has [`test.for`](#test-for) with an additional feature to integrate [`TestContext`](/guide/test-context).
|
|
518
|
-
:::
|
|
519
|
-
|
|
520
|
-
Use `test.each` when you need to run the same test with different variables.
|
|
521
|
-
You can inject parameters with [printf formatting](https://nodejs.org/api/util.html#util_util_format_format_args) in the test name in the order of the test function parameters.
|
|
522
|
-
|
|
523
|
-
- `%s`: string
|
|
524
|
-
- `%d`: number
|
|
525
|
-
- `%i`: integer
|
|
526
|
-
- `%f`: floating point value
|
|
527
|
-
- `%j`: json
|
|
528
|
-
- `%o`: object
|
|
529
|
-
- `%#`: 0-based index of the test case
|
|
530
|
-
- `%$`: 1-based index of the test case
|
|
531
|
-
- `%%`: single percent sign ('%')
|
|
532
|
-
|
|
533
|
-
```ts
|
|
534
|
-
import { expect, test } from 'vitest'
|
|
535
|
-
|
|
536
|
-
test.each([
|
|
537
|
-
[1, 1, 2],
|
|
538
|
-
[1, 2, 3],
|
|
539
|
-
[2, 1, 3],
|
|
540
|
-
])('add(%i, %i) -> %i', (a, b, expected) => {
|
|
541
|
-
expect(a + b).toBe(expected)
|
|
542
|
-
})
|
|
543
|
-
|
|
544
|
-
// this will return
|
|
545
|
-
// ✓ add(1, 1) -> 2
|
|
546
|
-
// ✓ add(1, 2) -> 3
|
|
547
|
-
// ✓ add(2, 1) -> 3
|
|
548
|
-
```
|
|
549
|
-
|
|
550
|
-
You can also access object properties and array elements with `$` prefix:
|
|
551
|
-
|
|
552
|
-
```ts
|
|
553
|
-
test.each([
|
|
554
|
-
{ a: 1, b: 1, expected: 2 },
|
|
555
|
-
{ a: 1, b: 2, expected: 3 },
|
|
556
|
-
{ a: 2, b: 1, expected: 3 },
|
|
557
|
-
])('add($a, $b) -> $expected', ({ a, b, expected }) => {
|
|
558
|
-
expect(a + b).toBe(expected)
|
|
559
|
-
})
|
|
560
|
-
|
|
561
|
-
// this will return
|
|
562
|
-
// ✓ add(1, 1) -> 2
|
|
563
|
-
// ✓ add(1, 2) -> 3
|
|
564
|
-
// ✓ add(2, 1) -> 3
|
|
565
|
-
|
|
566
|
-
test.each([
|
|
567
|
-
[1, 1, 2],
|
|
568
|
-
[1, 2, 3],
|
|
569
|
-
[2, 1, 3],
|
|
570
|
-
])('add($0, $1) -> $2', (a, b, expected) => {
|
|
571
|
-
expect(a + b).toBe(expected)
|
|
572
|
-
})
|
|
573
|
-
|
|
574
|
-
// this will return
|
|
575
|
-
// ✓ add(1, 1) -> 2
|
|
576
|
-
// ✓ add(1, 2) -> 3
|
|
577
|
-
// ✓ add(2, 1) -> 3
|
|
578
|
-
```
|
|
579
|
-
|
|
580
|
-
You can also access Object attributes with `.`, if you are using objects as arguments:
|
|
581
|
-
|
|
582
|
-
```ts
|
|
583
|
-
test.each`
|
|
584
|
-
a | b | expected
|
|
585
|
-
${{ val: 1 }} | ${'b'} | ${'1b'}
|
|
586
|
-
${{ val: 2 }} | ${'b'} | ${'2b'}
|
|
587
|
-
${{ val: 3 }} | ${'b'} | ${'3b'}
|
|
588
|
-
`('add($a.val, $b) -> $expected', ({ a, b, expected }) => {
|
|
589
|
-
expect(a.val + b).toBe(expected)
|
|
590
|
-
})
|
|
591
|
-
|
|
592
|
-
// this will return
|
|
593
|
-
// ✓ add(1, b) -> 1b
|
|
594
|
-
// ✓ add(2, b) -> 2b
|
|
595
|
-
// ✓ add(3, b) -> 3b
|
|
596
|
-
```
|
|
597
|
-
|
|
598
|
-
* First row should be column names, separated by `|`;
|
|
599
|
-
* One or more subsequent rows of data supplied as template literal expressions using `${value}` syntax.
|
|
600
|
-
|
|
601
|
-
```ts
|
|
602
|
-
import { expect, test } from 'vitest'
|
|
603
|
-
|
|
604
|
-
test.each`
|
|
605
|
-
a | b | expected
|
|
606
|
-
${1} | ${1} | ${2}
|
|
607
|
-
${'a'} | ${'b'} | ${'ab'}
|
|
608
|
-
${[]} | ${'b'} | ${'b'}
|
|
609
|
-
${{}} | ${'b'} | ${'[object Object]b'}
|
|
610
|
-
${{ asd: 1 }} | ${'b'} | ${'[object Object]b'}
|
|
611
|
-
`('returns $expected when $a is added $b', ({ a, b, expected }) => {
|
|
612
|
-
expect(a + b).toBe(expected)
|
|
613
|
-
})
|
|
614
|
-
```
|
|
615
|
-
|
|
616
|
-
::: tip
|
|
617
|
-
Vitest processes `$values` with Chai `format` method. If the value is too truncated, you can increase [chaiConfig.truncateThreshold](/config/chaiconfig#chaiconfig-truncatethreshold) in your config file.
|
|
618
|
-
:::
|
|
619
|
-
|
|
620
|
-
## test.for
|
|
621
|
-
|
|
622
|
-
- **Alias:** `it.for`
|
|
623
|
-
|
|
624
|
-
Alternative to `test.each` to provide [`TestContext`](/guide/test-context).
|
|
625
|
-
|
|
626
|
-
The difference from `test.each` lies in how arrays are provided in the arguments.
|
|
627
|
-
Non-array arguments to `test.for` (including template string usage) work exactly the same as for `test.each`.
|
|
628
|
-
|
|
629
|
-
```ts
|
|
630
|
-
// `each` spreads arrays
|
|
631
|
-
test.each([
|
|
632
|
-
[1, 1, 2],
|
|
633
|
-
[1, 2, 3],
|
|
634
|
-
[2, 1, 3],
|
|
635
|
-
])('add(%i, %i) -> %i', (a, b, expected) => { // [!code --]
|
|
636
|
-
expect(a + b).toBe(expected)
|
|
637
|
-
})
|
|
638
|
-
|
|
639
|
-
// `for` doesn't spread arrays (notice the square brackets around the arguments)
|
|
640
|
-
test.for([
|
|
641
|
-
[1, 1, 2],
|
|
642
|
-
[1, 2, 3],
|
|
643
|
-
[2, 1, 3],
|
|
644
|
-
])('add(%i, %i) -> %i', ([a, b, expected]) => { // [!code ++]
|
|
645
|
-
expect(a + b).toBe(expected)
|
|
646
|
-
})
|
|
647
|
-
```
|
|
648
|
-
|
|
649
|
-
The 2nd argument is [`TestContext`](/guide/test-context) and can be used for concurrent snapshots, for example:
|
|
650
|
-
|
|
651
|
-
```ts
|
|
652
|
-
test.concurrent.for([
|
|
653
|
-
[1, 1],
|
|
654
|
-
[1, 2],
|
|
655
|
-
[2, 1],
|
|
656
|
-
])('add(%i, %i)', ([a, b], { expect }) => {
|
|
657
|
-
expect(a + b).toMatchSnapshot()
|
|
658
|
-
})
|
|
659
|
-
```
|
|
660
|
-
|
|
661
|
-
## test.describe <Version>4.1.0</Version> {#test-describe}
|
|
662
|
-
|
|
663
|
-
Scoped `describe`. See [describe](/api/describe) for more information.
|
|
664
|
-
|
|
665
|
-
## test.suite <Version>4.1.0</Version> {#test-suite}
|
|
666
|
-
|
|
667
|
-
Alias for `suite`. See [describe](/api/describe) for more information.
|
|
668
|
-
|
|
669
|
-
## test.beforeEach
|
|
670
|
-
|
|
671
|
-
Scoped `beforeEach` hook that inherits types from [`test.extend`](#test-extend). See [beforeEach](/api/hooks#beforeeach) for more information.
|
|
672
|
-
|
|
673
|
-
## test.afterEach
|
|
674
|
-
|
|
675
|
-
Scoped `afterEach` hook that inherits types from [`test.extend`](#test-extend). See [afterEach](/api/hooks#aftereach) for more information.
|
|
676
|
-
|
|
677
|
-
## test.beforeAll
|
|
678
|
-
|
|
679
|
-
Scoped `beforeAll` hook that inherits types from [`test.extend`](#test-extend). See [beforeAll](/api/hooks#beforeall) for more information.
|
|
680
|
-
|
|
681
|
-
## test.afterAll
|
|
682
|
-
|
|
683
|
-
Scoped `afterAll` hook that inherits types from [`test.extend`](#test-extend). See [afterAll](/api/hooks#afterall) for more information.
|
|
684
|
-
|
|
685
|
-
## test.aroundEach <Version>4.1.0</Version> {#test-aroundeach}
|
|
686
|
-
|
|
687
|
-
Scoped `aroundEach` hook that inherits types from [`test.extend`](#test-extend). See [aroundEach](/api/hooks#aroundeach) for more information.
|
|
688
|
-
|
|
689
|
-
## test.aroundAll <Version>4.1.0</Version> {#test-aroundall}
|
|
690
|
-
|
|
691
|
-
Scoped `aroundAll` hook that inherits types from [`test.extend`](#test-extend). See [aroundAll](/api/hooks#aroundall) for more information.
|
|
692
|
-
|
|
693
|
-
## bench <Experimental /> {#bench}
|
|
694
|
-
|
|
695
|
-
- **Type:** `(name: string | Function, fn: BenchFunction, options?: BenchOptions) => void`
|
|
696
|
-
|
|
697
|
-
::: danger
|
|
698
|
-
Benchmarking is experimental and does not follow SemVer.
|
|
699
|
-
:::
|
|
700
|
-
|
|
701
|
-
`bench` defines a benchmark. In Vitest terms, benchmark is a function that defines a series of operations. Vitest runs this function multiple times to display different performance results.
|
|
702
|
-
|
|
703
|
-
Vitest uses the [`tinybench`](https://github.com/tinylibs/tinybench) library under the hood, inheriting all its options that can be used as a third argument.
|
|
704
|
-
|
|
705
|
-
```ts
|
|
706
|
-
import { bench } from 'vitest'
|
|
707
|
-
|
|
708
|
-
bench('normal sorting', () => {
|
|
709
|
-
const x = [1, 5, 4, 2, 3]
|
|
710
|
-
x.sort((a, b) => {
|
|
711
|
-
return a - b
|
|
712
|
-
})
|
|
713
|
-
}, { time: 1000 })
|
|
714
|
-
```
|
|
715
|
-
|
|
716
|
-
```ts
|
|
717
|
-
export interface Options {
|
|
718
|
-
/**
|
|
719
|
-
* time needed for running a benchmark task (milliseconds)
|
|
720
|
-
* @default 500
|
|
721
|
-
*/
|
|
722
|
-
time?: number
|
|
723
|
-
|
|
724
|
-
/**
|
|
725
|
-
* number of times that a task should run if even the time option is finished
|
|
726
|
-
* @default 10
|
|
727
|
-
*/
|
|
728
|
-
iterations?: number
|
|
729
|
-
|
|
730
|
-
/**
|
|
731
|
-
* function to get the current timestamp in milliseconds
|
|
732
|
-
*/
|
|
733
|
-
now?: () => number
|
|
734
|
-
|
|
735
|
-
/**
|
|
736
|
-
* An AbortSignal for aborting the benchmark
|
|
737
|
-
*/
|
|
738
|
-
signal?: AbortSignal
|
|
739
|
-
|
|
740
|
-
/**
|
|
741
|
-
* Throw if a task fails (events will not work if true)
|
|
742
|
-
*/
|
|
743
|
-
throws?: boolean
|
|
744
|
-
|
|
745
|
-
/**
|
|
746
|
-
* warmup time (milliseconds)
|
|
747
|
-
* @default 100ms
|
|
748
|
-
*/
|
|
749
|
-
warmupTime?: number
|
|
750
|
-
|
|
751
|
-
/**
|
|
752
|
-
* warmup iterations
|
|
753
|
-
* @default 5
|
|
754
|
-
*/
|
|
755
|
-
warmupIterations?: number
|
|
756
|
-
|
|
757
|
-
/**
|
|
758
|
-
* setup function to run before each benchmark task (cycle)
|
|
759
|
-
*/
|
|
760
|
-
setup?: Hook
|
|
761
|
-
|
|
762
|
-
/**
|
|
763
|
-
* teardown function to run after each benchmark task (cycle)
|
|
764
|
-
*/
|
|
765
|
-
teardown?: Hook
|
|
766
|
-
}
|
|
767
|
-
```
|
|
768
|
-
After the test case is run, the output structure information is as follows:
|
|
769
|
-
|
|
770
|
-
```
|
|
771
|
-
name hz min max mean p75 p99 p995 p999 rme samples
|
|
772
|
-
· normal sorting 6,526,368.12 0.0001 0.3638 0.0002 0.0002 0.0002 0.0002 0.0004 ±1.41% 652638
|
|
773
|
-
```
|
|
774
|
-
```ts
|
|
775
|
-
export interface TaskResult {
|
|
776
|
-
/*
|
|
777
|
-
* the last error that was thrown while running the task
|
|
778
|
-
*/
|
|
779
|
-
error?: unknown
|
|
780
|
-
|
|
781
|
-
/**
|
|
782
|
-
* The amount of time in milliseconds to run the benchmark task (cycle).
|
|
783
|
-
*/
|
|
784
|
-
totalTime: number
|
|
785
|
-
|
|
786
|
-
/**
|
|
787
|
-
* the minimum value in the samples
|
|
788
|
-
*/
|
|
789
|
-
min: number
|
|
790
|
-
/**
|
|
791
|
-
* the maximum value in the samples
|
|
792
|
-
*/
|
|
793
|
-
max: number
|
|
794
|
-
|
|
795
|
-
/**
|
|
796
|
-
* the number of operations per second
|
|
797
|
-
*/
|
|
798
|
-
hz: number
|
|
799
|
-
|
|
800
|
-
/**
|
|
801
|
-
* how long each operation takes (ms)
|
|
802
|
-
*/
|
|
803
|
-
period: number
|
|
804
|
-
|
|
805
|
-
/**
|
|
806
|
-
* task samples of each task iteration time (ms)
|
|
807
|
-
*/
|
|
808
|
-
samples: number[]
|
|
809
|
-
|
|
810
|
-
/**
|
|
811
|
-
* samples mean/average (estimate of the population mean)
|
|
812
|
-
*/
|
|
813
|
-
mean: number
|
|
814
|
-
|
|
815
|
-
/**
|
|
816
|
-
* samples variance (estimate of the population variance)
|
|
817
|
-
*/
|
|
818
|
-
variance: number
|
|
819
|
-
|
|
820
|
-
/**
|
|
821
|
-
* samples standard deviation (estimate of the population standard deviation)
|
|
822
|
-
*/
|
|
823
|
-
sd: number
|
|
824
|
-
|
|
825
|
-
/**
|
|
826
|
-
* standard error of the mean (a.k.a. the standard deviation of the sampling distribution of the sample mean)
|
|
827
|
-
*/
|
|
828
|
-
sem: number
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* degrees of freedom
|
|
832
|
-
*/
|
|
833
|
-
df: number
|
|
834
|
-
|
|
835
|
-
/**
|
|
836
|
-
* critical value of the samples
|
|
837
|
-
*/
|
|
838
|
-
critical: number
|
|
839
|
-
|
|
840
|
-
/**
|
|
841
|
-
* margin of error
|
|
842
|
-
*/
|
|
843
|
-
moe: number
|
|
844
|
-
|
|
845
|
-
/**
|
|
846
|
-
* relative margin of error
|
|
847
|
-
*/
|
|
848
|
-
rme: number
|
|
849
|
-
|
|
850
|
-
/**
|
|
851
|
-
* median absolute deviation
|
|
852
|
-
*/
|
|
853
|
-
mad: number
|
|
854
|
-
|
|
855
|
-
/**
|
|
856
|
-
* p50/median percentile
|
|
857
|
-
*/
|
|
858
|
-
p50: number
|
|
859
|
-
|
|
860
|
-
/**
|
|
861
|
-
* p75 percentile
|
|
862
|
-
*/
|
|
863
|
-
p75: number
|
|
864
|
-
|
|
865
|
-
/**
|
|
866
|
-
* p99 percentile
|
|
867
|
-
*/
|
|
868
|
-
p99: number
|
|
869
|
-
|
|
870
|
-
/**
|
|
871
|
-
* p995 percentile
|
|
872
|
-
*/
|
|
873
|
-
p995: number
|
|
874
|
-
|
|
875
|
-
/**
|
|
876
|
-
* p999 percentile
|
|
877
|
-
*/
|
|
878
|
-
p999: number
|
|
879
|
-
}
|
|
880
|
-
```
|
|
881
|
-
|
|
882
|
-
### bench.skip
|
|
883
|
-
|
|
884
|
-
- **Type:** `(name: string | Function, fn: BenchFunction, options?: BenchOptions) => void`
|
|
885
|
-
|
|
886
|
-
You can use `bench.skip` syntax to skip running certain benchmarks.
|
|
887
|
-
|
|
888
|
-
```ts
|
|
889
|
-
import { bench } from 'vitest'
|
|
890
|
-
|
|
891
|
-
bench.skip('normal sorting', () => {
|
|
892
|
-
const x = [1, 5, 4, 2, 3]
|
|
893
|
-
x.sort((a, b) => {
|
|
894
|
-
return a - b
|
|
895
|
-
})
|
|
896
|
-
})
|
|
897
|
-
```
|
|
898
|
-
|
|
899
|
-
### bench.only
|
|
900
|
-
|
|
901
|
-
- **Type:** `(name: string | Function, fn: BenchFunction, options?: BenchOptions) => void`
|
|
902
|
-
|
|
903
|
-
Use `bench.only` to only run certain benchmarks in a given suite. This is useful when debugging.
|
|
904
|
-
|
|
905
|
-
```ts
|
|
906
|
-
import { bench } from 'vitest'
|
|
907
|
-
|
|
908
|
-
bench.only('normal sorting', () => {
|
|
909
|
-
const x = [1, 5, 4, 2, 3]
|
|
910
|
-
x.sort((a, b) => {
|
|
911
|
-
return a - b
|
|
912
|
-
})
|
|
913
|
-
})
|
|
914
|
-
```
|
|
915
|
-
|
|
916
|
-
### bench.todo
|
|
917
|
-
|
|
918
|
-
- **Type:** `(name: string | Function) => void`
|
|
919
|
-
|
|
920
|
-
Use `bench.todo` to stub benchmarks to be implemented later.
|
|
921
|
-
|
|
922
|
-
```ts
|
|
923
|
-
import { bench } from 'vitest'
|
|
924
|
-
|
|
925
|
-
bench.todo('unimplemented test')
|
|
926
|
-
```
|