diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d020f7cdc12d5..527e937e48522 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,8 +85,8 @@ jobs: - name: Test run: pnpm run test:ci - - name: Test No Threads - run: pnpm run test:ci:no-threads + - name: Test Threads + run: pnpm run test:ci:threads - name: Test Vm Threads run: pnpm run test:ci:vm-threads diff --git a/docs/config/index.md b/docs/config/index.md index c0d73e06e1aec..c60f9506a85e0 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -552,7 +552,7 @@ By providing an object instead of a string you can define individual outputs whe ### pool 1.0.0+ {#pool} - **Type:** `'threads' | 'forks' | 'vmThreads' | 'vmForks'` -- **Default:** `'threads'` +- **Default:** `'forks'` (in v1 `'threads'`) - **CLI:** `--pool=threads` Pool used to run tests in. diff --git a/docs/guide/common-errors.md b/docs/guide/common-errors.md index 5831dbae0674c..b0b5082d7f588 100644 --- a/docs/guide/common-errors.md +++ b/docs/guide/common-errors.md @@ -65,9 +65,8 @@ This error can happen when NodeJS's `fetch` is used with default [`pool: 'thread As work-around you can switch to [`pool: 'forks'`](/config/#forks) or [`pool: 'vmForks'`](/config/#vmforks). -Specify `pool` in your configuration file: - -```ts +::: code-group +```ts [vitest.config.js] import { defineConfig } from 'vitest/config' export default defineConfig({ @@ -76,12 +75,33 @@ export default defineConfig({ }, }) ``` +```bash [CLI] +vitest --pool=forks +``` +::: -Or in your `package.json` scripts: +## Segfaults and native code errors -```diff -scripts: { -- "test": "vitest" -+ "test": "vitest --pool=forks" -} +Running [native NodeJS modules](https://nodejs.org/api/addons.html) in `pool: 'threads'` can run into cryptic errors coming from the native code. + +- `Segmentation fault (core dumped)` +- `thread '' panicked at 'assertion failed` +- `Abort trap: 6` +- `internal error: entered unreachable code` + +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`. + +::: code-group +```ts [vitest.config.js] +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + pool: 'forks', + }, +}) +``` +```bash [CLI] +vitest --pool=forks ``` +::: diff --git a/docs/guide/improving-performance.md b/docs/guide/improving-performance.md index 1ef6066ad4f06..71c6a0edba3e5 100644 --- a/docs/guide/improving-performance.md +++ b/docs/guide/improving-performance.md @@ -1,5 +1,7 @@ # Improving Performance +## Test isolation + By default Vitest runs every test file in an isolated environment based on the [pool](/config/#pool): - `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({ }) ``` ::: + +## Pool + +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. + +You can try to improve test run time by switching `pool` option in configuration: + +::: code-group +```bash [CLI] +vitest --pool=threads +``` +```ts [vitest.config.js] +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + test: { + pool: 'threads', + }, +}) +``` +::: diff --git a/package.json b/package.json index a2f527c5c5ab7..5f1c7d66cae51 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "test:all": "CI=true pnpm -r --stream run test --allowOnly", "test:ci": "CI=true pnpm -r --reporter-hide-prefix --stream --filter !test-browser --filter !test-esm --filter !test-browser run test --allowOnly", "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", - "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", + "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", "typecheck": "tsc -p tsconfig.check.json --noEmit", "typecheck:why": "tsc -p tsconfig.check.json --noEmit --explainFiles > explainTypes.txt", "ui:build": "vite build packages/ui", diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts index 44d21c4a350f7..a63162c45391b 100644 --- a/packages/vitest/src/defaults.ts +++ b/packages/vitest/src/defaults.ts @@ -66,7 +66,7 @@ const config = { watch: !isCI, globals: false, environment: 'node' as const, - pool: 'threads' as const, + pool: 'forks' as const, clearMocks: false, restoreMocks: false, mockReset: false, diff --git a/test/config/test/chai-config.test.ts b/test/config/test/chai-config.test.ts index 9d99ba26eb5b2..08c4f471d2555 100644 --- a/test/config/test/chai-config.test.ts +++ b/test/config/test/chai-config.test.ts @@ -18,8 +18,7 @@ describe('truncateThreshold', () => { ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ] ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 } ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 } - ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) } - " + ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }" `) expect(result.exitCode).toBe(0) }) @@ -43,8 +42,7 @@ describe('truncateThreshold', () => { ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', …(1) ] ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 } ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 } - ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) } - " + ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, …(2) }" `) expect(result.exitCode).toBe(0) }) @@ -68,8 +66,7 @@ describe('truncateThreshold', () => { ok 6 - test-each-title.test.ts > [ 'one', 'two', 'three', 'four', 'five' ] ok 7 - test-each-title.test.ts > { one: 1, two: 2, three: 3 } ok 8 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4 } - ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4, five: 5 } - " + ok 9 - test-each-title.test.ts > { one: 1, two: 2, three: 3, four: 4, five: 5 }" `) expect(result.exitCode).toBe(0) }) @@ -77,5 +74,5 @@ describe('truncateThreshold', () => { function cleanOutput(output: string) { // remove non-deterministic output - return output.replaceAll(/\s*# time=.*/g, '') + return output.replaceAll(/\s*# time=.*/g, '').trim() } diff --git a/test/config/test/failures.test.ts b/test/config/test/failures.test.ts index cc65dd796e804..ffbfc171c14de 100644 --- a/test/config/test/failures.test.ts +++ b/test/config/test/failures.test.ts @@ -143,7 +143,6 @@ test('boolean flag 100 should not crash CLI', async () => { test('nextTick cannot be mocked inside child_process', async () => { const { stderr } = await runVitest({ - pool: 'forks', fakeTimers: { toFake: ['nextTick'] }, include: ['./fixtures/test/fake-timers.test.ts'], }) @@ -153,6 +152,7 @@ test('nextTick cannot be mocked inside child_process', async () => { test('nextTick can be mocked inside worker_threads', async () => { const { stderr } = await runVitest({ + pool: 'threads', fakeTimers: { toFake: ['nextTick'] }, include: ['./fixtures/test/fake-timers.test.ts'], }) diff --git a/test/test-utils/index.ts b/test/test-utils/index.ts index 0ee2a080cdc6b..aca7ce8d3ed21 100644 --- a/test/test-utils/index.ts +++ b/test/test-utils/index.ts @@ -75,6 +75,7 @@ function captureLogs() { const originalStdoutWrite = process.stdout.write process.stdout.write = streams.stdout.write.bind(streams.stdout) as any + process.stdout.isTTY = false const originalStderrWrite = process.stderr.write process.stderr.write = streams.stderr.write.bind(streams.stderr) as any diff --git a/test/watch/vitest.config.ts b/test/watch/vitest.config.ts index 1178d41581c92..39c3d43d254e5 100644 --- a/test/watch/vitest.config.ts +++ b/test/watch/vitest.config.ts @@ -13,11 +13,13 @@ export default defineConfig({ // Test cases may have side effects, e.g. files under fixtures/ are modified on the fly to trigger file watchers poolOptions: { + forks: { singleFork: true }, threads: { singleThread: true }, vmThreads: { singleThread: true }, }, // TODO: Fix flakiness and remove allowOnly: true, + bail: 1, }, })