Skip to content

Commit e9fe418

Browse files
authored
fix(workspace): support overring pool and poolOptions on project level (#4765)
1 parent 508fced commit e9fe418

File tree

11 files changed

+212
-13
lines changed

11 files changed

+212
-13
lines changed

docs/config/index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ Please, be aware of these issues when using this option. Vitest team cannot fix
655655
- **Type:** `Record<'threads' | 'forks' | 'vmThreads', {}>`
656656
- **Default:** `{}`
657657

658-
#### poolOptions.threads<NonProjectOption />
658+
#### poolOptions.threads
659659

660660
Options for `threads` pool.
661661

@@ -687,7 +687,7 @@ Maximum number of threads. You can also use `VITEST_MAX_THREADS` environment var
687687

688688
Minimum number of threads. You can also use `VITEST_MIN_THREADS` environment variable.
689689

690-
##### poolOptions.threads.singleThread<NonProjectOption />
690+
##### poolOptions.threads.singleThread
691691

692692
- **Type:** `boolean`
693693
- **Default:** `false`
@@ -710,7 +710,7 @@ Use Atomics to synchronize threads.
710710

711711
This can improve performance in some cases, but might cause segfault in older Node versions.
712712

713-
##### poolOptions.threads.isolate<NonProjectOption />
713+
##### poolOptions.threads.isolate
714714

715715
- **Type:** `boolean`
716716
- **Default:** `true`
@@ -728,7 +728,7 @@ Pass additional arguments to `node` in the threads. See [Command-line API | Node
728728
Be careful when using, it as some options may crash worker, e.g. --prof, --title. See https://github.com/nodejs/node/issues/41103.
729729
:::
730730

731-
#### poolOptions.forks<NonProjectOption />
731+
#### poolOptions.forks
732732

733733
Options for `forks` pool.
734734

@@ -760,14 +760,14 @@ Maximum number of forks.
760760

761761
Minimum number of forks.
762762

763-
##### poolOptions.forks.isolate<NonProjectOption />
763+
##### poolOptions.forks.isolate
764764

765765
- **Type:** `boolean`
766766
- **Default:** `true`
767767

768768
Isolate environment for each test file.
769769

770-
##### poolOptions.forks.singleFork<NonProjectOption />
770+
##### poolOptions.forks.singleFork
771771

772772
- **Type:** `boolean`
773773
- **Default:** `false`
@@ -792,7 +792,7 @@ Pass additional arguments to `node` process in the child processes. See [Command
792792
Be careful when using, it as some options may crash worker, e.g. --prof, --title. See https://github.com/nodejs/node/issues/41103.
793793
:::
794794

795-
#### poolOptions.vmThreads<NonProjectOption />
795+
#### poolOptions.vmThreads
796796

797797
Options for `vmThreads` pool.
798798

packages/vitest/src/node/workspace.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,28 @@ export class WorkspaceProject {
319319

320320
getSerializableConfig() {
321321
const optimizer = this.config.deps?.optimizer
322+
const poolOptions = this.config.poolOptions
323+
324+
// Resolve from server.config to avoid comparing against default value
325+
const isolate = this.server?.config?.test?.isolate
326+
322327
return deepMerge({
323328
...this.config,
324329
coverage: this.ctx.config.coverage,
325330

326-
pool: this.ctx.config.pool,
327-
poolOptions: this.ctx.config.poolOptions,
331+
poolOptions: {
332+
forks: {
333+
singleFork: poolOptions?.forks?.singleFork ?? this.ctx.config.poolOptions?.forks?.singleFork ?? false,
334+
isolate: poolOptions?.forks?.isolate ?? isolate ?? this.ctx.config.poolOptions?.forks?.isolate ?? true,
335+
},
336+
threads: {
337+
singleThread: poolOptions?.threads?.singleThread ?? this.ctx.config.poolOptions?.threads?.singleThread ?? false,
338+
isolate: poolOptions?.threads?.isolate ?? isolate ?? this.ctx.config.poolOptions?.threads?.isolate ?? true,
339+
},
340+
vmThreads: {
341+
singleThread: poolOptions?.vmThreads?.singleThread ?? this.ctx.config.poolOptions?.vmThreads?.singleThread ?? false,
342+
},
343+
},
328344

329345
reporters: [],
330346
deps: {

packages/vitest/src/types/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,6 @@ export type ProjectConfig = Omit<
817817
| 'update'
818818
| 'reporters'
819819
| 'outputFile'
820-
| 'pool'
821820
| 'poolOptions'
822821
| 'teardownTimeout'
823822
| 'silent'
@@ -842,6 +841,11 @@ export type ProjectConfig = Omit<
842841
> & {
843842
sequencer?: Omit<SequenceOptions, 'sequencer' | 'seed'>
844843
deps?: Omit<DepsOptions, 'moduleDirectories'>
844+
poolOptions?: {
845+
threads?: Pick<NonNullable<PoolOptions['threads']>, 'singleThread' | 'isolate'>
846+
vmThreads?: Pick<NonNullable<PoolOptions['vmThreads']>, 'singleThread'>
847+
forks?: Pick<NonNullable<PoolOptions['forks']>, 'singleFork' | 'isolate'>
848+
}
845849
}
846850

847851
export type RuntimeConfig = Pick<

test/config/test/config-types.test-d.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ describe('define project helper', () => {
99
expectProjectTestConfig.toHaveProperty('name')
1010
expectMainTestConfig.toHaveProperty('name')
1111

12-
expectProjectTestConfig.not.toHaveProperty('pool')
13-
expectMainTestConfig.toHaveProperty('pool')
14-
1512
expectProjectTestConfig.not.toHaveProperty('coverage')
1613
expectMainTestConfig.toHaveProperty('coverage')
14+
15+
expectProjectTestConfig.not.toHaveProperty('reporters')
16+
expectMainTestConfig.toHaveProperty('reporters')
17+
})
18+
19+
test('allows expected project fields on a project config', () => {
20+
expectProjectTestConfig.toHaveProperty('pool')
21+
expectProjectTestConfig.toHaveProperty('poolOptions')
1722
})
1823
})
1924

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { isMainThread } from 'node:worker_threads'
2+
import { expect, test } from 'vitest'
3+
4+
test('is run in "node:child_process"', () => {
5+
expect(isChildProcess()).toBe(true)
6+
expect(isMainThread).toBe(true)
7+
})
8+
9+
// TODO: Use from "src/utils/base.ts" after #4441
10+
function isChildProcess(): boolean {
11+
return !!process?.send
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from 'vitest'
2+
import type { UserConfig } from 'vitest/config'
3+
4+
test('is isolated', () => {
5+
// @ts-expect-error -- internal
6+
const config: NonNullable<UserConfig['test']> = globalThis.__vitest_worker__.config
7+
8+
if (config.pool === 'forks') {
9+
expect(config.poolOptions?.forks?.isolate).toBe(true)
10+
}
11+
else {
12+
expect(config.pool).toBe('threads')
13+
expect(config.poolOptions?.threads?.isolate).toBe(true)
14+
}
15+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from 'vitest'
2+
import type { UserConfig } from 'vitest/config'
3+
4+
test('is multi worker', () => {
5+
// @ts-expect-error -- internal
6+
const config: NonNullable<UserConfig['test']> = globalThis.__vitest_worker__.config
7+
8+
if (config.pool === 'forks') {
9+
expect(config.poolOptions?.forks?.singleFork).toBe(false)
10+
}
11+
else {
12+
expect(config.pool).toBe('threads')
13+
expect(config.poolOptions?.threads?.singleThread).toBe(false)
14+
}
15+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from 'vitest'
2+
import type { UserConfig } from 'vitest/config'
3+
4+
test('is not isolated', () => {
5+
// @ts-expect-error -- internal
6+
const config: NonNullable<UserConfig['test']> = globalThis.__vitest_worker__.config
7+
8+
if (config.pool === 'forks') {
9+
expect(config.poolOptions?.forks?.isolate).toBe(false)
10+
}
11+
else {
12+
expect(config.pool).toBe('threads')
13+
expect(config.poolOptions?.threads?.isolate).toBe(false)
14+
}
15+
})
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect, test } from 'vitest'
2+
import type { UserConfig } from 'vitest/config'
3+
4+
test('is single worker', () => {
5+
// @ts-expect-error -- internal
6+
const config: NonNullable<UserConfig['test']> = globalThis.__vitest_worker__.config
7+
8+
if (config.pool === 'forks') {
9+
expect(config.poolOptions?.forks?.singleFork).toBe(true)
10+
}
11+
else {
12+
expect(config.pool).toBe('threads')
13+
expect(config.poolOptions?.threads?.singleThread).toBe(true)
14+
}
15+
})
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { isMainThread } from 'node:worker_threads'
2+
import { expect, test } from 'vitest'
3+
4+
test('is run in "node:worker_threads"', () => {
5+
expect(isChildProcess()).toBe(false)
6+
expect(isMainThread).toBe(false)
7+
})
8+
9+
// TODO: Use from "src/utils/base.ts" after #4441
10+
function isChildProcess(): boolean {
11+
return !!process?.send
12+
}

0 commit comments

Comments
 (0)