@vitest-agent/mcp 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/vitest-agent-mcp.js +2 -2
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +2 -2
- package/public/patterns/_meta.json +67 -0
- package/public/patterns/authoring-a-custom-vitest-agent-reporter.md +82 -0
- package/public/patterns/known-issues-and-caveats.md +52 -0
- package/public/patterns/operating-vitest-agent-as-an-agent.md +53 -0
- package/public/patterns/running-tests-via-mcp.md +58 -0
- package/public/patterns/silencing-leaking-output-in-tests.md +91 -0
- package/public/patterns/testing-effect-schema-definitions.md +71 -0
- package/public/patterns/testing-effect-services-with-mock-layers.md +63 -0
- package/public/vendor/vitest-docs/ATTRIBUTION.md +5 -0
- package/public/vendor/vitest-docs/api/advanced/artifacts.md +189 -0
- package/public/vendor/vitest-docs/api/advanced/metadata.md +68 -0
- package/public/vendor/vitest-docs/api/advanced/plugin.md +168 -0
- package/public/vendor/vitest-docs/api/advanced/reporters.md +342 -0
- package/public/vendor/vitest-docs/api/advanced/runner.md +334 -0
- package/public/vendor/vitest-docs/api/advanced/test-case.md +302 -0
- package/public/vendor/vitest-docs/api/advanced/test-collection.md +89 -0
- package/public/vendor/vitest-docs/api/advanced/test-module.md +140 -0
- package/public/vendor/vitest-docs/api/advanced/test-project.md +321 -0
- package/public/vendor/vitest-docs/api/advanced/test-specification.md +96 -0
- package/public/vendor/vitest-docs/api/advanced/test-suite.md +230 -0
- package/public/vendor/vitest-docs/api/advanced/vitest.md +684 -0
- package/public/vendor/vitest-docs/api/assert-type.md +22 -0
- package/public/vendor/vitest-docs/api/assert.md +1960 -0
- package/public/vendor/vitest-docs/api/browser/assertions.md +1277 -0
- package/public/vendor/vitest-docs/api/browser/commands.md +154 -0
- package/public/vendor/vitest-docs/api/browser/context.md +338 -0
- package/public/vendor/vitest-docs/api/browser/interactivity.md +681 -0
- package/public/vendor/vitest-docs/api/browser/locators.md +1171 -0
- package/public/vendor/vitest-docs/api/browser/react.md +346 -0
- package/public/vendor/vitest-docs/api/browser/svelte.md +292 -0
- package/public/vendor/vitest-docs/api/browser/vue.md +222 -0
- package/public/vendor/vitest-docs/api/describe.md +374 -0
- package/public/vendor/vitest-docs/api/expect-typeof.md +571 -0
- package/public/vendor/vitest-docs/api/expect.md +2304 -0
- package/public/vendor/vitest-docs/api/hooks.md +463 -0
- package/public/vendor/vitest-docs/api/mock.md +701 -0
- package/public/vendor/vitest-docs/api/test.md +926 -0
- package/public/vendor/vitest-docs/api/vi.md +1372 -0
- package/public/vendor/vitest-docs/config/alias.md +13 -0
- package/public/vendor/vitest-docs/config/allowonly.md +32 -0
- package/public/vendor/vitest-docs/config/api.md +27 -0
- package/public/vendor/vitest-docs/config/attachmentsdir.md +6 -0
- package/public/vendor/vitest-docs/config/bail.md +9 -0
- package/public/vendor/vitest-docs/config/benchmark.md +65 -0
- package/public/vendor/vitest-docs/config/browser/api.md +23 -0
- package/public/vendor/vitest-docs/config/browser/commands.md +6 -0
- package/public/vendor/vitest-docs/config/browser/connecttimeout.md +10 -0
- package/public/vendor/vitest-docs/config/browser/detailspanelposition.md +38 -0
- package/public/vendor/vitest-docs/config/browser/enabled.md +40 -0
- package/public/vendor/vitest-docs/config/browser/expect.md +250 -0
- package/public/vendor/vitest-docs/config/browser/headless.md +7 -0
- package/public/vendor/vitest-docs/config/browser/instances.md +47 -0
- package/public/vendor/vitest-docs/config/browser/isolate.md +11 -0
- package/public/vendor/vitest-docs/config/browser/locators.md +24 -0
- package/public/vendor/vitest-docs/config/browser/orchestratorscripts.md +39 -0
- package/public/vendor/vitest-docs/config/browser/playwright.md +214 -0
- package/public/vendor/vitest-docs/config/browser/preview.md +32 -0
- package/public/vendor/vitest-docs/config/browser/provider.md +79 -0
- package/public/vendor/vitest-docs/config/browser/screenshotdirectory.md +6 -0
- package/public/vendor/vitest-docs/config/browser/screenshotfailures.md +6 -0
- package/public/vendor/vitest-docs/config/browser/testerhtmlpath.md +5 -0
- package/public/vendor/vitest-docs/config/browser/trace.md +43 -0
- package/public/vendor/vitest-docs/config/browser/trackunhandlederrors.md +10 -0
- package/public/vendor/vitest-docs/config/browser/ui.md +7 -0
- package/public/vendor/vitest-docs/config/browser/viewport.md +6 -0
- package/public/vendor/vitest-docs/config/browser/webdriverio.md +64 -0
- package/public/vendor/vitest-docs/config/cache.md +26 -0
- package/public/vendor/vitest-docs/config/chaiconfig.md +29 -0
- package/public/vendor/vitest-docs/config/clearmocks.md +22 -0
- package/public/vendor/vitest-docs/config/coverage.md +455 -0
- package/public/vendor/vitest-docs/config/css.md +47 -0
- package/public/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +23 -0
- package/public/vendor/vitest-docs/config/deps.md +127 -0
- package/public/vendor/vitest-docs/config/detectasyncleaks.md +39 -0
- package/public/vendor/vitest-docs/config/diff.md +96 -0
- package/public/vendor/vitest-docs/config/dir.md +7 -0
- package/public/vendor/vitest-docs/config/disableconsoleintercept.md +15 -0
- package/public/vendor/vitest-docs/config/env.md +5 -0
- package/public/vendor/vitest-docs/config/environment.md +96 -0
- package/public/vendor/vitest-docs/config/environmentoptions.md +30 -0
- package/public/vendor/vitest-docs/config/exclude.md +49 -0
- package/public/vendor/vitest-docs/config/execargv.md +10 -0
- package/public/vendor/vitest-docs/config/expandsnapshotdiff.md +7 -0
- package/public/vendor/vitest-docs/config/expect.md +38 -0
- package/public/vendor/vitest-docs/config/experimental.md +510 -0
- package/public/vendor/vitest-docs/config/faketimers.md +51 -0
- package/public/vendor/vitest-docs/config/fileparallelism.md +11 -0
- package/public/vendor/vitest-docs/config/forcereruntriggers.md +19 -0
- package/public/vendor/vitest-docs/config/globals.md +42 -0
- package/public/vendor/vitest-docs/config/globalsetup.md +72 -0
- package/public/vendor/vitest-docs/config/hideskippedtests.md +7 -0
- package/public/vendor/vitest-docs/config/hooktimeout.md +7 -0
- package/public/vendor/vitest-docs/config/include-source.md +115 -0
- package/public/vendor/vitest-docs/config/include.md +71 -0
- package/public/vendor/vitest-docs/config/includetasklocation.md +17 -0
- package/public/vendor/vitest-docs/config/index.md +85 -0
- package/public/vendor/vitest-docs/config/isolate.md +13 -0
- package/public/vendor/vitest-docs/config/logheapusage.md +7 -0
- package/public/vendor/vitest-docs/config/maxconcurrency.md +9 -0
- package/public/vendor/vitest-docs/config/maxworkers.md +49 -0
- package/public/vendor/vitest-docs/config/mockreset.md +22 -0
- package/public/vendor/vitest-docs/config/mode.md +7 -0
- package/public/vendor/vitest-docs/config/name.md +111 -0
- package/public/vendor/vitest-docs/config/onconsolelog.md +25 -0
- package/public/vendor/vitest-docs/config/onstacktrace.md +32 -0
- package/public/vendor/vitest-docs/config/onunhandlederror.md +35 -0
- package/public/vendor/vitest-docs/config/open.md +7 -0
- package/public/vendor/vitest-docs/config/outputfile.md +7 -0
- package/public/vendor/vitest-docs/config/passwithnotests.md +7 -0
- package/public/vendor/vitest-docs/config/pool.md +45 -0
- package/public/vendor/vitest-docs/config/printconsoletrace.md +6 -0
- package/public/vendor/vitest-docs/config/projects.md +6 -0
- package/public/vendor/vitest-docs/config/provide.md +45 -0
- package/public/vendor/vitest-docs/config/reporters.md +69 -0
- package/public/vendor/vitest-docs/config/resolvesnapshotpath.md +36 -0
- package/public/vendor/vitest-docs/config/restoremocks.md +22 -0
- package/public/vendor/vitest-docs/config/retry.md +140 -0
- package/public/vendor/vitest-docs/config/root.md +6 -0
- package/public/vendor/vitest-docs/config/runner.md +6 -0
- package/public/vendor/vitest-docs/config/sequence.md +158 -0
- package/public/vendor/vitest-docs/config/server.md +68 -0
- package/public/vendor/vitest-docs/config/setupfiles.md +40 -0
- package/public/vendor/vitest-docs/config/silent.md +9 -0
- package/public/vendor/vitest-docs/config/slowtestthreshold.md +7 -0
- package/public/vendor/vitest-docs/config/snapshotenvironment.md +27 -0
- package/public/vendor/vitest-docs/config/snapshotformat.md +28 -0
- package/public/vendor/vitest-docs/config/snapshotserializers.md +6 -0
- package/public/vendor/vitest-docs/config/stricttags.md +30 -0
- package/public/vendor/vitest-docs/config/tags.md +141 -0
- package/public/vendor/vitest-docs/config/teardowntimeout.md +7 -0
- package/public/vendor/vitest-docs/config/testnamepattern.md +21 -0
- package/public/vendor/vitest-docs/config/testtimeout.md +7 -0
- package/public/vendor/vitest-docs/config/typecheck.md +77 -0
- package/public/vendor/vitest-docs/config/ui.md +15 -0
- package/public/vendor/vitest-docs/config/unstubenvs.md +20 -0
- package/public/vendor/vitest-docs/config/unstubglobals.md +20 -0
- package/public/vendor/vitest-docs/config/update.md +16 -0
- package/public/vendor/vitest-docs/config/vmmemorylimit.md +30 -0
- package/public/vendor/vitest-docs/config/watch.md +11 -0
- package/public/vendor/vitest-docs/config/watchtriggerpatterns.md +29 -0
- package/public/vendor/vitest-docs/guide/advanced/index.md +147 -0
- package/public/vendor/vitest-docs/guide/advanced/pool.md +148 -0
- package/public/vendor/vitest-docs/guide/advanced/reporters.md +93 -0
- package/public/vendor/vitest-docs/guide/advanced/tests.md +125 -0
- package/public/vendor/vitest-docs/guide/browser/aria-snapshots.md +470 -0
- package/public/vendor/vitest-docs/guide/browser/component-testing.md +571 -0
- package/public/vendor/vitest-docs/guide/browser/index.md +630 -0
- package/public/vendor/vitest-docs/guide/browser/multiple-setups.md +121 -0
- package/public/vendor/vitest-docs/guide/browser/trace-view.md +126 -0
- package/public/vendor/vitest-docs/guide/browser/visual-regression-testing.md +734 -0
- package/public/vendor/vitest-docs/guide/cli-generated.md +972 -0
- package/public/vendor/vitest-docs/guide/cli.md +234 -0
- package/public/vendor/vitest-docs/guide/common-errors.md +163 -0
- package/public/vendor/vitest-docs/guide/coverage.md +515 -0
- package/public/vendor/vitest-docs/guide/debugging.md +127 -0
- package/public/vendor/vitest-docs/guide/environment.md +101 -0
- package/public/vendor/vitest-docs/guide/extending-matchers.md +160 -0
- package/public/vendor/vitest-docs/guide/features.md +310 -0
- package/public/vendor/vitest-docs/guide/filtering.md +175 -0
- package/public/vendor/vitest-docs/guide/ide.md +43 -0
- package/public/vendor/vitest-docs/guide/improving-performance.md +245 -0
- package/public/vendor/vitest-docs/guide/in-source.md +159 -0
- package/public/vendor/vitest-docs/guide/index.md +128 -0
- package/public/vendor/vitest-docs/guide/learn/async.md +147 -0
- package/public/vendor/vitest-docs/guide/learn/debugging-tests.md +210 -0
- package/public/vendor/vitest-docs/guide/learn/matchers.md +277 -0
- package/public/vendor/vitest-docs/guide/learn/mock-functions.md +277 -0
- package/public/vendor/vitest-docs/guide/learn/setup-teardown.md +240 -0
- package/public/vendor/vitest-docs/guide/learn/snapshots.md +166 -0
- package/public/vendor/vitest-docs/guide/learn/testing-in-practice.md +430 -0
- package/public/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +127 -0
- package/public/vendor/vitest-docs/guide/learn/writing-tests.md +231 -0
- package/public/vendor/vitest-docs/guide/lifecycle.md +379 -0
- package/public/vendor/vitest-docs/guide/migration.md +863 -0
- package/public/vendor/vitest-docs/guide/mocking/classes.md +158 -0
- package/public/vendor/vitest-docs/guide/mocking/dates.md +52 -0
- package/public/vendor/vitest-docs/guide/mocking/file-system.md +74 -0
- package/public/vendor/vitest-docs/guide/mocking/functions.md +61 -0
- package/public/vendor/vitest-docs/guide/mocking/globals.md +20 -0
- package/public/vendor/vitest-docs/guide/mocking/modules.md +414 -0
- package/public/vendor/vitest-docs/guide/mocking/requests.md +114 -0
- package/public/vendor/vitest-docs/guide/mocking/timers.md +48 -0
- package/public/vendor/vitest-docs/guide/mocking.md +239 -0
- package/public/vendor/vitest-docs/guide/open-telemetry.md +156 -0
- package/public/vendor/vitest-docs/guide/parallelism.md +82 -0
- package/public/vendor/vitest-docs/guide/profiling-test-performance.md +243 -0
- package/public/vendor/vitest-docs/guide/projects.md +291 -0
- package/public/vendor/vitest-docs/guide/recipes.md +59 -0
- package/public/vendor/vitest-docs/guide/reporters.md +723 -0
- package/public/vendor/vitest-docs/guide/snapshot.md +620 -0
- package/public/vendor/vitest-docs/guide/test-annotations.md +103 -0
- package/public/vendor/vitest-docs/guide/test-context.md +902 -0
- package/public/vendor/vitest-docs/guide/test-tags.md +314 -0
- package/public/vendor/vitest-docs/guide/testing-types.md +149 -0
- package/public/vendor/vitest-docs/guide/ui.md +160 -0
- package/public/vendor/vitest-docs/guide/using-plugins.md +5 -0
- package/public/vendor/vitest-docs/manifest.json +1691 -0
- package/resources/index.js +15 -4
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# Profiling Test Performance
|
|
2
|
+
|
|
3
|
+
When you run Vitest it reports multiple time metrics of your tests:
|
|
4
|
+
|
|
5
|
+
> ```bash
|
|
6
|
+
> RUN v2.1.1 /x/vitest/examples/profiling
|
|
7
|
+
>
|
|
8
|
+
> ✓ test/prime-number.test.ts (1) 4517ms
|
|
9
|
+
> ✓ generate prime number 4517ms
|
|
10
|
+
>
|
|
11
|
+
> Test Files 1 passed (1)
|
|
12
|
+
> Tests 1 passed (1)
|
|
13
|
+
> Start at 09:32:53
|
|
14
|
+
> Duration 4.80s (transform 44ms, setup 0ms, import 35ms, tests 4.52s, environment 0ms)
|
|
15
|
+
> # Time metrics ^^
|
|
16
|
+
> ```
|
|
17
|
+
|
|
18
|
+
- Transform: How much time was spent transforming the files. See [File Transform](#file-transform).
|
|
19
|
+
- Setup: Time spent for running the [`setupFiles`](/config/setupfiles) files.
|
|
20
|
+
- Import: Time it took to import your test files and their dependencies. This also includes the time spent collecting all tests. Note that this doesn't include dynamic imports inside of tests.
|
|
21
|
+
- Tests: Time spent for actually running the test cases.
|
|
22
|
+
- Environment: Time spent for setting up the test [`environment`](/config/environment), for example JSDOM.
|
|
23
|
+
|
|
24
|
+
## Test Runner
|
|
25
|
+
|
|
26
|
+
In cases where your test execution time is high, you can generate a profile of the test runner. See NodeJS documentation for following options:
|
|
27
|
+
|
|
28
|
+
- [`--cpu-prof`](https://nodejs.org/api/cli.html#--cpu-prof)
|
|
29
|
+
- [`--heap-prof`](https://nodejs.org/api/cli.html#--heap-prof)
|
|
30
|
+
- [`--prof`](https://nodejs.org/api/cli.html#--prof)
|
|
31
|
+
|
|
32
|
+
:::warning
|
|
33
|
+
The `--prof` option does not work with `pool: 'threads'` due to `node:worker_threads` limitations.
|
|
34
|
+
:::
|
|
35
|
+
|
|
36
|
+
To pass these options to Vitest's test runner, define `execArgv` in your Vitest configuration:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { defineConfig } from 'vitest/config'
|
|
40
|
+
|
|
41
|
+
export default defineConfig({
|
|
42
|
+
test: {
|
|
43
|
+
fileParallelism: false,
|
|
44
|
+
execArgv: [
|
|
45
|
+
'--cpu-prof',
|
|
46
|
+
'--cpu-prof-dir=test-runner-profile',
|
|
47
|
+
'--heap-prof',
|
|
48
|
+
'--heap-prof-dir=test-runner-profile'
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
After the tests have run there should be a `test-runner-profile/*.cpuprofile` and `test-runner-profile/*.heapprofile` files generated. See [Inspecting profiling records](#inspecting-profiling-records) for instructions how to analyze these files.
|
|
55
|
+
|
|
56
|
+
See [Profiling | Examples](https://github.com/vitest-dev/vitest/tree/main/examples/profiling) for example.
|
|
57
|
+
|
|
58
|
+
## Main Thread
|
|
59
|
+
|
|
60
|
+
Profiling main thread is useful for debugging Vitest's Vite usage and [`globalSetup`](/config/globalsetup) files.
|
|
61
|
+
This is also where your Vite plugins are running.
|
|
62
|
+
|
|
63
|
+
:::tip
|
|
64
|
+
See [Performance | Vite](https://vitejs.dev/guide/performance.html) for more tips about Vite specific profiling.
|
|
65
|
+
|
|
66
|
+
We recommend [`vite-plugin-inspect`](https://github.com/antfu-collective/vite-plugin-inspect) for profiling your Vite plugin performance.
|
|
67
|
+
:::
|
|
68
|
+
|
|
69
|
+
To do this you'll need to pass arguments to the Node process that runs Vitest.
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
$ node --cpu-prof --cpu-prof-dir=main-profile ./node_modules/vitest/vitest.mjs --run
|
|
73
|
+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^
|
|
74
|
+
# NodeJS arguments Vitest arguments
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
After the tests have run there should be a `main-profile/*.cpuprofile` file generated. See [Inspecting profiling records](#inspecting-profiling-records) for instructions how to analyze these files.
|
|
78
|
+
|
|
79
|
+
## File Transform
|
|
80
|
+
|
|
81
|
+
This profiling strategy is a good way to identify unnecessary transforms caused by [barrel files](https://vitejs.dev/guide/performance.html#avoid-barrel-files).
|
|
82
|
+
If these logs contain files that should not be loaded when your test is run, you might have barrel files that are importing files unnecessarily.
|
|
83
|
+
|
|
84
|
+
You can also use [Vitest UI](/guide/ui) to debug slowness caused by barrel file.
|
|
85
|
+
The example below shows how importing files without barrel file reduces amount of transformed files by ~85%.
|
|
86
|
+
|
|
87
|
+
::: code-group
|
|
88
|
+
``` [File tree]
|
|
89
|
+
├── src
|
|
90
|
+
│ └── utils
|
|
91
|
+
│ ├── currency.ts
|
|
92
|
+
│ ├── formatters.ts <-- File to test
|
|
93
|
+
│ ├── index.ts
|
|
94
|
+
│ ├── location.ts
|
|
95
|
+
│ ├── math.ts
|
|
96
|
+
│ ├── time.ts
|
|
97
|
+
│ └── users.ts
|
|
98
|
+
├── test
|
|
99
|
+
│ └── formatters.test.ts
|
|
100
|
+
└── vitest.config.ts
|
|
101
|
+
```
|
|
102
|
+
```ts [example.test.ts]
|
|
103
|
+
import { expect, test } from 'vitest'
|
|
104
|
+
import { formatter } from '../src/utils' // [!code --]
|
|
105
|
+
import { formatter } from '../src/utils/formatters' // [!code ++]
|
|
106
|
+
|
|
107
|
+
test('formatter works', () => {
|
|
108
|
+
expect(formatter).not.toThrow()
|
|
109
|
+
})
|
|
110
|
+
```
|
|
111
|
+
:::
|
|
112
|
+
|
|
113
|
+
<img src="/module-graph-barrel-file.png" alt="Vitest UI demonstrating barrel file issues" />
|
|
114
|
+
|
|
115
|
+
To see how files are transformed, you can open the "Module Info" view in the UI:
|
|
116
|
+
|
|
117
|
+
<img alt="The module info view for an inlined module" img-light src="/ui/light-module-info.png">
|
|
118
|
+
<img alt="The module info view for an inlined module" img-dark src="/ui/dark-module-info.png">
|
|
119
|
+
|
|
120
|
+
## File Import
|
|
121
|
+
|
|
122
|
+
Some modules just take a long time to load. To identify which modules are the slowest, enable [`experimental.importDurations`](/config/experimental#experimental-importdurations) in your configuration:
|
|
123
|
+
|
|
124
|
+
```ts [vitest.config.ts]
|
|
125
|
+
import { defineConfig } from 'vitest/config'
|
|
126
|
+
|
|
127
|
+
export default defineConfig({
|
|
128
|
+
test: {
|
|
129
|
+
experimental: {
|
|
130
|
+
importDurations: {
|
|
131
|
+
print: true,
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
})
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
This will print a breakdown of the slowest imports after your tests finish:
|
|
139
|
+
|
|
140
|
+
```bash
|
|
141
|
+
Import Duration Breakdown (Top 10)
|
|
142
|
+
|
|
143
|
+
Module Self Total
|
|
144
|
+
my-test.test.ts 5ms 620ms [████████████████████]
|
|
145
|
+
date-fns/index.js 500ms 500ms [████████████████░░░░] # [!code error]
|
|
146
|
+
src/utils/helpers.ts 10ms 120ms [████████░░░░░░░░░░░░]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
You can also use `--experimental.importDurations.print` from the CLI without changing your configuration:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
vitest --experimental.importDurations.print
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Once you've identified the slow modules, there are several strategies to speed up imports:
|
|
156
|
+
|
|
157
|
+
### Use Specific Entry Points
|
|
158
|
+
|
|
159
|
+
Many libraries ship multiple entry points. Importing the main entry point (which is often a [barrel file](https://vitejs.dev/guide/performance.html#avoid-barrel-files)) can pull in far more code than you need.
|
|
160
|
+
|
|
161
|
+
For example, `date-fns` re-exports hundreds of functions from its main entry point. Instead of importing from the top-level module, import directly from the specific function:
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
import { format } from 'date-fns' // [!code --]
|
|
165
|
+
import { format } from 'date-fns/format' // [!code ++]
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Use `resolve.alias` to Redirect Imports
|
|
169
|
+
|
|
170
|
+
If a dependency doesn't provide granular entry points, or if third-party code imports the heavy entry point, you can use [`resolve.alias`](https://vite.dev/config/shared-options#resolve-alias) to redirect imports to a lighter alternative:
|
|
171
|
+
|
|
172
|
+
```ts [vitest.config.ts]
|
|
173
|
+
import { defineConfig } from 'vitest/config'
|
|
174
|
+
|
|
175
|
+
export default defineConfig({
|
|
176
|
+
resolve: {
|
|
177
|
+
alias: [
|
|
178
|
+
{
|
|
179
|
+
find: /^date-fns$/,
|
|
180
|
+
replacement: join(dirname(require.resolve('date-fns/package.json')), 'index.cjs'),
|
|
181
|
+
},
|
|
182
|
+
]
|
|
183
|
+
},
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Use the Dependency Optimizer
|
|
188
|
+
|
|
189
|
+
Vitest can bundle external libraries into a single file using [`deps.optimizer`](/config/deps#deps-optimizer), which reduces the overhead of importing packages with many internal modules:
|
|
190
|
+
|
|
191
|
+
```ts [vitest.config.ts]
|
|
192
|
+
import { defineConfig } from 'vitest/config'
|
|
193
|
+
|
|
194
|
+
export default defineConfig({
|
|
195
|
+
test: {
|
|
196
|
+
deps: {
|
|
197
|
+
optimizer: {
|
|
198
|
+
ssr: {
|
|
199
|
+
enabled: true,
|
|
200
|
+
include: ['date-fns'],
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
})
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
This is especially effective for UI libraries and packages with deep import trees. Use `optimizer.ssr` for `node`/`edge` environments and `optimizer.client` for `jsdom`/`happy-dom` environments.
|
|
209
|
+
|
|
210
|
+
## Code Coverage
|
|
211
|
+
|
|
212
|
+
If code coverage generation is slow on your project you can use `DEBUG=vitest:coverage` environment variable to enable performance logging.
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
$ DEBUG=vitest:coverage vitest --run --coverage
|
|
216
|
+
|
|
217
|
+
RUN v3.1.1 /x/vitest-example
|
|
218
|
+
|
|
219
|
+
vitest:coverage Reading coverage results 2/2
|
|
220
|
+
vitest:coverage Converting 1/2
|
|
221
|
+
vitest:coverage 4 ms /x/src/multiply.ts
|
|
222
|
+
vitest:coverage Converting 2/2
|
|
223
|
+
vitest:coverage 552 ms /x/src/add.ts
|
|
224
|
+
vitest:coverage Uncovered files 1/2
|
|
225
|
+
vitest:coverage File "/x/src/large-file.ts" is taking longer than 3s # [!code error]
|
|
226
|
+
vitest:coverage 3027 ms /x/src/large-file.ts
|
|
227
|
+
vitest:coverage Uncovered files 2/2
|
|
228
|
+
vitest:coverage 4 ms /x/src/untested-file.ts
|
|
229
|
+
vitest:coverage Generate coverage total time 3521 ms
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
This profiling approach is great for detecting large files that are accidentally picked by coverage providers.
|
|
233
|
+
For example if your configuration is accidentally including large built minified Javascript files in code coverage, they should appear in logs.
|
|
234
|
+
In these cases you might want to adjust your [`coverage.include`](/config/coverage#coverage-include) and [`coverage.exclude`](/config/coverage#coverage-exclude) options.
|
|
235
|
+
|
|
236
|
+
## Inspecting Profiling Records
|
|
237
|
+
|
|
238
|
+
You can inspect the contents of `*.cpuprofile` and `*.heapprofile` with various tools. See list below for examples.
|
|
239
|
+
|
|
240
|
+
- [Speedscope](https://www.speedscope.app/)
|
|
241
|
+
- [Performance Profiling JavaScript in Visual Studio Code](https://code.visualstudio.com/docs/nodejs/profiling#_analyzing-a-profile)
|
|
242
|
+
- [Profile Node.js performance with the Performance panel | developer.chrome.com](https://developer.chrome.com/docs/devtools/performance/nodejs#analyze)
|
|
243
|
+
- [Memory panel overview | developer.chrome.com](https://developer.chrome.com/docs/devtools/memory-problems/heap-snapshots#view_snapshots)
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
# Test Projects
|
|
2
|
+
|
|
3
|
+
::: tip Sample Project
|
|
4
|
+
|
|
5
|
+
[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__/)
|
|
6
|
+
|
|
7
|
+
:::
|
|
8
|
+
|
|
9
|
+
::: warning
|
|
10
|
+
This feature is also known as a `workspace`. The `workspace` is deprecated since 3.2 and replaced with the `projects` configuration. They are functionally the same.
|
|
11
|
+
:::
|
|
12
|
+
|
|
13
|
+
Vitest provides a way to define multiple project configurations within a single Vitest process. This feature is particularly useful for monorepo setups but can also be used to run tests with different configurations, such as `resolve.alias`, `plugins`, or `test.browser` and more.
|
|
14
|
+
|
|
15
|
+
## Defining Projects
|
|
16
|
+
|
|
17
|
+
You can define projects in your root [config](/config/):
|
|
18
|
+
|
|
19
|
+
```ts [vitest.config.ts]
|
|
20
|
+
import { defineConfig } from 'vitest/config'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
test: {
|
|
24
|
+
projects: ['packages/*'],
|
|
25
|
+
},
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Project configurations are inlined configs, files, or glob patterns referencing your projects. For example, if you have a folder named `packages` that contains your projects, you can define an array in your root Vitest config:
|
|
30
|
+
|
|
31
|
+
```ts [vitest.config.ts]
|
|
32
|
+
import { defineConfig } from 'vitest/config'
|
|
33
|
+
|
|
34
|
+
export default defineConfig({
|
|
35
|
+
test: {
|
|
36
|
+
projects: ['packages/*'],
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Vitest will treat every folder in `packages` as a separate project even if it doesn't have a config file inside. If a project entry resolves to a file (either from a glob pattern or a direct file path), Vitest will validate that the name either:
|
|
42
|
+
|
|
43
|
+
- starts with `vitest.config` or `vite.config` (for example, `vitest.config.unit.ts`)
|
|
44
|
+
- or matches `vitest.<name>.config.*` / `vite.<name>.config.*`, where `<name>` can contain letters, numbers, `_`, and `-`
|
|
45
|
+
|
|
46
|
+
For example, these config files are valid:
|
|
47
|
+
|
|
48
|
+
- `vitest.config.ts`
|
|
49
|
+
- `vite.config.js`
|
|
50
|
+
- `vitest.unit.config.ts`
|
|
51
|
+
- `vitest.e2e-node.config.ts`
|
|
52
|
+
- `vite.e2e.config.js`
|
|
53
|
+
- `vitest.config.unit.js`
|
|
54
|
+
- `vite.config.e2e.js`
|
|
55
|
+
|
|
56
|
+
To exclude folders and files, you can use the negation pattern:
|
|
57
|
+
|
|
58
|
+
```ts [vitest.config.ts]
|
|
59
|
+
import { defineConfig } from 'vitest/config'
|
|
60
|
+
|
|
61
|
+
export default defineConfig({
|
|
62
|
+
test: {
|
|
63
|
+
// include all folders inside "packages" except "excluded"
|
|
64
|
+
projects: [
|
|
65
|
+
'packages/*',
|
|
66
|
+
'!packages/excluded'
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
})
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
If you have a nested structure where some folders need to be projects, but other folders have their own subfolders, you have to use brackets to avoid matching the parent folder:
|
|
73
|
+
|
|
74
|
+
```ts [vitest.config.ts]
|
|
75
|
+
import { defineConfig } from 'vitest/config'
|
|
76
|
+
|
|
77
|
+
// For example, this will create projects:
|
|
78
|
+
// packages/a
|
|
79
|
+
// packages/b
|
|
80
|
+
// packages/business/c
|
|
81
|
+
// packages/business/d
|
|
82
|
+
// Notice that "packages/business" is not a project itself
|
|
83
|
+
|
|
84
|
+
export default defineConfig({
|
|
85
|
+
test: {
|
|
86
|
+
projects: [
|
|
87
|
+
// matches every folder inside "packages" except "business"
|
|
88
|
+
'packages/!(business)',
|
|
89
|
+
// matches every folder inside "packages/business"
|
|
90
|
+
'packages/business/*',
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
})
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
::: warning
|
|
97
|
+
Vitest does not treat the root `vitest.config` file as a project unless it is explicitly specified in the configuration. Consequently, the root configuration will only influence global options such as `reporters` and `coverage`. Note that Vitest will always run certain plugin hooks, like `apply`, `config`, `configResolved` or `configureServer`, specified in the root config file. Vitest also uses the same plugins to execute global setups and custom coverage provider.
|
|
98
|
+
:::
|
|
99
|
+
|
|
100
|
+
You can also reference projects with their config files:
|
|
101
|
+
|
|
102
|
+
```ts [vitest.config.ts]
|
|
103
|
+
import { defineConfig } from 'vitest/config'
|
|
104
|
+
|
|
105
|
+
export default defineConfig({
|
|
106
|
+
test: {
|
|
107
|
+
projects: ['packages/*/vitest.config.{e2e,unit}.ts'],
|
|
108
|
+
},
|
|
109
|
+
})
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
This pattern will only include projects with a `vitest.config` file that contains `e2e` or `unit` before the extension.
|
|
113
|
+
|
|
114
|
+
You can also define projects using inline configuration. The configuration supports both syntaxes simultaneously.
|
|
115
|
+
|
|
116
|
+
```ts [vitest.config.ts]
|
|
117
|
+
import { defineConfig } from 'vitest/config'
|
|
118
|
+
|
|
119
|
+
export default defineConfig({
|
|
120
|
+
test: {
|
|
121
|
+
projects: [
|
|
122
|
+
// matches every folder and file inside the `packages` folder
|
|
123
|
+
'packages/*',
|
|
124
|
+
{
|
|
125
|
+
// add "extends: true" to inherit the options from the root config
|
|
126
|
+
extends: true,
|
|
127
|
+
test: {
|
|
128
|
+
include: ['tests/**/*.{browser}.test.{ts,js}'],
|
|
129
|
+
// it is recommended to define a name when using inline configs
|
|
130
|
+
name: 'happy-dom',
|
|
131
|
+
environment: 'happy-dom',
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
test: {
|
|
136
|
+
include: ['tests/**/*.{node}.test.{ts,js}'],
|
|
137
|
+
// color of the name label can be changed
|
|
138
|
+
name: { label: 'node', color: 'green' },
|
|
139
|
+
environment: 'node',
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
})
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
::: warning
|
|
148
|
+
All projects must have unique names; otherwise, Vitest will throw an error. If a name is not provided in the inline configuration, Vitest will assign a number. For project configurations defined with glob syntax, Vitest will default to using the "name" property in the nearest `package.json` file or, if none exists, the folder name.
|
|
149
|
+
:::
|
|
150
|
+
|
|
151
|
+
Projects do not support all configuration properties. For better type safety, use the `defineProject` method instead of `defineConfig` within project configuration files:
|
|
152
|
+
|
|
153
|
+
```ts twoslash [packages/a/vitest.config.ts]
|
|
154
|
+
// @errors: 2769
|
|
155
|
+
import { defineProject } from 'vitest/config'
|
|
156
|
+
|
|
157
|
+
export default defineProject({
|
|
158
|
+
test: {
|
|
159
|
+
environment: 'jsdom',
|
|
160
|
+
// "reporters" is not supported in a project config,
|
|
161
|
+
// so it will show an error
|
|
162
|
+
reporters: ['json']
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Running Tests
|
|
168
|
+
|
|
169
|
+
To run tests, define a script in your root `package.json`:
|
|
170
|
+
|
|
171
|
+
```json [package.json]
|
|
172
|
+
{
|
|
173
|
+
"scripts": {
|
|
174
|
+
"test": "vitest"
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Now tests can be run using your package manager:
|
|
180
|
+
|
|
181
|
+
::: code-group
|
|
182
|
+
```bash [npm]
|
|
183
|
+
npm run test
|
|
184
|
+
```
|
|
185
|
+
```bash [yarn]
|
|
186
|
+
yarn test
|
|
187
|
+
```
|
|
188
|
+
```bash [pnpm]
|
|
189
|
+
pnpm run test
|
|
190
|
+
```
|
|
191
|
+
```bash [bun]
|
|
192
|
+
bun run test
|
|
193
|
+
```
|
|
194
|
+
:::
|
|
195
|
+
|
|
196
|
+
If you need to run tests only inside a single project, use the `--project` CLI option:
|
|
197
|
+
|
|
198
|
+
::: code-group
|
|
199
|
+
```bash [npm]
|
|
200
|
+
npm run test --project e2e
|
|
201
|
+
```
|
|
202
|
+
```bash [yarn]
|
|
203
|
+
yarn test --project e2e
|
|
204
|
+
```
|
|
205
|
+
```bash [pnpm]
|
|
206
|
+
pnpm run test --project e2e
|
|
207
|
+
```
|
|
208
|
+
```bash [bun]
|
|
209
|
+
bun run test --project e2e
|
|
210
|
+
```
|
|
211
|
+
:::
|
|
212
|
+
|
|
213
|
+
::: tip
|
|
214
|
+
CLI option `--project` can be used multiple times to filter out several projects:
|
|
215
|
+
|
|
216
|
+
::: code-group
|
|
217
|
+
```bash [npm]
|
|
218
|
+
npm run test --project e2e --project unit
|
|
219
|
+
```
|
|
220
|
+
```bash [yarn]
|
|
221
|
+
yarn test --project e2e --project unit
|
|
222
|
+
```
|
|
223
|
+
```bash [pnpm]
|
|
224
|
+
pnpm run test --project e2e --project unit
|
|
225
|
+
```
|
|
226
|
+
```bash [bun]
|
|
227
|
+
bun run test --project e2e --project unit
|
|
228
|
+
```
|
|
229
|
+
:::
|
|
230
|
+
|
|
231
|
+
## Configuration
|
|
232
|
+
|
|
233
|
+
None of the configuration options are inherited from the root-level config file. You can create a shared config file and merge it with the project config yourself:
|
|
234
|
+
|
|
235
|
+
```ts [packages/a/vitest.config.ts]
|
|
236
|
+
import { defineProject, mergeConfig } from 'vitest/config'
|
|
237
|
+
import configShared from '../vitest.shared.js'
|
|
238
|
+
|
|
239
|
+
export default mergeConfig(
|
|
240
|
+
configShared,
|
|
241
|
+
defineProject({
|
|
242
|
+
test: {
|
|
243
|
+
environment: 'jsdom',
|
|
244
|
+
}
|
|
245
|
+
})
|
|
246
|
+
)
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
Additionally, you can use the `extends` option to inherit from your root-level configuration. All options will be merged.
|
|
250
|
+
|
|
251
|
+
```ts [vitest.config.ts]
|
|
252
|
+
import { defineConfig } from 'vitest/config'
|
|
253
|
+
import react from '@vitejs/plugin-react'
|
|
254
|
+
|
|
255
|
+
export default defineConfig({
|
|
256
|
+
plugins: [react()],
|
|
257
|
+
test: {
|
|
258
|
+
pool: 'threads',
|
|
259
|
+
projects: [
|
|
260
|
+
{
|
|
261
|
+
// will inherit options from this config like plugins and pool
|
|
262
|
+
extends: true,
|
|
263
|
+
test: {
|
|
264
|
+
name: 'unit',
|
|
265
|
+
include: ['**/*.unit.test.ts'],
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
// won't inherit any options from this config
|
|
270
|
+
// this is the default behaviour
|
|
271
|
+
extends: false,
|
|
272
|
+
test: {
|
|
273
|
+
name: 'integration',
|
|
274
|
+
include: ['**/*.integration.test.ts'],
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
},
|
|
279
|
+
})
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
::: danger Unsupported Options
|
|
283
|
+
Some of the configuration options are not allowed in a project config. Most notably:
|
|
284
|
+
|
|
285
|
+
- `coverage`: coverage is done for the whole process
|
|
286
|
+
- `reporters`: only root-level reporters can be supported
|
|
287
|
+
- `resolveSnapshotPath`: only root-level resolver is respected
|
|
288
|
+
- all other options that don't affect test runners
|
|
289
|
+
|
|
290
|
+
All configuration options that are not supported inside a project configuration are marked with a <CRoot /> icon next to their name. They can only be defined once in the root config file.
|
|
291
|
+
:::
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Recipes
|
|
2
|
+
|
|
3
|
+
## Disabling Isolation for Specific Test Files Only
|
|
4
|
+
|
|
5
|
+
You can speed up your test run by disabling isolation for specific set of files by specifying `isolate` per `projects` entries:
|
|
6
|
+
|
|
7
|
+
```ts [vitest.config.ts]
|
|
8
|
+
import { defineConfig } from 'vitest/config'
|
|
9
|
+
|
|
10
|
+
export default defineConfig({
|
|
11
|
+
test: {
|
|
12
|
+
projects: [
|
|
13
|
+
{
|
|
14
|
+
test: {
|
|
15
|
+
// Non-isolated unit tests
|
|
16
|
+
name: 'Unit tests',
|
|
17
|
+
isolate: false,
|
|
18
|
+
exclude: ['**.integration.test.ts'],
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
test: {
|
|
23
|
+
// Isolated integration tests
|
|
24
|
+
name: 'Integration tests',
|
|
25
|
+
include: ['**.integration.test.ts'],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
],
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Parallel and Sequential Test Files
|
|
34
|
+
|
|
35
|
+
You can split test files into parallel and sequential groups by using `projects` option:
|
|
36
|
+
|
|
37
|
+
```ts [vitest.config.ts]
|
|
38
|
+
import { defineConfig } from 'vitest/config'
|
|
39
|
+
|
|
40
|
+
export default defineConfig({
|
|
41
|
+
test: {
|
|
42
|
+
projects: [
|
|
43
|
+
{
|
|
44
|
+
test: {
|
|
45
|
+
name: 'Parallel',
|
|
46
|
+
exclude: ['**.sequential.test.ts'],
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
test: {
|
|
51
|
+
name: 'Sequential',
|
|
52
|
+
include: ['**.sequential.test.ts'],
|
|
53
|
+
fileParallelism: false,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
```
|