You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/advanced/vitest.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -349,7 +349,7 @@ This makes this method very slow, unless you disable isolation before collecting
349
349
function cancelCurrentRun(reason:CancelReason):Promise<void>
350
350
```
351
351
352
-
This method will gracefully cancel all ongoing tests. It will wait for started tests to finish running and will not run tests that were scheduled to run but haven't started yet.
352
+
This method will gracefully cancel all ongoing tests. It will stop the on-going tests and will not run tests that were scheduled to run but haven't started yet.
Copy file name to clipboardExpand all lines: docs/guide/improving-performance.md
+14Lines changed: 14 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,20 @@ export default defineConfig({
77
77
78
78
You can limit the working directory when Vitest searches for files using [`test.dir`](/config/dir) option. This should make the search faster if you have unrelated folders and files in the root directory.
79
79
80
+
## Caching Between Reruns
81
+
82
+
In watch mode, Vitest caches all transformed files in memory, which makes reruns fast. However, this cache is discarded once the test run finishes. By enabling [`experimental.fsModuleCache`](/config/experimental#experimental-fsmodulecache), Vitest persists this cache to the file system so it can be reused across reruns.
83
+
84
+
This improvement is most noticeable when rerunning a small number of tests that depend on a large module graph. For full test suites, parallelization already mitigates the cost because other tests populate the in-memory cache while earlier tests are still running. For example, running one test file with a huge module graph (>900 modules):
By default Vitest runs tests in `pool: 'forks'`. While `'forks'` pool is better for compatibility issues ([hanging process](/guide/common-errors.html#failed-to-terminate-worker) and [segfaults](/guide/common-errors.html#segfaults-and-native-code-errors)), it may be slightly slower than `pool: 'threads'` in larger projects.
To see how files are transformed, you can use `VITEST_DEBUG_DUMP` environment variable to write transformed files in the file system:
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:
You can also use `--experimental.importDurations.print` from the CLI without changing your configuration:
116
150
117
151
```bash
118
-
$ VITEST_DEBUG_DUMP=true vitest --run
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.
119
160
120
-
RUN v2.1.1 /x/vitest/examples/profiling
121
-
...
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:
import { format } from 'date-fns/format' // [!code ++]
127
166
```
128
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:
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
+
129
210
## Code Coverage
130
211
131
212
If code coverage generation is slow on your project you can use `DEBUG=vitest:coverage` environment variable to enable performance logging.
0 commit comments