Skip to content

Commit 3303ba8

Browse files
committed
fix: inherit Vitest 5 inline project test selectors
Vitest 5 defaults extends to true for inline projects, so omitted extends must inherit root include/exclude/root unless the project sets extends: false.
1 parent fc5dac6 commit 3303ba8

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎scripts/react-port/test-discovery.mjs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export function configuredTestSelectors(
237237
project && typeof project === 'object' && !Array.isArray(project)
238238
? runner === 'playwright'
239239
? { ...shared, ...project }
240-
: project.extends === true
240+
: vitestInlineProjectInheritsRoot(project.extends, vitestVersion)
241241
? { ...project, test: mergeConfiguration(shared, project.test ?? {}) }
242242
: project
243243
: project,
@@ -340,10 +340,22 @@ export function resolveConfigurationImport(fileName, specifier, available) {
340340
return candidates.find((candidate) => available.has(candidate));
341341
}
342342

343+
// Vitest 3/4 inherit only when extends is true. Vitest 5 defaults extends to
344+
// true and uses extends: false to isolate an inline project.
345+
function vitestInlineProjectInheritsRoot(extendsValue, version) {
346+
if (extendsValue === true) return true;
347+
if (extendsValue === false) return false;
348+
return vitestMajor(version) === '5';
349+
}
350+
343351
// Vitest 3 and 4/5 have different selector defaults. Unknown versions must not
344352
// inherit the locally installed runner's selectors during immutable intake.
353+
function vitestMajor(version) {
354+
return /^[~^]?([345])\.\d+\.\d+$/.exec(version ?? '')?.[1];
355+
}
356+
345357
function vitestSelectorDefaults(version) {
346-
const major = /^[~^]?([345])\.\d+\.\d+$/.exec(version ?? '')?.[1];
358+
const major = vitestMajor(version);
347359
if (!major) return undefined;
348360
return {
349361
include: ['**/*.{test,spec}.?(c|m)[jt]s?(x)'],

‎scripts/react-port/test-discovery.test.mjs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,27 @@ test('Vitest projects inherit selectors only when extends is true and match with
293293
);
294294
});
295295

296+
test('Vitest 5 inline projects inherit root selectors unless extends is false', () => {
297+
const omitted = `export default defineConfig({ test: { exclude: ['**/e2e/**'], projects: [
298+
{ test: { include: ['**/*.test.ts'] } }
299+
] } });`;
300+
const isolated = `export default defineConfig({ test: { exclude: ['**/e2e/**'], projects: [
301+
{ extends: false, test: { include: ['**/*.test.ts'] } }
302+
] } });`;
303+
const files = ['src/widget.test.ts', 'e2e/flow.test.ts'];
304+
assert.deepEqual(selected(omitted, files, { runner: 'vitest', vitestVersion: '^5.0.0' }), [
305+
'src/widget.test.ts',
306+
]);
307+
assert.deepEqual(selected(omitted, files, { runner: 'vitest', vitestVersion: '^4.1.0' }), [
308+
'src/widget.test.ts',
309+
'e2e/flow.test.ts',
310+
]);
311+
assert.deepEqual(selected(isolated, files, { runner: 'vitest', vitestVersion: '^5.0.0' }), [
312+
'src/widget.test.ts',
313+
'e2e/flow.test.ts',
314+
]);
315+
});
316+
296317
test('reads versioned Vitest config defaults and retains explicit exclusions', () => {
297318
const source = `import { configDefaults as defaults, defineConfig } from 'vitest/config'; export default defineConfig({ test: { exclude: [...defaults.exclude, 'src/index.test.ts'] } });`;
298319
const files = [

0 commit comments

Comments
 (0)