Skip to content

Commit c8a4e21

Browse files
committed
fix!: change default pool to 'forks'
1 parent ffec1dc commit c8a4e21

File tree

10 files changed

+65
-26
lines changed

10 files changed

+65
-26
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ jobs:
8585
- name: Test
8686
run: pnpm run test:ci
8787

88-
- name: Test No Threads
89-
run: pnpm run test:ci:no-threads
88+
- name: Test Threads
89+
run: pnpm run test:ci:threads
9090

9191
- name: Test Vm Threads
9292
run: pnpm run test:ci:vm-threads

docs/config/index.md

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

554554
- **Type:** `'threads' | 'forks' | 'vmThreads' | 'vmForks'`
555-
- **Default:** `'threads'`
555+
- **Default:** `'forks'` (in v1 `'threads'`)
556556
- **CLI:** `--pool=threads`
557557

558558
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+
:::

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"test:all": "CI=true pnpm -r --stream run test --allowOnly",
2929
"test:ci": "CI=true pnpm -r --reporter-hide-prefix --stream --filter !test-browser --filter !test-esm --filter !test-browser run test --allowOnly",
3030
"test:ci:vm-threads": "CI=true pnpm -r --reporter-hide-prefix --stream --filter !test-coverage --filter !test-single-thread --filter !test-browser --filter !test-esm --filter !test-network-imports --filter !test-browser --filter !example-react-testing-lib-msw run test --allowOnly --pool vmThreads",
31-
"test:ci:no-threads": "CI=true pnpm -r --reporter-hide-prefix --stream --filter !test-vm-threads --filter !test-coverage --filter !test-watch --filter !test-bail --filter !test-esm --filter !test-browser run test --allowOnly --pool forks",
31+
"test:ci:threads": "CI=true pnpm -r --reporter-hide-prefix --stream --filter !test-vm-threads --filter !test-coverage --filter !test-watch --filter !test-bail --filter !test-esm --filter !test-browser run test --allowOnly --pool threads",
3232
"typecheck": "tsc -p tsconfig.check.json --noEmit",
3333
"typecheck:why": "tsc -p tsconfig.check.json --noEmit --explainFiles > explainTypes.txt",
3434
"ui:build": "vite build packages/ui",

packages/vitest/src/defaults.ts

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

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
@@ -143,7 +143,6 @@ test('boolean flag 100 should not crash CLI', async () => {
143143

144144
test('nextTick cannot be mocked inside child_process', async () => {
145145
const { stderr } = await runVitest({
146-
pool: 'forks',
147146
fakeTimers: { toFake: ['nextTick'] },
148147
include: ['./fixtures/test/fake-timers.test.ts'],
149148
})
@@ -153,6 +152,7 @@ test('nextTick cannot be mocked inside child_process', async () => {
153152

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

test/test-utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function captureLogs() {
7575

7676
const originalStdoutWrite = process.stdout.write
7777
process.stdout.write = streams.stdout.write.bind(streams.stdout) as any
78+
process.stdout.isTTY = false
7879

7980
const originalStderrWrite = process.stderr.write
8081
process.stderr.write = streams.stderr.write.bind(streams.stderr) as any

test/watch/vitest.config.ts

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

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

2017
// TODO: Fix flakiness and remove
2118
allowOnly: true,
19+
bail: 1,
2220
},
2321
})

0 commit comments

Comments
 (0)