@vitest-agent/mcp 1.1.0 → 1.3.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 +3 -2
- package/index.js +1 -1
- package/package.json +1 -1
- package/server.js +2 -4
- package/tools/run-tests.js +17 -3
- 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,101 +0,0 @@
|
|
|
1
|
-
# Test Environment
|
|
2
|
-
|
|
3
|
-
Vitest provides [`environment`](/config/environment) option to run code inside a specific environment. You can modify how environment behaves with [`environmentOptions`](/config/environmentoptions) option.
|
|
4
|
-
|
|
5
|
-
By default, you can use these environments:
|
|
6
|
-
|
|
7
|
-
- `node` is default environment
|
|
8
|
-
- `jsdom` emulates browser environment by providing Browser API, uses [`jsdom`](https://github.com/jsdom/jsdom) package
|
|
9
|
-
- `happy-dom` emulates browser environment by providing Browser API, and considered to be faster than jsdom, but lacks some API, uses [`happy-dom`](https://github.com/capricorn86/happy-dom) package
|
|
10
|
-
- `edge-runtime` emulates Vercel's [edge-runtime](https://edge-runtime.vercel.app/), uses [`@edge-runtime/vm`](https://npmx.dev/package/@edge-runtime/vm) package
|
|
11
|
-
|
|
12
|
-
::: info
|
|
13
|
-
When using `jsdom` or `happy-dom` environments, Vitest follows the same rules that Vite does when importing [CSS](https://vitejs.dev/guide/features.html#css) and [assets](https://vitejs.dev/guide/features.html#static-assets). If importing external dependency fails with `unknown extension .css` error, you need to inline the whole import chain manually by adding all packages to [`server.deps.inline`](/config/server#inline). For example, if the error happens in `package-3` in this import chain: `source code -> package-1 -> package-2 -> package-3`, you need to add all three packages to `server.deps.inline`.
|
|
14
|
-
|
|
15
|
-
The `require` of CSS and assets inside the external dependencies are resolved automatically.
|
|
16
|
-
:::
|
|
17
|
-
|
|
18
|
-
::: warning
|
|
19
|
-
"Environments" exist only when running tests in Node.js.
|
|
20
|
-
|
|
21
|
-
`browser` is not considered an environment in Vitest. If you wish to run part of your tests using [Browser Mode](/guide/browser/), you can create a [test project](/guide/browser/#projects-config).
|
|
22
|
-
:::
|
|
23
|
-
|
|
24
|
-
## Environments for Specific Files
|
|
25
|
-
|
|
26
|
-
When setting `environment` option in your config, it will apply to all the test files in your project. To have more fine-grained control, you can use control comments to specify environment for specific files. Control comments are comments that start with `@vitest-environment` and are followed by the environment name:
|
|
27
|
-
|
|
28
|
-
```ts
|
|
29
|
-
// @vitest-environment jsdom
|
|
30
|
-
|
|
31
|
-
import { expect, test } from 'vitest'
|
|
32
|
-
|
|
33
|
-
test('test', () => {
|
|
34
|
-
expect(typeof window).not.toBe('undefined')
|
|
35
|
-
})
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## Custom Environment
|
|
39
|
-
|
|
40
|
-
You can create your own package to extend Vitest environment. To do so, create package with the name `vitest-environment-${name}` or specify a path to a valid JS/TS file. That package should export an object with the shape of `Environment`:
|
|
41
|
-
|
|
42
|
-
```ts
|
|
43
|
-
import type { Environment } from 'vitest/runtime'
|
|
44
|
-
|
|
45
|
-
export default <Environment>{
|
|
46
|
-
name: 'custom',
|
|
47
|
-
viteEnvironment: 'ssr',
|
|
48
|
-
// optional - only if you support "vmForks" or "vmThreads" pools
|
|
49
|
-
async setupVM() {
|
|
50
|
-
const vm = await import('node:vm')
|
|
51
|
-
const context = vm.createContext()
|
|
52
|
-
return {
|
|
53
|
-
getVmContext() {
|
|
54
|
-
return context
|
|
55
|
-
},
|
|
56
|
-
teardown() {
|
|
57
|
-
// called after all tests with this env have been run
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
setup() {
|
|
62
|
-
// custom setup
|
|
63
|
-
return {
|
|
64
|
-
teardown() {
|
|
65
|
-
// called after all tests with this env have been run
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
::: warning
|
|
73
|
-
Vitest requires `viteEnvironment` option on environment object (fallbacks to the Vitest environment name by default). It should be equal to `ssr`, `client` or any custom [Vite environment](https://vite.dev/guide/api-environment) name. This value determines which environment is used to process file.
|
|
74
|
-
:::
|
|
75
|
-
|
|
76
|
-
You also have access to default Vitest environments through `vitest/runtime` entry:
|
|
77
|
-
|
|
78
|
-
```ts
|
|
79
|
-
import { builtinEnvironments, populateGlobal } from 'vitest/runtime'
|
|
80
|
-
|
|
81
|
-
console.log(builtinEnvironments) // { jsdom, happy-dom, node, edge-runtime }
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
Vitest also provides `populateGlobal` utility function, which can be used to move properties from object into the global namespace:
|
|
85
|
-
|
|
86
|
-
```ts
|
|
87
|
-
interface PopulateOptions {
|
|
88
|
-
// should non-class functions be bind to the global namespace
|
|
89
|
-
bindFunctions?: boolean
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
interface PopulateResult {
|
|
93
|
-
// a list of all keys that were copied, even if value doesn't exist on original object
|
|
94
|
-
keys: Set<string>
|
|
95
|
-
// a map of original object that might have been overridden with keys
|
|
96
|
-
// you can return these values inside `teardown` function
|
|
97
|
-
originals: Map<string | symbol, any>
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function populateGlobal(global: any, original: any, options: PopulateOptions): PopulateResult
|
|
101
|
-
```
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
# Extending Matchers
|
|
2
|
-
|
|
3
|
-
Since Vitest is compatible with both Chai and Jest, you can use either the [`chai.use`](https://www.chaijs.com/guide/plugins/) API or `expect.extend`, whichever you prefer.
|
|
4
|
-
|
|
5
|
-
This guide will explore extending matchers with `expect.extend`. If you are interested in Chai's API, check [their guide](https://www.chaijs.com/guide/plugins/).
|
|
6
|
-
|
|
7
|
-
To extend default matchers, call `expect.extend` with an object containing your matchers.
|
|
8
|
-
|
|
9
|
-
```ts
|
|
10
|
-
expect.extend({
|
|
11
|
-
toBeFoo(received, expected) {
|
|
12
|
-
const { isNot } = this
|
|
13
|
-
return {
|
|
14
|
-
// do not alter your "pass" based on isNot. Vitest does it for you
|
|
15
|
-
pass: received === 'foo',
|
|
16
|
-
message: () => `${received} is${isNot ? ' not' : ''} foo`
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
})
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
If you are using TypeScript, you can extend default `Matchers` interface in an ambient declaration file (e.g: `vitest.d.ts`) with the code below:
|
|
23
|
-
|
|
24
|
-
```ts
|
|
25
|
-
import 'vitest'
|
|
26
|
-
|
|
27
|
-
declare module 'vitest' {
|
|
28
|
-
interface Matchers<T = any> {
|
|
29
|
-
toBeFoo: () => R
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
::: tip
|
|
35
|
-
Importing `vitest` makes TypeScript think this is an ES module file, type declaration won't work without it.
|
|
36
|
-
:::
|
|
37
|
-
|
|
38
|
-
Extending the `Matchers` interface will add a type to `expect.extend`, `expect().*`, and `expect.*` methods at the same time.
|
|
39
|
-
|
|
40
|
-
::: warning
|
|
41
|
-
Don't forget to include the ambient declaration file in your `tsconfig.json`.
|
|
42
|
-
:::
|
|
43
|
-
|
|
44
|
-
The return value of a matcher should be compatible with the following interface:
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
interface MatcherResult {
|
|
48
|
-
pass: boolean
|
|
49
|
-
message: () => string
|
|
50
|
-
// If you pass these, they will automatically appear inside a diff when
|
|
51
|
-
// the matcher does not pass, so you don't need to print the diff yourself
|
|
52
|
-
actual?: unknown
|
|
53
|
-
expected?: unknown
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
::: warning
|
|
58
|
-
If you create an asynchronous matcher, don't forget to `await` the result (`await expect('foo').toBeFoo()`) in the test itself:
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
expect.extend({
|
|
62
|
-
async toBeAsyncAssertion() {
|
|
63
|
-
// ...
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
await expect().toBeAsyncAssertion()
|
|
68
|
-
```
|
|
69
|
-
:::
|
|
70
|
-
|
|
71
|
-
The first argument inside a matcher's function is the received value (the one inside `expect(received)`). The rest are arguments passed directly to the matcher. Since version 4.1, Vitest exposes several types that can be used by your custom matcher:
|
|
72
|
-
|
|
73
|
-
```ts
|
|
74
|
-
import type {
|
|
75
|
-
// the function type
|
|
76
|
-
Matcher,
|
|
77
|
-
// the return value
|
|
78
|
-
MatcherResult,
|
|
79
|
-
// state available as `this`
|
|
80
|
-
MatcherState,
|
|
81
|
-
} from 'vitest'
|
|
82
|
-
import { expect } from 'vitest'
|
|
83
|
-
|
|
84
|
-
// a simple matcher, using "function" to have access to "this"
|
|
85
|
-
const customMatcher: Matcher = function (received) {
|
|
86
|
-
// ...
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// a matcher with arguments
|
|
90
|
-
const customMatcher: Matcher<MatcherState, [arg1: unknown, arg2: unknown]> = function (received, arg1, arg2) {
|
|
91
|
-
// ...
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// a matcher with custom annotations
|
|
95
|
-
function customMatcher(this: MatcherState, received: unknown, arg1: unknown, arg2: unknown): MatcherResult {
|
|
96
|
-
// ...
|
|
97
|
-
return {
|
|
98
|
-
pass: false,
|
|
99
|
-
message: () => 'something went wrong!',
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
expect.extend({ customMatcher })
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
::: tip
|
|
107
|
-
To build custom **snapshot matchers** (wrappers around `toMatchSnapshot()` / `toMatchInlineSnapshot()` / `toMatchFileSnapshot()`), use `Snapshots` exported from `vitest`. See [Custom Snapshot Matchers](/guide/snapshot#custom-snapshot-matchers).
|
|
108
|
-
:::
|
|
109
|
-
|
|
110
|
-
Matcher function has access to `this` context with the following properties:
|
|
111
|
-
|
|
112
|
-
## `isNot`
|
|
113
|
-
|
|
114
|
-
Returns true, if matcher was called on `not` (`expect(received).not.toBeFoo()`). You do not need to respect it, Vitest will reverse the value of `pass` automatically.
|
|
115
|
-
|
|
116
|
-
## `promise`
|
|
117
|
-
|
|
118
|
-
If matcher was called on `resolved/rejected`, this value will contain the name of modifier. Otherwise, it will be an empty string.
|
|
119
|
-
|
|
120
|
-
## `equals`
|
|
121
|
-
|
|
122
|
-
This is a utility function that allows you to compare two values. It will return `true` if values are equal, `false` otherwise. This function is used internally for almost every matcher. It supports objects with asymmetric matchers by default.
|
|
123
|
-
|
|
124
|
-
## `utils`
|
|
125
|
-
|
|
126
|
-
This contains a set of utility functions that you can use to display messages.
|
|
127
|
-
|
|
128
|
-
`this` context also contains information about the current test. You can also get it by calling `expect.getState()`. The most useful properties are:
|
|
129
|
-
|
|
130
|
-
## `currentTestName`
|
|
131
|
-
|
|
132
|
-
Full name of the current test (including describe block).
|
|
133
|
-
|
|
134
|
-
## `task` <Advanced /> <Version>4.1.0</Version> {#task}
|
|
135
|
-
|
|
136
|
-
Contains a reference to [the `Test` runner task](/api/advanced/runner#tasks) when available.
|
|
137
|
-
|
|
138
|
-
::: warning
|
|
139
|
-
When using the global `expect` with concurrent tests, `this.task` is `undefined`. Use `context.expect` instead to ensure `task` is available in custom matchers.
|
|
140
|
-
:::
|
|
141
|
-
|
|
142
|
-
## `testPath`
|
|
143
|
-
|
|
144
|
-
File path to the current test.
|
|
145
|
-
|
|
146
|
-
## `environment`
|
|
147
|
-
|
|
148
|
-
The name of the current [`environment`](/config/environment) (for example, `jsdom`).
|
|
149
|
-
|
|
150
|
-
## `soft`
|
|
151
|
-
|
|
152
|
-
Was assertion called as a [`soft`](/api/expect#soft) one. You don't need to respect it, Vitest will always catch the error.
|
|
153
|
-
|
|
154
|
-
## `assertion` <Advanced /> <Version type="experimental">4.1.4</Version> {#assertion}
|
|
155
|
-
|
|
156
|
-
The underlying [Chai assertion](https://www.chaijs.com/guide/plugins/) object. This is the same instance that Chai plugins receive, giving you access to Chai's flag system and chainable methods. This can be useful for building custom matchers that need to interact with Chai's internals.
|
|
157
|
-
|
|
158
|
-
::: tip
|
|
159
|
-
These are not all of the available properties, only the most useful ones. The other state values are used by Vitest internally.
|
|
160
|
-
:::
|
|
@@ -1,310 +0,0 @@
|
|
|
1
|
-
# Features
|
|
2
|
-
|
|
3
|
-
<script setup>
|
|
4
|
-
import FeaturesList from '../.vitepress/components/FeaturesList.vue'
|
|
5
|
-
</script>
|
|
6
|
-
|
|
7
|
-
<FeaturesList class="!gap-1 text-lg" />
|
|
8
|
-
|
|
9
|
-
<div h-2 />
|
|
10
|
-
<CourseLink href="https://vueschool.io/lessons/your-first-test?friend=vueuse">Learn how to write your first test by Video</CourseLink>
|
|
11
|
-
|
|
12
|
-
::: tip
|
|
13
|
-
This page is a high-level overview of Vitest's capabilities. If you're new to Vitest, we recommend reading the [Learn](/guide/learn/writing-tests) tutorial first for a hands-on introduction.
|
|
14
|
-
:::
|
|
15
|
-
|
|
16
|
-
## Shared Config between Test, Dev and Build
|
|
17
|
-
|
|
18
|
-
Vite's config, transformers, resolvers, and plugins. Use the same setup from your app to run the tests.
|
|
19
|
-
|
|
20
|
-
Learn more at [Configuring Vitest](/config/).
|
|
21
|
-
|
|
22
|
-
## Watch Mode
|
|
23
|
-
|
|
24
|
-
```bash
|
|
25
|
-
$ vitest
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
When you modify your source code or the test files, Vitest smartly searches the module graph and only reruns the related tests, just like how HMR works in Vite!
|
|
29
|
-
|
|
30
|
-
`vitest` starts in `watch mode` **by default in development environment** and `run mode` in CI environment (when `process.env.CI` presents) smartly. You can use `vitest watch` or `vitest run` to explicitly specify the desired mode.
|
|
31
|
-
|
|
32
|
-
Start Vitest with the `--standalone` flag to keep it running in the background. It won't run any tests until they change. Vitest will not run tests if the source code is changed until the test that imports the source has been run
|
|
33
|
-
|
|
34
|
-
## Common Web Idioms Out-Of-The-Box
|
|
35
|
-
|
|
36
|
-
Out-of-the-box ES Module / TypeScript / JSX support / PostCSS
|
|
37
|
-
|
|
38
|
-
## Threads
|
|
39
|
-
|
|
40
|
-
By default Vitest runs test files in [multiple processes](/guide/parallelism) using [`node:child_process`](https://nodejs.org/api/child_process.html), allowing tests to run simultaneously. If you want to speed up your test suite even further, consider enabling `--pool=threads` to run tests using [`node:worker_threads`](https://nodejs.org/api/worker_threads.html) (beware that some packages might not work with this setup).
|
|
41
|
-
To run tests in a single thread or process, see [`fileParallelism`](/config/fileparallelism).
|
|
42
|
-
|
|
43
|
-
Vitest also isolates each file's environment so env mutations in one file don't affect others. Isolation can be disabled by passing `--no-isolate` to the CLI (trading correctness for run performance).
|
|
44
|
-
|
|
45
|
-
## Test Filtering
|
|
46
|
-
|
|
47
|
-
Vitest provides many ways to narrow down the tests to run in order to speed up testing so you can focus on development.
|
|
48
|
-
|
|
49
|
-
Learn more about [Test Filtering](/guide/filtering).
|
|
50
|
-
|
|
51
|
-
## Running Tests Concurrently
|
|
52
|
-
|
|
53
|
-
Use `.concurrent` in consecutive tests to start them in parallel.
|
|
54
|
-
|
|
55
|
-
```ts
|
|
56
|
-
import { describe, it } from 'vitest'
|
|
57
|
-
|
|
58
|
-
// The two tests marked with concurrent will be started in parallel
|
|
59
|
-
describe('suite', () => {
|
|
60
|
-
it('serial test', async () => { /* ... */ })
|
|
61
|
-
it.concurrent('concurrent test 1', async ({ expect }) => { /* ... */ })
|
|
62
|
-
it.concurrent('concurrent test 2', async ({ expect }) => { /* ... */ })
|
|
63
|
-
})
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
If you use `.concurrent` on a suite, every test in it will be started in parallel.
|
|
67
|
-
|
|
68
|
-
```ts
|
|
69
|
-
import { describe, it } from 'vitest'
|
|
70
|
-
|
|
71
|
-
// All tests within this suite will be started in parallel
|
|
72
|
-
describe.concurrent('suite', () => {
|
|
73
|
-
it('concurrent test 1', async ({ expect }) => { /* ... */ })
|
|
74
|
-
it('concurrent test 2', async ({ expect }) => { /* ... */ })
|
|
75
|
-
it.concurrent('concurrent test 3', async ({ expect }) => { /* ... */ })
|
|
76
|
-
})
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
You can also use `.skip`, `.only`, and `.todo` with concurrent suites and tests. Read more in the [API Reference](/api/test#test-concurrent).
|
|
80
|
-
|
|
81
|
-
::: warning
|
|
82
|
-
When running concurrent tests, Snapshots and Assertions must use `expect` from the local [Test Context](/guide/test-context) to ensure the right test is detected.
|
|
83
|
-
:::
|
|
84
|
-
|
|
85
|
-
## Snapshot
|
|
86
|
-
|
|
87
|
-
[Jest-compatible](https://jestjs.io/docs/snapshot-testing) snapshot support.
|
|
88
|
-
|
|
89
|
-
```ts
|
|
90
|
-
import { expect, it } from 'vitest'
|
|
91
|
-
|
|
92
|
-
it('renders correctly', () => {
|
|
93
|
-
const result = render()
|
|
94
|
-
expect(result).toMatchSnapshot()
|
|
95
|
-
})
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
Learn more at [Snapshot](/guide/snapshot).
|
|
99
|
-
|
|
100
|
-
## Chai and Jest `expect` Compatibility
|
|
101
|
-
|
|
102
|
-
[Chai](https://www.chaijs.com/) is built-in for assertions with [Jest `expect`](https://jestjs.io/docs/expect)-compatible APIs.
|
|
103
|
-
|
|
104
|
-
Notice that if you are using third-party libraries that add matchers, setting [`test.globals`](/config/globals) to `true` will provide better compatibility.
|
|
105
|
-
|
|
106
|
-
## Mocking
|
|
107
|
-
|
|
108
|
-
Vitest provides `jest`-compatible APIs on `vi` object.
|
|
109
|
-
|
|
110
|
-
```ts
|
|
111
|
-
import { expect, vi } from 'vitest'
|
|
112
|
-
|
|
113
|
-
const fn = vi.fn()
|
|
114
|
-
|
|
115
|
-
fn('hello', 1)
|
|
116
|
-
|
|
117
|
-
expect(vi.isMockFunction(fn)).toBe(true)
|
|
118
|
-
expect(fn.mock.calls[0]).toEqual(['hello', 1])
|
|
119
|
-
|
|
120
|
-
fn.mockImplementation((arg: string) => arg)
|
|
121
|
-
|
|
122
|
-
fn('world', 2)
|
|
123
|
-
|
|
124
|
-
expect(fn.mock.results[1].value).toBe('world')
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
Vitest supports both [happy-dom](https://github.com/capricorn86/happy-dom) or [jsdom](https://github.com/jsdom/jsdom) for mocking DOM and browser APIs. They don't come with Vitest, you will need to install them separately:
|
|
128
|
-
|
|
129
|
-
::: code-group
|
|
130
|
-
```bash [happy-dom]
|
|
131
|
-
$ npm i -D happy-dom
|
|
132
|
-
```
|
|
133
|
-
```bash [jsdom]
|
|
134
|
-
$ npm i -D jsdom
|
|
135
|
-
```
|
|
136
|
-
:::
|
|
137
|
-
|
|
138
|
-
After that, change the `environment` option in your config file:
|
|
139
|
-
|
|
140
|
-
```ts [vitest.config.ts]
|
|
141
|
-
import { defineConfig } from 'vitest/config'
|
|
142
|
-
|
|
143
|
-
export default defineConfig({
|
|
144
|
-
test: {
|
|
145
|
-
environment: 'happy-dom', // or 'jsdom', 'node'
|
|
146
|
-
},
|
|
147
|
-
})
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
Learn more at [Mocking](/guide/mocking).
|
|
151
|
-
|
|
152
|
-
## Coverage
|
|
153
|
-
|
|
154
|
-
Vitest supports Native code coverage via [`v8`](https://v8.dev/blog/javascript-code-coverage) and instrumented code coverage via [`istanbul`](https://istanbul.js.org/).
|
|
155
|
-
|
|
156
|
-
```json [package.json]
|
|
157
|
-
{
|
|
158
|
-
"scripts": {
|
|
159
|
-
"test": "vitest",
|
|
160
|
-
"coverage": "vitest run --coverage"
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
Learn more at [Coverage](/guide/coverage).
|
|
166
|
-
|
|
167
|
-
## In-Source Testing
|
|
168
|
-
|
|
169
|
-
Vitest also provides a way to run tests within your source code along with the implementation, similar to [Rust's module tests](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest).
|
|
170
|
-
|
|
171
|
-
This makes the tests share the same closure as the implementations and able to test against private states without exporting. Meanwhile, it also brings the feedback loop closer for development.
|
|
172
|
-
|
|
173
|
-
```ts [src/index.ts]
|
|
174
|
-
// the implementation
|
|
175
|
-
export function add(...args: number[]): number {
|
|
176
|
-
return args.reduce((a, b) => a + b, 0)
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// in-source test suites
|
|
180
|
-
if (import.meta.vitest) {
|
|
181
|
-
const { it, expect } = import.meta.vitest
|
|
182
|
-
it('add', () => {
|
|
183
|
-
expect(add()).toBe(0)
|
|
184
|
-
expect(add(1)).toBe(1)
|
|
185
|
-
expect(add(1, 2, 3)).toBe(6)
|
|
186
|
-
})
|
|
187
|
-
}
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
Learn more at [In-source testing](/guide/in-source).
|
|
191
|
-
|
|
192
|
-
## Benchmarking <Badge type="warning">Experimental</Badge> {#benchmarking}
|
|
193
|
-
|
|
194
|
-
You can run benchmark tests with [`bench`](/api/test#bench) function via [Tinybench](https://github.com/tinylibs/tinybench) to compare performance results.
|
|
195
|
-
|
|
196
|
-
```ts [sort.bench.ts]
|
|
197
|
-
import { bench, describe } from 'vitest'
|
|
198
|
-
|
|
199
|
-
describe('sort', () => {
|
|
200
|
-
bench('normal', () => {
|
|
201
|
-
const x = [1, 5, 4, 2, 3]
|
|
202
|
-
x.sort((a, b) => {
|
|
203
|
-
return a - b
|
|
204
|
-
})
|
|
205
|
-
})
|
|
206
|
-
|
|
207
|
-
bench('reverse', () => {
|
|
208
|
-
const x = [1, 5, 4, 2, 3]
|
|
209
|
-
x.reverse().sort((a, b) => {
|
|
210
|
-
return a - b
|
|
211
|
-
})
|
|
212
|
-
})
|
|
213
|
-
})
|
|
214
|
-
```
|
|
215
|
-
|
|
216
|
-
<img alt="Benchmark report" img-dark src="https://github.com/vitest-dev/vitest/assets/4232207/6f0383ea-38ba-4f14-8a05-ab243afea01d">
|
|
217
|
-
<img alt="Benchmark report" img-light src="https://github.com/vitest-dev/vitest/assets/4232207/efbcb427-ecf1-4882-88de-210cd73415f6">
|
|
218
|
-
|
|
219
|
-
## Type Testing <Badge type="warning">Experimental</Badge> {#type-testing}
|
|
220
|
-
|
|
221
|
-
You can [write tests](/guide/testing-types) to catch type regressions. Vitest comes with [`expect-type`](https://github.com/mmkal/expect-type) package to provide you with a similar and easy to understand API.
|
|
222
|
-
|
|
223
|
-
```ts [types.test-d.ts]
|
|
224
|
-
import { assertType, expectTypeOf, test } from 'vitest'
|
|
225
|
-
import { mount } from './mount.js'
|
|
226
|
-
|
|
227
|
-
test('my types work properly', () => {
|
|
228
|
-
expectTypeOf(mount).toBeFunction()
|
|
229
|
-
expectTypeOf(mount).parameter(0).toExtend<{ name: string }>()
|
|
230
|
-
|
|
231
|
-
// @ts-expect-error name is a string
|
|
232
|
-
assertType(mount({ name: 42 }))
|
|
233
|
-
})
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
## Sharding
|
|
237
|
-
|
|
238
|
-
Run tests on different machines using [`--shard`](/guide/cli#shard) and [`--reporter=blob`](/guide/reporters#blob-reporter) flags.
|
|
239
|
-
All test and coverage results can be merged at the end of your CI pipeline using `--merge-reports` command:
|
|
240
|
-
|
|
241
|
-
```bash
|
|
242
|
-
vitest --shard=1/2 --reporter=blob --coverage
|
|
243
|
-
vitest --shard=2/2 --reporter=blob --coverage
|
|
244
|
-
vitest --merge-reports --reporter=junit --coverage
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
See [`Improving Performance | Sharding`](/guide/improving-performance#sharding) for more information.
|
|
248
|
-
|
|
249
|
-
## Environment Variables
|
|
250
|
-
|
|
251
|
-
Vitest exclusively autoloads environment variables prefixed with `VITE_` from `.env` files to maintain compatibility with frontend-related tests, adhering to [Vite's established convention](https://vitejs.dev/guide/env-and-mode.html#env-files). To load every environmental variable from `.env` files anyway, you can use `loadEnv` method imported from `vite`:
|
|
252
|
-
|
|
253
|
-
```ts [vitest.config.ts]
|
|
254
|
-
import { loadEnv } from 'vite'
|
|
255
|
-
import { defineConfig } from 'vitest/config'
|
|
256
|
-
|
|
257
|
-
export default defineConfig(({ mode }) => ({
|
|
258
|
-
test: {
|
|
259
|
-
// mode defines what ".env.{mode}" file to choose if exists
|
|
260
|
-
env: loadEnv(mode, process.cwd(), ''),
|
|
261
|
-
},
|
|
262
|
-
}))
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
## Unhandled Errors
|
|
266
|
-
|
|
267
|
-
By default, Vitest catches and reports all [unhandled rejections](https://developer.mozilla.org/en-US/docs/Web/API/Window/unhandledrejection_event), [uncaught exceptions](https://nodejs.org/api/process.html#event-uncaughtexception) (in Node.js) and [error](https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event) events (in the [browser](/guide/browser/)).
|
|
268
|
-
|
|
269
|
-
You can disable this behaviour by catching them manually. Vitest assumes the callback is handled by you and won't report the error.
|
|
270
|
-
|
|
271
|
-
::: code-group
|
|
272
|
-
```ts [setup.node.js]
|
|
273
|
-
// in Node.js
|
|
274
|
-
process.on('unhandledRejection', () => {
|
|
275
|
-
// your own handler
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
process.on('uncaughtException', () => {
|
|
279
|
-
// your own handler
|
|
280
|
-
})
|
|
281
|
-
```
|
|
282
|
-
```ts [setup.browser.js]
|
|
283
|
-
// in the browser
|
|
284
|
-
window.addEventListener('error', () => {
|
|
285
|
-
// your own handler
|
|
286
|
-
})
|
|
287
|
-
|
|
288
|
-
window.addEventListener('unhandledrejection', () => {
|
|
289
|
-
// your own handler
|
|
290
|
-
})
|
|
291
|
-
```
|
|
292
|
-
:::
|
|
293
|
-
|
|
294
|
-
Alternatively, you can also ignore reported errors with a [`dangerouslyIgnoreUnhandledErrors`](/config/dangerouslyignoreunhandlederrors) option. Vitest will still report them, but they won't affect the test result (exit code won't be changed).
|
|
295
|
-
|
|
296
|
-
If you need to test that error was not caught, you can create a test that looks like this:
|
|
297
|
-
|
|
298
|
-
```ts
|
|
299
|
-
test('my function throws uncaught error', async ({ onTestFinished }) => {
|
|
300
|
-
const unhandledRejectionListener = vi.fn()
|
|
301
|
-
process.on('unhandledRejection', unhandledRejectionListener)
|
|
302
|
-
onTestFinished(() => {
|
|
303
|
-
process.off('unhandledRejection', unhandledRejectionListener)
|
|
304
|
-
})
|
|
305
|
-
|
|
306
|
-
callMyFunctionThatRejectsError()
|
|
307
|
-
|
|
308
|
-
await expect.poll(unhandledRejectionListener).toHaveBeenCalled()
|
|
309
|
-
})
|
|
310
|
-
```
|