Skip to content

fix(pool): auto-adjust minWorkers when only maxWorkers specified #8110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vitest/src/node/pools/forks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function createForksPool(
const maxThreads
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount
const minThreads
= poolOptions.minForks ?? vitest.config.minWorkers ?? threadsCount
= poolOptions.minForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)

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

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/pools/threads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function createThreadsPool(
const maxThreads
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount
const minThreads
= poolOptions.minThreads ?? vitest.config.minWorkers ?? threadsCount
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)

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

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/pools/vmForks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function createVmForksPool(
const maxThreads
= poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount
const minThreads
= poolOptions.maxForks ?? vitest.config.minWorkers ?? threadsCount
= poolOptions.maxForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)

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

Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/node/pools/vmThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function createVmThreadsPool(
const maxThreads
= poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount
const minThreads
= poolOptions.minThreads ?? vitest.config.minWorkers ?? threadsCount
= poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads)

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

Expand Down
24 changes: 22 additions & 2 deletions test/config/test/failures.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { UserConfig as ViteUserConfig } from 'vite'
import type { UserConfig } from 'vitest/node'
import type { VitestRunnerCLIOptions } from '../../test-utils'
import { cpus } from 'node:os'
import { normalize, resolve } from 'pathe'

import { beforeEach, expect, test } from 'vitest'
import { version } from 'vitest/package.json'
import * as testUtils from '../../test-utils'
Expand All @@ -12,7 +12,7 @@ const names = ['edge', 'chromium', 'webkit', 'chrome', 'firefox', 'safari'] as c
const browsers = providers.map(provider => names.map(name => ({ name, provider }))).flat()

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

function runVitestCli(...cliArgs: string[]) {
Expand Down Expand Up @@ -553,3 +553,23 @@ test('non existing project name array will throw', async () => {
const { stderr } = await runVitest({ project: ['non-existing-project', 'also-non-existing'] })
expect(stderr).toMatch('No projects matched the filter "non-existing-project", "also-non-existing".')
})

test('minWorkers must be smaller than maxWorkers', async () => {
const { stderr } = await runVitest({ minWorkers: 2, maxWorkers: 1 })

expect(stderr).toMatch('RangeError: options.minThreads and options.maxThreads must not conflict')
})

test('minWorkers higher than maxWorkers does not crash', async ({ skip }) => {
skip(cpus().length < 2, 'Test requires +2 CPUs')

const { stdout, stderr } = await runVitest({
maxWorkers: 1,

// Overrides defaults of "runVitest" of "test-utils"
minWorkers: undefined,
})

expect(stdout).toMatch('✓ example.test.ts > it works')
expect(stderr).toBe('')
})
Loading