diff --git a/packages/vitest/src/node/config/resolveConfig.ts b/packages/vitest/src/node/config/resolveConfig.ts index aa5ea3a0d920..f38c4e4a1fdb 100644 --- a/packages/vitest/src/node/config/resolveConfig.ts +++ b/packages/vitest/src/node/config/resolveConfig.ts @@ -540,7 +540,7 @@ export function resolveConfig( ), // Exclude test files - ...resolved.include, + ...resolved.include.filter(pattern => !pattern.startsWith('!')), // Configs resolved.config && slash(resolved.config), diff --git a/test/coverage-test/test/include-exclude.unit.test.ts b/test/coverage-test/test/include-exclude.unit.test.ts index 0287b7d0096a..333aa850a48d 100644 --- a/test/coverage-test/test/include-exclude.unit.test.ts +++ b/test/coverage-test/test/include-exclude.unit.test.ts @@ -59,6 +59,19 @@ test('include without actual glob', async () => { expect.soft(isIncluded('src-file-in-root.js')).toBe(false) }) +test('test.include with negations', async () => { + const isIncluded = await init({ + include: ['src/**'], + testInclude: ['!something'], + }) + + expect.soft(isIncluded('src/component.js')).toBe(true) + expect.soft(isIncluded('src/nested/component.ts')).toBe(true) + + expect.soft(isIncluded('top-level.ts')).toBe(false) + expect.soft(isIncluded('utils/math.ts')).toBe(false) +}) + test('no include defaults to match all files', async () => { const isIncluded = await init({ exclude: [], @@ -131,10 +144,10 @@ test('files outside project when allowExternal: true', async () => { expect(isIncluded(resolve(process.cwd(), '../../package-b/src/two.ts'))).toBe(false) }) -async function init(options: Partial) { +async function init(options: Partial & { testInclude?: string[] }) { const vitest = await createVitest('test', { config: false, - include: ['dont-match-anything'], + include: ['dont-match-anything', ...(options.testInclude || [])], coverage: { ...options, enabled: true, @@ -143,7 +156,7 @@ async function init(options: Partial) { }, {}, { stdout: new Writable() }) onTestFinished(() => vitest.close()) - await vitest.init() + await vitest.standalone() const provider = vitest.coverageProvider as unknown as BaseCoverageProvider