From 8a18c8e20a19f2c8d9f402e426886999f378c389 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 17 Jun 2025 19:41:37 +0300 Subject: [PATCH] fix(cli): throw error when `--shard x/` exceeds count of test files (#8112) --- packages/vitest/src/node/pool.ts | 7 +++++++ test/config/test/failures.test.ts | 12 ++++++++++++ test/coverage-test/test/shard.test.ts | 8 ++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/packages/vitest/src/node/pool.ts b/packages/vitest/src/node/pool.ts index 7244b99a2..405610307 100644 --- a/packages/vitest/src/node/pool.ts +++ b/packages/vitest/src/node/pool.ts @@ -224,6 +224,13 @@ export function createPool(ctx: Vitest): ProcessPool { async function sortSpecs(specs: TestSpecification[]) { if (ctx.config.shard) { + if (!ctx.config.passWithNoTests && ctx.config.shard.count > specs.length) { + throw new Error( + '--shard must be a smaller than count of test files. ' + + `Resolved ${specs.length} test files for --shard=${ctx.config.shard.index}/${ctx.config.shard.count}.`, + ) + } + specs = await sequencer.shard(specs) } return sequencer.sort(specs) diff --git a/test/config/test/failures.test.ts b/test/config/test/failures.test.ts index e14656edb..0b37e0216 100644 --- a/test/config/test/failures.test.ts +++ b/test/config/test/failures.test.ts @@ -51,6 +51,18 @@ test('shard index must be smaller than count', async () => { expect(stderr).toMatch('Error: --shard must be a positive number less then ') }) +test('shard count must be smaller than count of test files', async () => { + const { stderr } = await runVitest({ root: './fixtures/shard', shard: '1/4', include: ['**/*.test.js'] }) + + expect(stderr).toMatch('Error: --shard must be a smaller than count of test files. Resolved 3 test files for --shard=1/4.') +}) + +test('shard count can be smaller than count of test files when passWithNoTests', async () => { + const { stderr } = await runVitest({ root: './fixtures/shard', shard: '1/4', passWithNoTests: true, include: ['**/*.test.js'] }) + + expect(stderr).toMatch('') +}) + test('inspect requires changing pool and singleThread/singleFork', async () => { const { stderr } = await runVitest({ inspect: true }) diff --git a/test/coverage-test/test/shard.test.ts b/test/coverage-test/test/shard.test.ts index 523e57ef0..cb7cba717 100644 --- a/test/coverage-test/test/shard.test.ts +++ b/test/coverage-test/test/shard.test.ts @@ -2,16 +2,16 @@ import { readdirSync } from 'node:fs' import { expect } from 'vitest' import { coverageTest, normalizeURL, runVitest, test } from '../utils' -test('{ shard: 1/4 }', async () => { +test('{ shard: 1/3 }', async () => { await runVitest({ - include: [normalizeURL(import.meta.url)], - shard: '1/4', + include: [normalizeURL(import.meta.url), 'fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'], + shard: '1/3', }) }) coverageTest('temporary directory is postfixed with --shard value', () => { const files = readdirSync('./coverage') - expect(files).toContain('.tmp-1-4') + expect(files).toContain('.tmp-1-3') expect(files).not.toContain('.tmp') }) -- 2.51.2