@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,128 +0,0 @@
|
|
|
1
|
-
# Getting Started
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
|
|
5
|
-
Vitest (pronounced as _"veetest"_) is a next generation testing framework
|
|
6
|
-
powered by
|
|
7
|
-
Vite.
|
|
8
|
-
|
|
9
|
-
You can learn more about the rationale behind the project in the [Why Vitest](/guide/why) section.
|
|
10
|
-
|
|
11
|
-
## Trying Vitest Online
|
|
12
|
-
|
|
13
|
-
You can try Vitest online on [StackBlitz](https://vitest.new). It runs Vitest directly in the browser, and it is almost identical to the local setup but doesn't require installing anything on your machine.
|
|
14
|
-
|
|
15
|
-
## Adding Vitest to Your Project
|
|
16
|
-
|
|
17
|
-
<CourseLink href="https://vueschool.io/lessons/how-to-install-vitest?friend=vueuse">Learn how to install by Video</CourseLink>
|
|
18
|
-
|
|
19
|
-
::: code-group
|
|
20
|
-
```bash [npm]
|
|
21
|
-
npm install -D vitest
|
|
22
|
-
```
|
|
23
|
-
```bash [yarn]
|
|
24
|
-
yarn add -D vitest
|
|
25
|
-
```
|
|
26
|
-
```bash [pnpm]
|
|
27
|
-
pnpm add -D vitest
|
|
28
|
-
```
|
|
29
|
-
```bash [bun]
|
|
30
|
-
bun add -D vitest
|
|
31
|
-
```
|
|
32
|
-
:::
|
|
33
|
-
|
|
34
|
-
:::tip
|
|
35
|
-
Vitest requires Vite >=v6.0.0 and Node >=v20.0.0
|
|
36
|
-
:::
|
|
37
|
-
|
|
38
|
-
It is recommended that you install a copy of `vitest` in your `package.json`, using one of the methods listed above. However, if you would prefer to run `vitest` directly, you can use `npx vitest` (the `npx` tool comes with npm and Node.js).
|
|
39
|
-
|
|
40
|
-
The `npx` tool will execute the specified command. By default, `npx` will first check if the command exists in the local project's binaries. If it is not found there, `npx` will look in the system's `$PATH` and execute it if found. If the command is not found in either location, `npx` will install it in a temporary location prior to execution.
|
|
41
|
-
|
|
42
|
-
## Writing Tests
|
|
43
|
-
|
|
44
|
-
As an example, we will write a simple test that verifies the output of a function that adds two numbers.
|
|
45
|
-
|
|
46
|
-
``` js [sum.js]
|
|
47
|
-
export function sum(a, b) {
|
|
48
|
-
return a + b
|
|
49
|
-
}
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
``` js [sum.test.js]
|
|
53
|
-
import { expect, test } from 'vitest'
|
|
54
|
-
import { sum } from './sum.js'
|
|
55
|
-
|
|
56
|
-
test('adds 1 + 2 to equal 3', () => {
|
|
57
|
-
expect(sum(1, 2)).toBe(3)
|
|
58
|
-
})
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
::: tip
|
|
62
|
-
By default, tests must contain `.test.` or `.spec.` in their file name.
|
|
63
|
-
:::
|
|
64
|
-
|
|
65
|
-
Next, in order to execute the test, add the following section to your `package.json`:
|
|
66
|
-
|
|
67
|
-
```json [package.json]
|
|
68
|
-
{
|
|
69
|
-
"scripts": {
|
|
70
|
-
"test": "vitest"
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
Finally, run `npm run test`, `yarn test` or `pnpm test`, depending on your package manager, and Vitest will print this message:
|
|
76
|
-
|
|
77
|
-
```txt
|
|
78
|
-
✓ sum.test.js (1)
|
|
79
|
-
✓ adds 1 + 2 to equal 3
|
|
80
|
-
|
|
81
|
-
Test Files 1 passed (1)
|
|
82
|
-
Tests 1 passed (1)
|
|
83
|
-
Start at 02:15:44
|
|
84
|
-
Duration 311ms
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
::: warning
|
|
88
|
-
If you are using Bun as your package manager, make sure to use `bun run test` command instead of `bun test`, otherwise Bun will run its own test runner.
|
|
89
|
-
:::
|
|
90
|
-
|
|
91
|
-
Your first test is passing! Continue to [Writing Tests](/guide/learn/writing-tests) to learn about organizing tests, reading test output, and the core testing patterns you'll use every day.
|
|
92
|
-
|
|
93
|
-
To run tests once without watching for file changes, use `vitest run`. You can also pass additional flags like `--reporter` or `--coverage`. For a full list of CLI options, run `npx vitest --help` or see the [CLI guide](/guide/cli).
|
|
94
|
-
|
|
95
|
-
## Configuring Vitest
|
|
96
|
-
|
|
97
|
-
Vitest reads your `vite.config.*` by default, so your existing Vite plugins and configuration work out-of-the-box. You can also create a dedicated `vitest.config.*` for test-specific settings. See the [Config Reference](/config/) for details.
|
|
98
|
-
|
|
99
|
-
## IDE Integrations
|
|
100
|
-
|
|
101
|
-
We also provided an official extension for Visual Studio Code to enhance your testing experience with Vitest.
|
|
102
|
-
|
|
103
|
-
[Install from VS Code Marketplace](https://marketplace.visualstudio.com/items?itemName=vitest.explorer)
|
|
104
|
-
|
|
105
|
-
Learn more about [IDE Integrations](/guide/ide)
|
|
106
|
-
|
|
107
|
-
## Examples
|
|
108
|
-
|
|
109
|
-
| Example | Source | Playground |
|
|
110
|
-
|---|---|---|
|
|
111
|
-
| `basic` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/basic) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/basic?initialPath=__vitest__/) |
|
|
112
|
-
| `fastify` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/fastify) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/fastify?initialPath=__vitest__/) |
|
|
113
|
-
| `in-source-test` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/in-source-test) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/in-source-test?initialPath=__vitest__/) |
|
|
114
|
-
| `lit` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/lit) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/lit?initialPath=__vitest__/) |
|
|
115
|
-
| `vue` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/vue) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/vue?initialPath=__vitest__/) |
|
|
116
|
-
| `marko` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/marko) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/marko?initialPath=__vitest__/) |
|
|
117
|
-
| `preact` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/preact) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/preact?initialPath=__vitest__/) |
|
|
118
|
-
| `qwik` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/qwik) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/qwik?initialPath=__vitest__/) |
|
|
119
|
-
| `react` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/react) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/react?initialPath=__vitest__/) |
|
|
120
|
-
| `solid` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/solid) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/solid?initialPath=__vitest__/) |
|
|
121
|
-
| `svelte` | [GitHub](https://github.com/vitest-tests/browser-examples/tree/main/examples/svelte) | [Play Online](https://stackblitz.com/fork/github/vitest-tests/browser-examples/tree/main/examples/svelte?initialPath=__vitest__/) |
|
|
122
|
-
| `profiling` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/profiling) | Not Available |
|
|
123
|
-
| `typecheck` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/typecheck) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/typecheck?initialPath=__vitest__/) |
|
|
124
|
-
| `projects` | [GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/projects) | [Play Online](https://stackblitz.com/fork/github/vitest-dev/vitest/tree/main/examples/projects?initialPath=__vitest__/) |
|
|
125
|
-
|
|
126
|
-
## Community
|
|
127
|
-
|
|
128
|
-
If you have questions or need help, reach out to the community at [Discord](https://chat.vitest.dev) and [GitHub Discussions](https://github.com/vitest-dev/vitest/discussions).
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
# Testing Asynchronous Code
|
|
2
|
-
|
|
3
|
-
JavaScript code frequently runs asynchronously. Whether you're fetching data, reading files, or waiting on timers, Vitest needs to know when the code it is testing has completed before moving on to the next test. Here are the patterns you'll use most often.
|
|
4
|
-
|
|
5
|
-
## Async/Await
|
|
6
|
-
|
|
7
|
-
The most straightforward approach is to make your test function `async`. Vitest will automatically wait for the returned promise to resolve before considering the test complete. If the promise rejects, the test fails with the rejection reason.
|
|
8
|
-
|
|
9
|
-
```js
|
|
10
|
-
import { expect, test } from 'vitest'
|
|
11
|
-
|
|
12
|
-
function fetchUser(id) {
|
|
13
|
-
return Promise.resolve({ id, name: 'Alice' })
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
test('fetches user by id', async () => {
|
|
17
|
-
const user = await fetchUser(1)
|
|
18
|
-
expect(user.name).toBe('Alice')
|
|
19
|
-
})
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
This is the pattern you'll use the vast majority of the time. It reads just like synchronous code, and errors propagate naturally through `await`.
|
|
23
|
-
|
|
24
|
-
## Resolves and Rejects
|
|
25
|
-
|
|
26
|
-
Sometimes you'd rather assert on a promise directly instead of `await`-ing it into a variable first. The [`.resolves`](/api/expect#resolves) and [`.rejects`](/api/expect#rejects) helpers let you do this. They unwrap the promise and then apply the matcher to the resolved or rejected value:
|
|
27
|
-
|
|
28
|
-
```js
|
|
29
|
-
test('resolves to Alice', async () => {
|
|
30
|
-
await expect(fetchUser(1)).resolves.toMatchObject({ name: 'Alice' })
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
test('rejects with an error', async () => {
|
|
34
|
-
await expect(fetchInvalidUser()).rejects.toThrow('User not found')
|
|
35
|
-
})
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
::: warning
|
|
39
|
-
Don't forget the `await` before `expect`. Vitest will detect unawaited assertions and print a warning at the end of the test, but it's best to always include `await` explicitly. Vitest will also wait for all pending promises in `Promise.all` before starting the next test, but relying on this behavior makes tests harder to understand.
|
|
40
|
-
:::
|
|
41
|
-
|
|
42
|
-
## Assertion Counting
|
|
43
|
-
|
|
44
|
-
With async code, there's a subtle risk: an assertion inside a callback or `.then()` chain might never execute, and the test would still pass because no assertion failed. [`expect.hasAssertions()`](/api/expect#hasassertions) guards against this by verifying that at least one assertion ran during the test:
|
|
45
|
-
|
|
46
|
-
```js
|
|
47
|
-
test('callback is invoked', async () => {
|
|
48
|
-
expect.hasAssertions()
|
|
49
|
-
|
|
50
|
-
const data = await fetchData()
|
|
51
|
-
data.items.forEach((item) => {
|
|
52
|
-
expect(item.id).toBeDefined()
|
|
53
|
-
})
|
|
54
|
-
// if data.items is empty, the test fails instead of silently passing
|
|
55
|
-
})
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
When you know exactly how many assertions should run, [`expect.assertions(n)`](/api/expect#assertions) is more precise:
|
|
59
|
-
|
|
60
|
-
```js
|
|
61
|
-
test('both callbacks are called', async () => {
|
|
62
|
-
expect.assertions(2)
|
|
63
|
-
|
|
64
|
-
await Promise.all([
|
|
65
|
-
fetchUser(1).then(user => expect(user.name).toBe('Alice')),
|
|
66
|
-
fetchUser(2).then(user => expect(user.name).toBe('Bob')),
|
|
67
|
-
])
|
|
68
|
-
})
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
In most cases, `async`/`await` with direct assertions is clear enough and you don't need assertion counting. It's most useful when assertions are inside callbacks, loops, or conditional branches where you want to guarantee they actually executed.
|
|
72
|
-
|
|
73
|
-
::: tip
|
|
74
|
-
If you want every test in your project to require at least one assertion, enable [`expect.requireAssertions`](/config/expect#expect-requireassertions) in your config instead of adding `expect.hasAssertions()` to each test manually.
|
|
75
|
-
:::
|
|
76
|
-
|
|
77
|
-
## Callbacks
|
|
78
|
-
|
|
79
|
-
Some older APIs use callbacks instead of promises. Since Vitest works with promises, the simplest approach is to wrap the callback in a `Promise`:
|
|
80
|
-
|
|
81
|
-
```js
|
|
82
|
-
function fetchData(callback) {
|
|
83
|
-
setTimeout(() => callback('peanut butter'), 100)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
test('the data is peanut butter', async () => {
|
|
87
|
-
const data = await new Promise((resolve) => {
|
|
88
|
-
fetchData(resolve)
|
|
89
|
-
})
|
|
90
|
-
expect(data).toBe('peanut butter')
|
|
91
|
-
})
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
This pattern works for any callback-based API. Pass `resolve` as the success callback, and the test will wait until the callback is invoked.
|
|
95
|
-
|
|
96
|
-
::: tip
|
|
97
|
-
Most modern Node.js APIs (such as `fs/promises` and `fetch`) support promises natively, so you can use `async`/`await` directly. The callback wrapping pattern above is mainly useful for older libraries that haven't adopted promises yet.
|
|
98
|
-
:::
|
|
99
|
-
|
|
100
|
-
## Timeouts
|
|
101
|
-
|
|
102
|
-
By default, each test has a 5-second timeout. If a test takes longer than that (perhaps because a promise never resolves, or a network request hangs), it will fail with a timeout error. This prevents your test suite from getting stuck indefinitely.
|
|
103
|
-
|
|
104
|
-
You can set a [custom timeout](/api/test#timeout) as the third argument to `test`, which is useful for tests that legitimately need more time:
|
|
105
|
-
|
|
106
|
-
```js
|
|
107
|
-
test('long-running operation', async () => {
|
|
108
|
-
await someSlowOperation()
|
|
109
|
-
}, 10_000) // 10 seconds
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
If you find yourself needing longer timeouts across many tests, you can change the default for all tests with the [`testTimeout`](/config/testtimeout) config option:
|
|
113
|
-
|
|
114
|
-
```js [vitest.config.js]
|
|
115
|
-
import { defineConfig } from 'vitest/config'
|
|
116
|
-
|
|
117
|
-
export default defineConfig({
|
|
118
|
-
test: {
|
|
119
|
-
testTimeout: 10_000,
|
|
120
|
-
},
|
|
121
|
-
})
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
## Unhandled Rejections
|
|
125
|
-
|
|
126
|
-
By default, Vitest reports unhandled promise rejections as errors in the test run. If a promise rejects somewhere in your code and nothing catches it, the test run will fail, even if all your assertions passed. This is intentional: unhandled rejections usually indicate real bugs, like a forgotten `await` or a fire-and-forget promise that silently fails.
|
|
127
|
-
|
|
128
|
-
```js
|
|
129
|
-
test('this causes an unhandled rejection error', () => {
|
|
130
|
-
// This promise rejects but is never awaited or caught
|
|
131
|
-
Promise.reject(new Error('oops'))
|
|
132
|
-
})
|
|
133
|
-
```
|
|
134
|
-
|
|
135
|
-
To fix this, make sure you `await` all promises or catch expected rejections:
|
|
136
|
-
|
|
137
|
-
```js
|
|
138
|
-
test('handle the rejection', async () => {
|
|
139
|
-
// Either await the promise
|
|
140
|
-
await expect(Promise.reject(new Error('oops'))).rejects.toThrow('oops')
|
|
141
|
-
|
|
142
|
-
// Or catch it explicitly if you don't need to assert on it
|
|
143
|
-
Promise.reject(new Error('expected')).catch(() => {})
|
|
144
|
-
})
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
If your code intentionally produces unhandled rejections, you can filter specific errors with [`onUnhandledError`](/config/onunhandlederror) or disable the check entirely with [`dangerouslyIgnoreUnhandledErrors`](/config/dangerouslyignoreunhandlederrors).
|
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
# Debugging Failing Tests
|
|
2
|
-
|
|
3
|
-
This page covers how to investigate test failures in Vitest: reading error output, isolating problems, identifying common causes, and using the available debugging tools.
|
|
4
|
-
|
|
5
|
-
## Reading the Error
|
|
6
|
-
|
|
7
|
-
When a test fails, Vitest gives you several pieces of information. Let's look at a real failure and break it down:
|
|
8
|
-
|
|
9
|
-
<<< ./snippets/debug-output-fail.ansi
|
|
10
|
-
|
|
11
|
-
There's a lot here, but each part tells you something:
|
|
12
|
-
|
|
13
|
-
**The header** (`FAIL src/user.test.js > createUser > sets the default role`) tells you which file, describe block, and test failed. This is the full path in the test tree.
|
|
14
|
-
|
|
15
|
-
**The assertion message** (`expected { ... } to deeply equal { ... }`) tells you what kind of check failed and shows the two values being compared.
|
|
16
|
-
|
|
17
|
-
**The diff** shows exactly what's different. Lines starting with <code class="diff-add">+</code> are what you actually got, and lines starting with <code class="diff-remove">-</code> are what you expected. In this case, the role was <code class="diff-add">"viewer"</code> but the test expected <code class="diff-remove">"member"</code>.
|
|
18
|
-
|
|
19
|
-
**The code snippet** shows the exact line and a few surrounding lines, with a caret (`^`) pointing to the failing assertion. You can click the file path in most terminals and IDEs to jump directly there.
|
|
20
|
-
|
|
21
|
-
At this point, the question is: did the code change (maybe the default role was intentionally updated to `"viewer"`), or is the test wrong? Check the source code for `createUser` to find out. If the default was intentionally changed, update the test. If not, you've found a bug.
|
|
22
|
-
|
|
23
|
-
## Isolating the Problem
|
|
24
|
-
|
|
25
|
-
When a test fails and the cause isn't immediately clear, the first step is to isolate it. Run just that one test, without the rest of your suite:
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
# Run only the failing test file
|
|
29
|
-
vitest src/user.test.js
|
|
30
|
-
|
|
31
|
-
# Run only tests matching a name pattern
|
|
32
|
-
vitest -t "sets the default role"
|
|
33
|
-
|
|
34
|
-
# Combine both for maximum precision
|
|
35
|
-
vitest src/user.test.js -t "sets the default role"
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
You can also add [`.only`](/api/test#only) to the test itself:
|
|
39
|
-
|
|
40
|
-
```js
|
|
41
|
-
test.only('sets the default role', () => {
|
|
42
|
-
// only this test runs in the file
|
|
43
|
-
})
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
If you have many failures and want to focus on the first one, use [`--bail`](/config/bail) to stop after a set number of failures:
|
|
47
|
-
|
|
48
|
-
```bash
|
|
49
|
-
vitest --bail 1
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
If the test passes when run alone but fails when run with others, you have a test isolation problem (more on that below). If it fails even when run alone, the issue is in the test itself or the code it's testing.
|
|
53
|
-
|
|
54
|
-
## Common Causes of Failures
|
|
55
|
-
|
|
56
|
-
### Shared State Between Tests
|
|
57
|
-
|
|
58
|
-
This is one of the most common and frustrating issues. A test passes when you run it alone, but fails when the full suite runs. The usual cause is that some other test modifies shared state (a global variable, a module-level cache, a database) and doesn't clean up after itself.
|
|
59
|
-
|
|
60
|
-
```js
|
|
61
|
-
// This is a problem: `users` is shared between tests
|
|
62
|
-
const users = []
|
|
63
|
-
|
|
64
|
-
test('adds a user', () => {
|
|
65
|
-
users.push('Alice')
|
|
66
|
-
expect(users).toEqual(['Alice'])
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('starts empty', () => {
|
|
70
|
-
// This fails because 'Alice' is still in the array!
|
|
71
|
-
expect(users).toEqual([])
|
|
72
|
-
})
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
The fix is to reset the state before each test with [`beforeEach`](/api/hooks#beforeeach), or better yet, use [`test.extend`](/guide/test-context#extend-test-context) to create fresh state for each test automatically:
|
|
76
|
-
|
|
77
|
-
```js
|
|
78
|
-
const test = baseTest.extend('users', () => [])
|
|
79
|
-
|
|
80
|
-
test('adds a user', ({ users }) => {
|
|
81
|
-
users.push('Alice')
|
|
82
|
-
expect(users).toEqual(['Alice'])
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
test('starts empty', ({ users }) => {
|
|
86
|
-
// Passes: each test gets its own array
|
|
87
|
-
expect(users).toEqual([])
|
|
88
|
-
})
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
### Async Issues
|
|
92
|
-
|
|
93
|
-
Tests that involve promises can fail intermittently or in confusing ways if the async flow isn't handled correctly. The most common mistake is forgetting an `await`:
|
|
94
|
-
|
|
95
|
-
```js
|
|
96
|
-
// This test always passes, even if fetchUser rejects!
|
|
97
|
-
test('fetches user', () => {
|
|
98
|
-
// Missing await: the test finishes before the promise settles
|
|
99
|
-
expect(fetchUser(1)).resolves.toMatchObject({ name: 'Alice' })
|
|
100
|
-
})
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
Vitest will usually warn you about unawaited assertions at the end of the test. If you see that warning, add the missing `await`:
|
|
104
|
-
|
|
105
|
-
```js
|
|
106
|
-
test('fetches user', async () => {
|
|
107
|
-
await expect(fetchUser(1)).resolves.toMatchObject({ name: 'Alice' })
|
|
108
|
-
})
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
If a test hangs and eventually times out, it usually means a promise never resolves. Check for missing callbacks, unresolved conditions, or deadlocks in the code you're testing.
|
|
112
|
-
|
|
113
|
-
### Stale Snapshots
|
|
114
|
-
|
|
115
|
-
If you're using [snapshot tests](/guide/learn/snapshots) and you intentionally changed the output of your code, the existing snapshots will be outdated. The test fails and shows a diff between the old snapshot and the new output.
|
|
116
|
-
|
|
117
|
-
This is expected. Review the diff to confirm the changes are correct, then update the snapshots by pressing `u` in watch mode or running `vitest -u`.
|
|
118
|
-
|
|
119
|
-
### Wrong Test Environment
|
|
120
|
-
|
|
121
|
-
If your code accesses browser APIs like `document` or `window` and you see errors like "document is not defined", your test is running in the Node environment (the default). You can switch to a browser-like environment with the [`environment`](/config/environment) config option, or better yet, use [Browser Mode](/guide/browser/) which runs tests in a real browser.
|
|
122
|
-
|
|
123
|
-
### Mocks Not Cleaned Up
|
|
124
|
-
|
|
125
|
-
If a mock from one test leaks into another, you'll get unexpected behavior. For example, a `vi.spyOn` that overrides a method's return value will persist into the next test unless it's restored.
|
|
126
|
-
|
|
127
|
-
The easiest fix is to enable automatic mock restoration in your config:
|
|
128
|
-
|
|
129
|
-
```js [vitest.config.js]
|
|
130
|
-
import { defineConfig } from 'vitest/config'
|
|
131
|
-
|
|
132
|
-
export default defineConfig({
|
|
133
|
-
test: {
|
|
134
|
-
restoreMocks: true,
|
|
135
|
-
},
|
|
136
|
-
})
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
This calls [`mockRestore()`](/api/mock#mockrestore) on every mock after each test. See the [Mock Functions](/guide/learn/mock-functions#resetting-mocks) tutorial for more details.
|
|
140
|
-
|
|
141
|
-
## Debugging Tools
|
|
142
|
-
|
|
143
|
-
### Console Logging
|
|
144
|
-
|
|
145
|
-
There's nothing wrong with adding `console.log` to your tests. It's the fastest way to inspect values and understand what's happening:
|
|
146
|
-
|
|
147
|
-
```js
|
|
148
|
-
test('transforms data correctly', () => {
|
|
149
|
-
const input = getData()
|
|
150
|
-
console.log('input:', input)
|
|
151
|
-
|
|
152
|
-
const result = transform(input)
|
|
153
|
-
console.log('result:', result)
|
|
154
|
-
|
|
155
|
-
expect(result).toMatchObject({ status: 'ok' })
|
|
156
|
-
})
|
|
157
|
-
```
|
|
158
|
-
|
|
159
|
-
Vitest displays console output inline with the test results, so you can see which test produced which log.
|
|
160
|
-
|
|
161
|
-
### Vitest UI
|
|
162
|
-
|
|
163
|
-
For a visual overview of your test suite, run Vitest with the `--ui` flag:
|
|
164
|
-
|
|
165
|
-
```bash
|
|
166
|
-
vitest --ui
|
|
167
|
-
```
|
|
168
|
-
|
|
169
|
-
This opens a browser-based dashboard where you can see all your tests, their status, and their output. It also includes a module graph that shows how your files are connected, which can help you understand why a change in one file causes failures in another. See the [Vitest UI](/guide/ui) guide for more details.
|
|
170
|
-
|
|
171
|
-
### VS Code Extension
|
|
172
|
-
|
|
173
|
-
The [Vitest VS Code extension](https://vitest.dev/vscode) lets you run and debug individual tests directly from your editor. You can click a "play" button next to any test, set breakpoints, and step through code in the VS Code debugger. This is often faster than switching between the terminal and your editor.
|
|
174
|
-
|
|
175
|
-
### Verbose Output
|
|
176
|
-
|
|
177
|
-
If the default output isn't showing enough detail, use the verbose reporter:
|
|
178
|
-
|
|
179
|
-
```bash
|
|
180
|
-
vitest --reporter=verbose
|
|
181
|
-
```
|
|
182
|
-
|
|
183
|
-
This shows every test individually (not just the files), which can help spot patterns in which tests pass and which fail.
|
|
184
|
-
|
|
185
|
-
### Attaching a Debugger
|
|
186
|
-
|
|
187
|
-
For more complex issues where you need to step through code line by line, you can run Vitest with the `--inspect-brk` flag and attach a debugger. The `--no-file-parallelism` flag ensures tests run in the main thread so breakpoints work reliably:
|
|
188
|
-
|
|
189
|
-
```bash
|
|
190
|
-
vitest --inspect-brk --no-file-parallelism
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
Then attach from VS Code, IntelliJ, or Chrome DevTools (`chrome://inspect`). See the [Debugging](/guide/debugging) guide for detailed setup instructions for each editor.
|
|
194
|
-
|
|
195
|
-
## Getting Help
|
|
196
|
-
|
|
197
|
-
If you're stuck, these resources can help:
|
|
198
|
-
|
|
199
|
-
- The [Common Errors](/guide/common-errors) page covers specific error messages and their solutions
|
|
200
|
-
- [GitHub Issues](https://github.com/vitest-dev/vitest/issues) for searching known bugs and workarounds
|
|
201
|
-
- The [Discord community](https://chat.vitest.dev) for real-time help from other Vitest users and maintainers
|
|
202
|
-
|
|
203
|
-
<style>
|
|
204
|
-
.vp-doc code.diff-add {
|
|
205
|
-
color: var(--vp-c-green-2) !important;
|
|
206
|
-
}
|
|
207
|
-
.vp-doc code.diff-remove {
|
|
208
|
-
color: var(--vp-c-red-2) !important;
|
|
209
|
-
}
|
|
210
|
-
</style>
|