Skip to content

Commit 14dc072

Browse files
authored
fix(pool): auto-adjust minWorkers when only maxWorkers specified (#8110)
1 parent 85dc019 commit 14dc072

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

packages/vitest/src/node/pools/forks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function createForksPool(
7979
const maxThreads
8080
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount
8181
const minThreads
82-
= poolOptions.minForks ?? vitest.config.minWorkers ?? threadsCount
82+
= poolOptions.minForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
8383

8484
const worker = resolve(vitest.distPath, 'workers/forks.js')
8585

packages/vitest/src/node/pools/threads.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function createThreadsPool(
5757
const maxThreads
5858
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount
5959
const minThreads
60-
= poolOptions.minThreads ?? vitest.config.minWorkers ?? threadsCount
60+
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
6161

6262
const worker = resolve(vitest.distPath, 'workers/threads.js')
6363

packages/vitest/src/node/pools/vmForks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function createVmForksPool(
8585
const maxThreads
8686
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount
8787
const minThreads
88-
= poolOptions.maxForks ?? vitest.config.minWorkers ?? threadsCount
88+
= poolOptions.maxForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
8989

9090
const worker = resolve(vitest.distPath, 'workers/vmForks.js')
9191

packages/vitest/src/node/pools/vmThreads.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function createVmThreadsPool(
6060
const maxThreads
6161
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount
6262
const minThreads
63-
= poolOptions.minThreads ?? vitest.config.minWorkers ?? threadsCount
63+
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)
6464

6565
const worker = resolve(vitest.distPath, 'workers/vmThreads.js')
6666

test/config/test/failures.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { UserConfig as ViteUserConfig } from 'vite'
22
import type { UserConfig } from 'vitest/node'
33
import type { VitestRunnerCLIOptions } from '../../test-utils'
4+
import { cpus } from 'node:os'
45
import { normalize, resolve } from 'pathe'
5-
66
import { beforeEach, expect, test } from 'vitest'
77
import { version } from 'vitest/package.json'
88
import * as testUtils from '../../test-utils'
@@ -12,7 +12,7 @@ const names = ['edge', 'chromium', 'webkit', 'chrome', 'firefox', 'safari'] as c
1212
const browsers = providers.map(provider => names.map(name => ({ name, provider }))).flat()
1313

1414
function runVitest(config: NonNullable<UserConfig> & { shard?: any }, viteOverrides: ViteUserConfig = {}, runnerOptions?: VitestRunnerCLIOptions) {
15-
return testUtils.runVitest({ root: './fixtures/test', ...config }, [], undefined, viteOverrides, runnerOptions)
15+
return testUtils.runVitest({ root: './fixtures/test', include: ['example.test.ts'], ...config }, [], undefined, viteOverrides, runnerOptions)
1616
}
1717

1818
function runVitestCli(...cliArgs: string[]) {
@@ -553,3 +553,23 @@ test('non existing project name array will throw', async () => {
553553
const { stderr } = await runVitest({ project: ['non-existing-project', 'also-non-existing'] })
554554
expect(stderr).toMatch('No projects matched the filter "non-existing-project", "also-non-existing".')
555555
})
556+
557+
test('minWorkers must be smaller than maxWorkers', async () => {
558+
const { stderr } = await runVitest({ minWorkers: 2, maxWorkers: 1 })
559+
560+
expect(stderr).toMatch('RangeError: options.minThreads and options.maxThreads must not conflict')
561+
})
562+
563+
test('minWorkers higher than maxWorkers does not crash', async ({ skip }) => {
564+
skip(cpus().length < 2, 'Test requires +2 CPUs')
565+
566+
const { stdout, stderr } = await runVitest({
567+
maxWorkers: 1,
568+
569+
// Overrides defaults of "runVitest" of "test-utils"
570+
minWorkers: undefined,
571+
})
572+
573+
expect(stdout).toMatch('✓ example.test.ts > it works')
574+
expect(stderr).toBe('')
575+
})

0 commit comments

Comments
 (0)