Skip to content

Commit 27ea604

Browse files
docs: add "Parallelism" guide (#7975)
Co-authored-by: Ari Perkkiö <[email protected]>
1 parent 623ce92 commit 27ea604

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

docs/.vitepress/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ function guide(): DefaultTheme.SidebarItem[] {
496496
text: 'Mocking',
497497
link: '/guide/mocking',
498498
},
499+
{
500+
text: 'Parallelism',
501+
link: '/guide/parallelism',
502+
},
499503
{
500504
text: 'Testing Types',
501505
link: '/guide/testing-types',

docs/guide/features.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Out-of-the-box ES Module / TypeScript / JSX support / PostCSS
3434

3535
## Threads
3636

37-
By default Vitest runs test files in multiple processes using [`node:child_process`](https://nodejs.org/api/child_process.html) via [Tinypool](https://github.com/tinylibs/tinypool) (a lightweight fork of [Piscina](https://github.com/piscinajs/piscina)), 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).
37+
By default Vitest runs test files in [multiple processes](/guide/parallelism) using [`node:child_process`](https://nodejs.org/api/child_process.html) via [Tinypool](https://github.com/tinylibs/tinypool) (a lightweight fork of [Piscina](https://github.com/piscinajs/piscina)), 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).
3838

3939
To run tests in a single thread or process, see [`poolOptions`](/config/#pooloptions).
4040

docs/guide/parallelism.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: Parallelism | Guide
3+
outline: deep
4+
---
5+
6+
# Parallelism
7+
8+
## File Parallelism
9+
10+
By default, Vitest runs _test files_ in parallel. Depending on the specified `pool`, Vitest uses a different mechanism to parallelize test files:
11+
12+
- `forks` (the default) and `vmForks` run tests in different [child processes](https://nodejs.org/api/child_process.html)
13+
- `threads` and `vmThreads` run tests in different [worker threads](https://nodejs.org/api/worker_threads.html)
14+
15+
Both "child processes" and "worker threads" are refered to as "workers". You can configure the number of running workers with [`minWorkers`](/config/#minworkers) and [`maxWorkers`](/config/#maxworkers) options. Or more granually with [`poolOptions`](/config/#pooloptions) configuration.
16+
17+
If you have a lot of tests, it is usually faster to run them in parallel, but it also depends on the project, the environment and [isolation](/config/#isolate) state. To disable file parallelisation, you can set [`fileParallelism`](/config/#fileparallelism) to `false`. To learn more about possible performance improvements, read the [Performance Guide](/guide/improving-performance).
18+
19+
## Test Parallelism
20+
21+
Unlike _test files_, Vitest runs _tests_ in sequence. This means that tests inside a single test file will run in the order they are defined.
22+
23+
Vitest supports the [`concurrent`](/api/#test-concurrent) option to run tests together. If this option is set, Vitest will group concurrent tests in the same _file_ (the number of simultaneously running tests depends on the [`maxConcurrency`](/config/#maxconcurrency) option) and run them with [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all).
24+
25+
Vitest doesn't perform any smart analysis and doesn't create additional workers to run these tests. This means that the performance of your tests will improve only if you rely heavily on asynchronous operations. For example, these tests will still run one after another even though the `concurrent` option is specified. This is because they are synchronous:
26+
27+
```ts
28+
test.concurrent('the first test', () => {
29+
expect(1).toBe(1)
30+
})
31+
32+
test.concurrent('the second test', () => {
33+
expect(2).toBe(2)
34+
})
35+
```
36+
37+
If you wish to run all tests concurrently, you can set the [`sequence.concurrent`](/config/#sequence-concurrent) option to `true`.

0 commit comments

Comments
 (0)