Skip to content

Commit 8a18c8e

Browse files
authored
fix(cli): throw error when --shard x/<count> exceeds count of test files (#8112)
1 parent 8abd7cc commit 8a18c8e

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

packages/vitest/src/node/pool.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ export function createPool(ctx: Vitest): ProcessPool {
224224

225225
async function sortSpecs(specs: TestSpecification[]) {
226226
if (ctx.config.shard) {
227+
if (!ctx.config.passWithNoTests && ctx.config.shard.count > specs.length) {
228+
throw new Error(
229+
'--shard <count> must be a smaller than count of test files. '
230+
+ `Resolved ${specs.length} test files for --shard=${ctx.config.shard.index}/${ctx.config.shard.count}.`,
231+
)
232+
}
233+
227234
specs = await sequencer.shard(specs)
228235
}
229236
return sequencer.sort(specs)

test/config/test/failures.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ test('shard index must be smaller than count', async () => {
5151
expect(stderr).toMatch('Error: --shard <index> must be a positive number less then <count>')
5252
})
5353

54+
test('shard count must be smaller than count of test files', async () => {
55+
const { stderr } = await runVitest({ root: './fixtures/shard', shard: '1/4', include: ['**/*.test.js'] })
56+
57+
expect(stderr).toMatch('Error: --shard <count> must be a smaller than count of test files. Resolved 3 test files for --shard=1/4.')
58+
})
59+
60+
test('shard count can be smaller than count of test files when passWithNoTests', async () => {
61+
const { stderr } = await runVitest({ root: './fixtures/shard', shard: '1/4', passWithNoTests: true, include: ['**/*.test.js'] })
62+
63+
expect(stderr).toMatch('')
64+
})
65+
5466
test('inspect requires changing pool and singleThread/singleFork', async () => {
5567
const { stderr } = await runVitest({ inspect: true })
5668

test/coverage-test/test/shard.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { readdirSync } from 'node:fs'
22
import { expect } from 'vitest'
33
import { coverageTest, normalizeURL, runVitest, test } from '../utils'
44

5-
test('{ shard: 1/4 }', async () => {
5+
test('{ shard: 1/3 }', async () => {
66
await runVitest({
7-
include: [normalizeURL(import.meta.url)],
8-
shard: '1/4',
7+
include: [normalizeURL(import.meta.url), 'fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'],
8+
shard: '1/3',
99
})
1010
})
1111

1212
coverageTest('temporary directory is postfixed with --shard value', () => {
1313
const files = readdirSync('./coverage')
1414

15-
expect(files).toContain('.tmp-1-4')
15+
expect(files).toContain('.tmp-1-3')
1616
expect(files).not.toContain('.tmp')
1717
})

0 commit comments

Comments
 (0)