Skip to content

Commit a6d4fc6

Browse files
committed
fix!: change default pool to 'forks'
1 parent 5fd8147 commit a6d4fc6

File tree

8 files changed

+63
-26
lines changed

8 files changed

+63
-26
lines changed

docs/config/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ By providing an object instead of a string you can define individual outputs whe
586586
### pool<NonProjectOption /> <Badge type="info">1.0.0+</Badge> {#pool}
587587

588588
- **Type:** `'threads' | 'forks' | 'vmThreads' | 'vmForks'`
589-
- **Default:** `'threads'`
589+
- **Default:** `'forks'` (in v1 `'threads'`)
590590
- **CLI:** `--pool=threads`
591591

592592
Pool used to run tests in.

docs/guide/common-errors.md

+29-9
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ This error can happen when NodeJS's `fetch` is used with default [`pool: 'thread
6565

6666
As work-around you can switch to [`pool: 'forks'`](/config/#forks) or [`pool: 'vmForks'`](/config/#vmforks).
6767

68-
Specify `pool` in your configuration file:
69-
70-
```ts
68+
::: code-group
69+
```ts [vitest.config.js]
7170
import { defineConfig } from 'vitest/config'
7271

7372
export default defineConfig({
@@ -76,12 +75,33 @@ export default defineConfig({
7675
},
7776
})
7877
```
78+
```bash [CLI]
79+
vitest --pool=forks
80+
```
81+
:::
7982

80-
Or in your `package.json` scripts:
83+
## Segfaults and native code errors
8184

82-
```diff
83-
scripts: {
84-
- "test": "vitest"
85-
+ "test": "vitest --pool=forks"
86-
}
85+
Running [native NodeJS modules](https://nodejs.org/api/addons.html) in `pool: 'threads'` can run into cryptic errors coming from the native code.
86+
87+
- `Segmentation fault (core dumped)`
88+
- `thread '<unnamed>' panicked at 'assertion failed`
89+
- `Abort trap: 6`
90+
- `internal error: entered unreachable code`
91+
92+
In these cases the native module is likely not built to be multi-thread safe. As work-around, you can switch to `pool: 'forks'` which runs the test cases in multiple `node:child_process` instead of multiple `node:worker_threads`.
93+
94+
::: code-group
95+
```ts [vitest.config.js]
96+
import { defineConfig } from 'vitest/config'
97+
98+
export default defineConfig({
99+
test: {
100+
pool: 'forks',
101+
},
102+
})
103+
```
104+
```bash [CLI]
105+
vitest --pool=forks
87106
```
107+
:::

docs/guide/improving-performance.md

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Improving Performance
22

3+
## Test isolation
4+
35
By default Vitest runs every test file in an isolated environment based on the [pool](/config/#pool):
46

57
- `threads` pool runs every test file in a separate [`Worker`](https://nodejs.org/api/worker_threads.html#class-worker)
@@ -49,3 +51,24 @@ export default defineConfig({
4951
})
5052
```
5153
:::
54+
55+
## Pool
56+
57+
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.
58+
59+
You can try to improve test run time by switching `pool` option in configuration:
60+
61+
::: code-group
62+
```bash [CLI]
63+
vitest --pool=threads
64+
```
65+
```ts [vitest.config.js]
66+
import { defineConfig } from 'vitest/config'
67+
68+
export default defineConfig({
69+
test: {
70+
pool: 'threads',
71+
},
72+
})
73+
```
74+
:::

packages/vitest/src/defaults.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const config = {
6767
watch: !isCI,
6868
globals: false,
6969
environment: 'node' as const,
70-
pool: 'threads' as const,
70+
pool: 'forks' as const,
7171
clearMocks: false,
7272
restoreMocks: false,
7373
mockReset: false,

test/browser/vitest.config.unit.mts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ export default defineConfig({
44
test: {
55
include: ['specs/**/*.{spec,test}.ts'],
66
poolOptions: {
7-
threads: {
8-
singleThread: true,
7+
forks: {
8+
singleFork: true,
99
},
1010
},
1111
hookTimeout: process.env.CI ? 120_000 : 10_000,

test/config/test/chai-config.test.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ describe('truncateThreshold', () => {
1818
ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ]
1919
ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 }
2020
ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 }
21-
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }
22-
"
21+
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }"
2322
`)
2423
expect(result.exitCode).toBe(0)
2524
})
@@ -43,8 +42,7 @@ describe('truncateThreshold', () => {
4342
ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ]
4443
ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 }
4544
ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 }
46-
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }
47-
"
45+
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }"
4846
`)
4947
expect(result.exitCode).toBe(0)
5048
})
@@ -68,14 +66,13 @@ describe('truncateThreshold', () => {
6866
ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', 'five' ]
6967
ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 }
7068
ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 }
71-
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4, five: 5 }
72-
"
69+
ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4, five: 5 }"
7370
`)
7471
expect(result.exitCode).toBe(0)
7572
})
7673
})
7774

7875
function cleanOutput(output: string) {
7976
// remove non-deterministic output
80-
return output.replaceAll(/\s*# time=.*/g, '')
77+
return output.replaceAll(/\s*# time=.*/g, '').trim()
8178
}

test/config/test/failures.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ test('boolean flag 100 should not crash CLI', async () => {
144144

145145
test('nextTick cannot be mocked inside child_process', async () => {
146146
const { stderr } = await runVitest({
147-
pool: 'forks',
148147
fakeTimers: { toFake: ['nextTick'] },
149148
include: ['./fake-timers.test.ts'],
150149
})
@@ -154,6 +153,7 @@ test('nextTick cannot be mocked inside child_process', async () => {
154153

155154
test('nextTick can be mocked inside worker_threads', async () => {
156155
const { stderr } = await runVitest({
156+
pool: 'threads',
157157
fakeTimers: { toFake: ['nextTick'] },
158158
include: ['./fixtures/test/fake-timers.test.ts'],
159159
})

test/watch/vitest.config.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ export default defineConfig({
1313
testTimeout: process.env.CI ? 60_000 : 10_000,
1414

1515
// Test cases may have side effects, e.g. files under fixtures/ are modified on the fly to trigger file watchers
16-
poolOptions: {
17-
forks: { singleFork: true },
18-
threads: { singleThread: true },
19-
vmThreads: { singleThread: true },
20-
},
16+
fileParallelism: false,
2117

2218
// TODO: Fix flakiness and remove
2319
allowOnly: true,
20+
bail: 1,
2421
},
2522
})

0 commit comments

Comments
 (0)