Skip to content

Commit e9fe418

Browse files
authoredJan 9, 2024
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

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

+18-2
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

+5-1
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

+8-3
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

+12
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+
}
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+
})
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+
})
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+
})
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+
})
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+
}

‎test/workspaces/vitest.workspace.ts

+90
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,96 @@ export default defineWorkspace([
2323
},
2424
},
2525

26+
// Projects testing pool and poolOptions
27+
{
28+
test: {
29+
name: 'Threads pool',
30+
include: [
31+
'./space-pools/threads.test.ts',
32+
'./space-pools/multi-worker.test.ts',
33+
'./space-pools/isolate.test.ts',
34+
],
35+
pool: 'threads',
36+
},
37+
},
38+
{
39+
test: {
40+
name: 'Single thread pool',
41+
include: [
42+
'./space-pools/threads.test.ts',
43+
'./space-pools/single-worker.test.ts',
44+
],
45+
pool: 'threads',
46+
poolOptions: { threads: { singleThread: true } },
47+
},
48+
},
49+
{
50+
test: {
51+
name: 'Non-isolated thread pool #1',
52+
include: [
53+
'./space-pools/threads.test.ts',
54+
'./space-pools/no-isolate.test.ts',
55+
],
56+
pool: 'threads',
57+
poolOptions: { threads: { isolate: false } },
58+
},
59+
},
60+
{
61+
test: {
62+
name: 'Non-isolated thread pool #2',
63+
include: [
64+
'./space-pools/threads.test.ts',
65+
'./space-pools/no-isolate.test.ts',
66+
],
67+
pool: 'threads',
68+
isolate: false,
69+
},
70+
},
71+
{
72+
test: {
73+
name: 'Forks pool',
74+
include: [
75+
'./space-pools/forks.test.ts',
76+
'./space-pools/multi-worker.test.ts',
77+
'./space-pools/isolate.test.ts',
78+
],
79+
pool: 'forks',
80+
},
81+
},
82+
{
83+
test: {
84+
name: 'Single fork pool',
85+
include: [
86+
'./space-pools/forks.test.ts',
87+
'./space-pools/single-worker.test.ts',
88+
],
89+
pool: 'forks',
90+
poolOptions: { forks: { singleFork: true } },
91+
},
92+
},
93+
{
94+
test: {
95+
name: 'Non-isolated fork pool #1',
96+
include: [
97+
'./space-pools/forks.test.ts',
98+
'./space-pools/no-isolate.test.ts',
99+
],
100+
pool: 'forks',
101+
poolOptions: { forks: { isolate: false } },
102+
},
103+
},
104+
{
105+
test: {
106+
name: 'Non-isolated fork pool #2',
107+
include: [
108+
'./space-pools/forks.test.ts',
109+
'./space-pools/no-isolate.test.ts',
110+
],
111+
pool: 'forks',
112+
isolate: false,
113+
},
114+
},
115+
26116
// These two projects run on same environment but still transform
27117
// a single file differently due to Vite plugins
28118
{

0 commit comments

Comments
 (0)
Please sign in to comment.