@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,239 +0,0 @@
|
|
|
1
|
-
# Mocking
|
|
2
|
-
|
|
3
|
-
::: tip
|
|
4
|
-
New to mocking? Start with the [Mock Functions](/guide/learn/mock-functions) tutorial for a hands-on introduction to `vi.fn`, `vi.spyOn`, and `vi.mock`.
|
|
5
|
-
:::
|
|
6
|
-
|
|
7
|
-
When writing tests it's only a matter of time before you need to create a "fake" version of an internal — or external — service. This is commonly referred to as **mocking**. Vitest provides utility functions to help you out through its `vi` helper. You can import it from `vitest` or access it globally if [`global` configuration](/config/globals) is enabled.
|
|
8
|
-
|
|
9
|
-
::: warning
|
|
10
|
-
Always remember to clear or restore mocks before or after each test run to undo mock state changes between runs! See [`mockReset`](/api/mock#mockreset) docs for more info.
|
|
11
|
-
:::
|
|
12
|
-
|
|
13
|
-
If you are not familiar with `vi.fn`, `vi.mock` or `vi.spyOn` methods, check the [API section](/api/vi) first.
|
|
14
|
-
|
|
15
|
-
Vitest has a comprehensive list of guides regarding mocking:
|
|
16
|
-
|
|
17
|
-
- [Mocking Classes](/guide/mocking/classes.md)
|
|
18
|
-
- [Mocking Dates](/guide/mocking/dates.md)
|
|
19
|
-
- [Mocking the File System](/guide/mocking/file-system.md)
|
|
20
|
-
- [Mocking Functions](/guide/mocking/functions.md)
|
|
21
|
-
- [Mocking Globals](/guide/mocking/globals.md)
|
|
22
|
-
- [Mocking Modules](/guide/mocking/modules.md)
|
|
23
|
-
- [Mocking Requests](/guide/mocking/requests.md)
|
|
24
|
-
- [Mocking Timers](/guide/mocking/timers.md)
|
|
25
|
-
|
|
26
|
-
For a simpler and quicker way to get started with mocking, you can check the Cheat Sheet below.
|
|
27
|
-
|
|
28
|
-
## Cheat Sheet
|
|
29
|
-
|
|
30
|
-
I want to…
|
|
31
|
-
|
|
32
|
-
### Mock exported variables
|
|
33
|
-
```js [example.js]
|
|
34
|
-
export const getter = 'variable'
|
|
35
|
-
```
|
|
36
|
-
```ts [example.test.ts]
|
|
37
|
-
import * as exports from './example.js'
|
|
38
|
-
|
|
39
|
-
vi.spyOn(exports, 'getter', 'get').mockReturnValue('mocked')
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
::: warning
|
|
43
|
-
This will not work in the Browser Mode. For a workaround, see [Limitations](/guide/browser/#spying-on-module-exports).
|
|
44
|
-
:::
|
|
45
|
-
|
|
46
|
-
### Mock an exported function
|
|
47
|
-
|
|
48
|
-
1. Example with `vi.mock`:
|
|
49
|
-
|
|
50
|
-
::: warning
|
|
51
|
-
Don't forget that a `vi.mock` call is hoisted to top of the file. It will always be executed before all imports.
|
|
52
|
-
:::
|
|
53
|
-
|
|
54
|
-
```ts [example.js]
|
|
55
|
-
export function method() {}
|
|
56
|
-
```
|
|
57
|
-
```ts
|
|
58
|
-
import { method } from './example.js'
|
|
59
|
-
|
|
60
|
-
vi.mock('./example.js', () => ({
|
|
61
|
-
method: vi.fn()
|
|
62
|
-
}))
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
2. Example with `vi.spyOn`:
|
|
66
|
-
```ts
|
|
67
|
-
import * as exports from './example.js'
|
|
68
|
-
|
|
69
|
-
vi.spyOn(exports, 'method').mockImplementation(() => {})
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
::: warning
|
|
73
|
-
`vi.spyOn` example will not work in the Browser Mode. For a workaround, see [Limitations](/guide/browser/#spying-on-module-exports).
|
|
74
|
-
:::
|
|
75
|
-
|
|
76
|
-
### Mock an exported class implementation
|
|
77
|
-
|
|
78
|
-
1. Example with a fake `class`:
|
|
79
|
-
```ts [example.js]
|
|
80
|
-
export class SomeClass {}
|
|
81
|
-
```
|
|
82
|
-
```ts
|
|
83
|
-
import { SomeClass } from './example.js'
|
|
84
|
-
|
|
85
|
-
vi.mock(import('./example.js'), () => {
|
|
86
|
-
const SomeClass = vi.fn(class FakeClass {
|
|
87
|
-
someMethod = vi.fn()
|
|
88
|
-
})
|
|
89
|
-
return { SomeClass }
|
|
90
|
-
})
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
2. Example with `vi.spyOn`:
|
|
94
|
-
|
|
95
|
-
```ts
|
|
96
|
-
import * as mod from './example.js'
|
|
97
|
-
|
|
98
|
-
vi.spyOn(mod, 'SomeClass').mockImplementation(class FakeClass {
|
|
99
|
-
someMethod = vi.fn()
|
|
100
|
-
})
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
::: warning
|
|
104
|
-
`vi.spyOn` example will not work in the Browser Mode. For a workaround, see [Limitations](/guide/browser/#spying-on-module-exports).
|
|
105
|
-
:::
|
|
106
|
-
|
|
107
|
-
### Spy on an object returned from a function
|
|
108
|
-
|
|
109
|
-
1. Example using cache:
|
|
110
|
-
|
|
111
|
-
```ts [example.js]
|
|
112
|
-
export function useObject() {
|
|
113
|
-
return { method: () => true }
|
|
114
|
-
}
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
```ts [useObject.js]
|
|
118
|
-
import { useObject } from './example.js'
|
|
119
|
-
|
|
120
|
-
const obj = useObject()
|
|
121
|
-
obj.method()
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
```ts [useObject.test.js]
|
|
125
|
-
import { useObject } from './example.js'
|
|
126
|
-
|
|
127
|
-
vi.mock(import('./example.js'), () => {
|
|
128
|
-
let _cache
|
|
129
|
-
const useObject = () => {
|
|
130
|
-
if (!_cache) {
|
|
131
|
-
_cache = {
|
|
132
|
-
method: vi.fn(),
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
// now every time that useObject() is called it will
|
|
136
|
-
// return the same object reference
|
|
137
|
-
return _cache
|
|
138
|
-
}
|
|
139
|
-
return { useObject }
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
const obj = useObject()
|
|
143
|
-
// obj.method was called inside some-path
|
|
144
|
-
expect(obj.method).toHaveBeenCalled()
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
### Mock part of a module
|
|
148
|
-
|
|
149
|
-
```ts
|
|
150
|
-
import { mocked, original } from './some-path.js'
|
|
151
|
-
|
|
152
|
-
vi.mock(import('./some-path.js'), async (importOriginal) => {
|
|
153
|
-
const mod = await importOriginal()
|
|
154
|
-
return {
|
|
155
|
-
...mod,
|
|
156
|
-
mocked: vi.fn()
|
|
157
|
-
}
|
|
158
|
-
})
|
|
159
|
-
original() // has original behaviour
|
|
160
|
-
mocked() // is a spy function
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
::: warning
|
|
164
|
-
Don't forget that this only [mocks _external_ access](/guide/mocking/modules#mocking-modules-pitfalls). In this example, if `original` calls `mocked` internally, it will always call the function defined in the module, not in the mock factory.
|
|
165
|
-
:::
|
|
166
|
-
|
|
167
|
-
### Mock the current date
|
|
168
|
-
|
|
169
|
-
To mock `Date`'s time, you can use `vi.setSystemTime` helper function. This value will **not** automatically reset between different tests.
|
|
170
|
-
|
|
171
|
-
Beware that using `vi.useFakeTimers` also changes the `Date`'s time.
|
|
172
|
-
|
|
173
|
-
```ts
|
|
174
|
-
const mockDate = new Date(2022, 0, 1)
|
|
175
|
-
vi.setSystemTime(mockDate)
|
|
176
|
-
const now = new Date()
|
|
177
|
-
expect(now.valueOf()).toBe(mockDate.valueOf())
|
|
178
|
-
// reset mocked time
|
|
179
|
-
vi.useRealTimers()
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### Mock a global variable
|
|
183
|
-
|
|
184
|
-
You can set global variable by assigning a value to `globalThis` or using [`vi.stubGlobal`](/api/vi#vi-stubglobal) helper. When using `vi.stubGlobal`, it will **not** automatically reset between different tests, unless you enable [`unstubGlobals`](/config/unstubglobals) config option or call [`vi.unstubAllGlobals`](/api/vi#vi-unstuballglobals).
|
|
185
|
-
|
|
186
|
-
```ts
|
|
187
|
-
vi.stubGlobal('__VERSION__', '1.0.0')
|
|
188
|
-
expect(__VERSION__).toBe('1.0.0')
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
### Mock `import.meta.env`
|
|
192
|
-
|
|
193
|
-
1. To change environmental variable, you can just assign a new value to it.
|
|
194
|
-
|
|
195
|
-
::: warning
|
|
196
|
-
The environmental variable value will **_not_** automatically reset between different tests.
|
|
197
|
-
:::
|
|
198
|
-
|
|
199
|
-
```ts
|
|
200
|
-
import { beforeEach, expect, it } from 'vitest'
|
|
201
|
-
|
|
202
|
-
// you can reset it in beforeEach hook manually
|
|
203
|
-
const originalViteEnv = import.meta.env.VITE_ENV
|
|
204
|
-
|
|
205
|
-
beforeEach(() => {
|
|
206
|
-
import.meta.env.VITE_ENV = originalViteEnv
|
|
207
|
-
})
|
|
208
|
-
|
|
209
|
-
it('changes value', () => {
|
|
210
|
-
import.meta.env.VITE_ENV = 'staging'
|
|
211
|
-
expect(import.meta.env.VITE_ENV).toBe('staging')
|
|
212
|
-
})
|
|
213
|
-
```
|
|
214
|
-
|
|
215
|
-
2. If you want to automatically reset the value(s), you can use the `vi.stubEnv` helper with the [`unstubEnvs`](/config/unstubenvs) config option enabled (or call [`vi.unstubAllEnvs`](/api/vi#vi-unstuballenvs) manually in a `beforeEach` hook):
|
|
216
|
-
|
|
217
|
-
```ts
|
|
218
|
-
import { expect, it, vi } from 'vitest'
|
|
219
|
-
|
|
220
|
-
// before running tests "VITE_ENV" is "test"
|
|
221
|
-
import.meta.env.VITE_ENV === 'test'
|
|
222
|
-
|
|
223
|
-
it('changes value', () => {
|
|
224
|
-
vi.stubEnv('VITE_ENV', 'staging')
|
|
225
|
-
expect(import.meta.env.VITE_ENV).toBe('staging')
|
|
226
|
-
})
|
|
227
|
-
|
|
228
|
-
it('the value is restored before running an other test', () => {
|
|
229
|
-
expect(import.meta.env.VITE_ENV).toBe('test')
|
|
230
|
-
})
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
```ts [vitest.config.ts]
|
|
234
|
-
export default defineConfig({
|
|
235
|
-
test: {
|
|
236
|
-
unstubEnvs: true,
|
|
237
|
-
},
|
|
238
|
-
})
|
|
239
|
-
```
|
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
# Open Telemetry Support <Experimental /> {#open-telemetry-support}
|
|
2
|
-
|
|
3
|
-
::: tip FEEDBACK
|
|
4
|
-
Please, leave feedback regarding this feature in a [GitHub Discussion](https://github.com/vitest-dev/vitest/discussions/9222).
|
|
5
|
-
:::
|
|
6
|
-
|
|
7
|
-
::: tip Example Project
|
|
8
|
-
[GitHub](https://github.com/vitest-dev/vitest/tree/main/examples/opentelemetry)
|
|
9
|
-
:::
|
|
10
|
-
|
|
11
|
-
[OpenTelemetry](https://opentelemetry.io/) traces can be a useful tool to debug the performance and behavior of your application inside tests.
|
|
12
|
-
|
|
13
|
-
If enabled, Vitest integration generates spans that are scoped to your test's worker.
|
|
14
|
-
|
|
15
|
-
::: warning
|
|
16
|
-
OpenTelemetry initialization increases the startup time of every test unless Vitest runs without [isolation](/config/isolate). You can see it as the `vitest.runtime.traces` span inside `vitest.worker.start`.
|
|
17
|
-
:::
|
|
18
|
-
|
|
19
|
-
To start using OpenTelemetry in Vitest, specify an SDK module path via [`experimental.openTelemetry.sdkPath`](/config/experimental#experimental-opentelemetry) and set `experimental.openTelemetry.enabled` to `true`. Vitest will automatically instrument the whole process and each individual test worker.
|
|
20
|
-
|
|
21
|
-
Make sure to export the SDK as a default export, so that Vitest can flush the network requests before the process is closed. Note that Vitest doesn't automatically call `start`.
|
|
22
|
-
|
|
23
|
-
## Quickstart
|
|
24
|
-
|
|
25
|
-
Before previewing your application traces, install required packages and specify the path to your instrumentation file in the config.
|
|
26
|
-
|
|
27
|
-
```shell
|
|
28
|
-
npm i @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node @opentelemetry/exporter-trace-otlp-proto
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
::: code-group
|
|
32
|
-
```js{12} [otel.js]
|
|
33
|
-
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node'
|
|
34
|
-
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'
|
|
35
|
-
import { NodeSDK } from '@opentelemetry/sdk-node'
|
|
36
|
-
|
|
37
|
-
const sdk = new NodeSDK({
|
|
38
|
-
serviceName: 'vitest',
|
|
39
|
-
traceExporter: new OTLPTraceExporter(),
|
|
40
|
-
instrumentations: [getNodeAutoInstrumentations()],
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
sdk.start()
|
|
44
|
-
export default sdk
|
|
45
|
-
```
|
|
46
|
-
```js [vitest.config.js]
|
|
47
|
-
import { defineConfig } from 'vitest/config'
|
|
48
|
-
|
|
49
|
-
export default defineConfig({
|
|
50
|
-
test: {
|
|
51
|
-
experimental: {
|
|
52
|
-
openTelemetry: {
|
|
53
|
-
enabled: true,
|
|
54
|
-
sdkPath: './otel.js',
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
})
|
|
59
|
-
```
|
|
60
|
-
:::
|
|
61
|
-
|
|
62
|
-
::: danger FAKE TIMERS
|
|
63
|
-
If you are using fake timers, it is important to reset them before the test ends, otherwise traces might not be tracked properly.
|
|
64
|
-
:::
|
|
65
|
-
|
|
66
|
-
Vitest doesn't process the `sdkPath` module, so it is important that the SDK can be imported within your Node.js environment. It is ideal to use the `.js` extension for this file. Using another extension will slow down your tests and may require providing additional Node.js arguments.
|
|
67
|
-
|
|
68
|
-
If you want to provide a TypeScript file, make sure to familiarize yourself with [TypeScript](https://nodejs.org/api/typescript.html#type-stripping) page in the Node.js documentation.
|
|
69
|
-
|
|
70
|
-
## Custom Traces
|
|
71
|
-
|
|
72
|
-
You can use the OpenTelemetry API yourself to track certain operations in your code. Custom traces automatically inherit the Vitest OpenTelemetry context:
|
|
73
|
-
|
|
74
|
-
```ts
|
|
75
|
-
import { trace } from '@opentelemetry/api'
|
|
76
|
-
import { test } from 'vitest'
|
|
77
|
-
import { db } from './src/db'
|
|
78
|
-
|
|
79
|
-
const tracer = trace.getTracer('vitest')
|
|
80
|
-
|
|
81
|
-
test('db connects properly', async () => {
|
|
82
|
-
// this is shown inside `vitest.test.runner.test.callback` span
|
|
83
|
-
await tracer.startActiveSpan('db.connect', () => db.connect())
|
|
84
|
-
})
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## Browser Mode
|
|
88
|
-
|
|
89
|
-
When running tests in [browser mode](/guide/browser/), Vitest propagates trace context between Node.js and the browser. Node.js side traces (test orchestration, browser driver communication) are available without additional configuration.
|
|
90
|
-
|
|
91
|
-
To capture traces from the browser runtime, provide a browser-compatible SDK via `browserSdkPath`:
|
|
92
|
-
|
|
93
|
-
```shell
|
|
94
|
-
npm i @opentelemetry/sdk-trace-web @opentelemetry/exporter-trace-otlp-proto
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
::: code-group
|
|
98
|
-
```js [otel-browser.js]
|
|
99
|
-
import {
|
|
100
|
-
BatchSpanProcessor,
|
|
101
|
-
WebTracerProvider,
|
|
102
|
-
} from '@opentelemetry/sdk-trace-web'
|
|
103
|
-
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-proto'
|
|
104
|
-
|
|
105
|
-
const provider = new WebTracerProvider({
|
|
106
|
-
spanProcessors: [
|
|
107
|
-
new BatchSpanProcessor(new OTLPTraceExporter()),
|
|
108
|
-
],
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
provider.register()
|
|
112
|
-
export default provider
|
|
113
|
-
```
|
|
114
|
-
```js [vitest.config.js]
|
|
115
|
-
import { defineConfig } from 'vitest/config'
|
|
116
|
-
|
|
117
|
-
export default defineConfig({
|
|
118
|
-
test: {
|
|
119
|
-
browser: {
|
|
120
|
-
enabled: true,
|
|
121
|
-
provider: 'playwright',
|
|
122
|
-
instances: [{ browser: 'chromium' }],
|
|
123
|
-
},
|
|
124
|
-
experimental: {
|
|
125
|
-
openTelemetry: {
|
|
126
|
-
enabled: true,
|
|
127
|
-
sdkPath: './otel.js',
|
|
128
|
-
browserSdkPath: './otel-browser.js',
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
})
|
|
133
|
-
```
|
|
134
|
-
:::
|
|
135
|
-
|
|
136
|
-
::: warning ASYNC CONTEXT
|
|
137
|
-
Unlike Node.js, browsers do not have automatic async context propagation. Vitest handles this internally for test execution, but custom spans in deeply nested async code may not propagate context automatically.
|
|
138
|
-
:::
|
|
139
|
-
|
|
140
|
-
## View Traces
|
|
141
|
-
|
|
142
|
-
To generate traces, run Vitest as usual. You can run Vitest in either watch mode or run mode. Vitest will call `sdk.shutdown()` manually after everything is finished to make sure traces are handled properly.
|
|
143
|
-
|
|
144
|
-
You can view traces using any of the open source or commercial products that support OpenTelemetry API. If you did not use OpenTelemetry before, we recommend starting with [Jaeger](https://www.jaegertracing.io/docs/2.11/getting-started/#all-in-one) because it is really easy to setup.
|
|
145
|
-
|
|
146
|
-
<img src="/otel-jaeger.png" alt="an example of open telemetry result in jaeger" />
|
|
147
|
-
|
|
148
|
-
## `@opentelemetry/api`
|
|
149
|
-
|
|
150
|
-
Vitest declares `@opentelemetry/api` as an optional peer dependency, which it uses internally to generate spans. When trace collection is not enabled, Vitest will not attempt to use this dependency.
|
|
151
|
-
|
|
152
|
-
When configuring Vitest to use OpenTelemetry, you will typically install `@opentelemetry/sdk-node`, which includes `@opentelemetry/api` as a transitive dependency, thereby satisfying Vitest's peer dependency requirement. If you encounter an error indicating that `@opentelemetry/api` cannot be found, this typically means trace collection has not been enabled. If the error persists after proper configuration, you may need to install `@opentelemetry/api` explicitly.
|
|
153
|
-
|
|
154
|
-
## Inter-Process Context Propagation
|
|
155
|
-
|
|
156
|
-
Vitest supports automatic context propagation from parent processes via the `TRACEPARENT` and `TRACESTATE` environment variables as defined in the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/env-carriers.md). This is particularly useful when running Vitest as part of a larger distributed tracing system (e.g., CI/CD pipelines with OpenTelemetry instrumentation).
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
# Parallelism
|
|
2
|
-
|
|
3
|
-
Vitest has two levels of parallelism: it can run multiple *test files* at the same time, and within each file it can run multiple *tests* at the same time. Understanding the difference between the two is important because they work differently and have different trade-offs.
|
|
4
|
-
|
|
5
|
-
## File Parallelism
|
|
6
|
-
|
|
7
|
-
By default, Vitest runs test files in parallel across multiple workers. Each file gets its own isolated environment, so tests in different files can't interfere with each other.
|
|
8
|
-
|
|
9
|
-
The mechanism Vitest uses to create workers depends on the configured [`pool`](/config/pool):
|
|
10
|
-
|
|
11
|
-
- `forks` (the default) and `vmForks` run each file in a separate [child process](https://nodejs.org/api/child_process.html)
|
|
12
|
-
- `threads` and `vmThreads` run each file in a separate [worker thread](https://nodejs.org/api/worker_threads.html)
|
|
13
|
-
|
|
14
|
-
You can control how many workers run simultaneously with the [`maxWorkers`](/config/maxworkers) option. More workers means more files run in parallel, but also more memory and CPU usage. The right number depends on your machine and how heavy your tests are.
|
|
15
|
-
|
|
16
|
-
For most projects, file parallelism is the single biggest factor in test suite speed. However, there are cases where you might want to disable it — for example, if your tests share an external resource like a database that can't handle concurrent access. You can set [`fileParallelism`](/config/fileparallelism) to `false` to run files one at a time.
|
|
17
|
-
|
|
18
|
-
To learn more about tuning performance, see the [Performance Guide](/guide/improving-performance).
|
|
19
|
-
|
|
20
|
-
## Test Parallelism
|
|
21
|
-
|
|
22
|
-
Within a single file, Vitest runs tests sequentially by default. Tests execute in the order they are defined, one after another. This is the safest default because tests within a file often share setup and state through lifecycle hooks like `beforeEach`.
|
|
23
|
-
|
|
24
|
-
If the tests in a file are independent, you can opt into running them concurrently with the [`concurrent`](/api/test#test-concurrent) modifier:
|
|
25
|
-
|
|
26
|
-
```ts
|
|
27
|
-
import { expect, test } from 'vitest'
|
|
28
|
-
|
|
29
|
-
test.concurrent('fetches user profile', async () => {
|
|
30
|
-
const user = await fetchUser(1)
|
|
31
|
-
expect(user.name).toBe('Alice')
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
test.concurrent('fetches user posts', async () => {
|
|
35
|
-
const posts = await fetchPosts(1)
|
|
36
|
-
expect(posts).toHaveLength(3)
|
|
37
|
-
})
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
When tests are marked as `concurrent`, Vitest groups them together and runs them with [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all). The number of tests running at once is bounded by the [`maxConcurrency`](/config/maxconcurrency) option.
|
|
41
|
-
|
|
42
|
-
::: tip When does `concurrent` actually help?
|
|
43
|
-
Vitest doesn't create extra workers for concurrent tests — they all run in the same worker as the file they belong to. This means `concurrent` only speeds things up when your tests spend time *waiting* (on network requests, timers, file I/O, etc.). Purely synchronous tests won't benefit because they still block the single JavaScript thread:
|
|
44
|
-
|
|
45
|
-
```ts
|
|
46
|
-
// These run one after another despite `concurrent`,
|
|
47
|
-
// because there is nothing to await
|
|
48
|
-
test.concurrent('the first test', () => {
|
|
49
|
-
expect(1).toBe(1)
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test.concurrent('the second test', () => {
|
|
53
|
-
expect(2).toBe(2)
|
|
54
|
-
})
|
|
55
|
-
```
|
|
56
|
-
:::
|
|
57
|
-
|
|
58
|
-
You can also apply `concurrent` to an entire suite:
|
|
59
|
-
|
|
60
|
-
```ts
|
|
61
|
-
import { describe, expect, test } from 'vitest'
|
|
62
|
-
|
|
63
|
-
describe.concurrent('user API', () => {
|
|
64
|
-
test('fetches profile', async () => {
|
|
65
|
-
const user = await fetchUser(1)
|
|
66
|
-
expect(user.name).toBe('Alice')
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
test('fetches posts', async () => {
|
|
70
|
-
const posts = await fetchPosts(1)
|
|
71
|
-
expect(posts).toHaveLength(3)
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
If you want *all* tests in your project to run concurrently by default, set [`sequence.concurrent`](/config/sequence#sequence-concurrent) to `true` in your config.
|
|
77
|
-
|
|
78
|
-
### Hooks with Concurrent Tests
|
|
79
|
-
|
|
80
|
-
When tests run concurrently, lifecycle hooks behave differently. `beforeAll` and `afterAll` still run once for the group, but `beforeEach` and `afterEach` run for each test — potentially at the same time, since the tests themselves overlap.
|
|
81
|
-
|
|
82
|
-
The hook execution order is controlled by [`sequence.hooks`](/config/sequence#sequence-hooks). With `sequence.hooks: 'parallel'`, hooks are also bounded by the [`maxConcurrency`](/config/maxconcurrency) limit.
|