diff --git a/test/coverage-test/fixtures/configs/vitest.config.configure-vitest-hook.ts b/test/coverage-test/fixtures/configs/vitest.config.configure-vitest-hook.ts new file mode 100644 index 000000000..2bcd70e6d --- /dev/null +++ b/test/coverage-test/fixtures/configs/vitest.config.configure-vitest-hook.ts @@ -0,0 +1,18 @@ +import assert from 'node:assert'; +import { defineConfig } from 'vitest/config' + +export default defineConfig({ + plugins: [{ + name: "coverage-options-by-runtime-plugin", + configureVitest(context) { + const coverage = context.vitest.config.coverage + assert(coverage.provider === "v8" || coverage.provider === "istanbul") + + coverage.include ||= [] + coverage.include.push("**/even.ts"); + coverage.include.push("**/untested-file.ts"); + + coverage.exclude.push("**/math.ts"); + }, + }], +}) diff --git a/test/coverage-test/test/include-exclude.test.ts b/test/coverage-test/test/include-exclude.test.ts index e8f3b3f52..51fba2c0f 100644 --- a/test/coverage-test/test/include-exclude.test.ts +++ b/test/coverage-test/test/include-exclude.test.ts @@ -1,3 +1,4 @@ +import assert from 'node:assert' import { expect } from 'vitest' import { coverageConfigDefaults } from 'vitest/config' import { readCoverageMap, runVitest, test } from '../utils' @@ -59,3 +60,82 @@ test('test files are automatically excluded from report when excludes is set', a const files = coverageMap.files() expect(files.find(file => file.includes('test-that-looks-like-source-file'))).toBeFalsy() }) + +test('files included and excluded in plugin\'s configureVitest are excluded', async () => { + await runVitest({ + config: 'fixtures/configs/vitest.config.configure-vitest-hook.ts', + include: ['fixtures/test/math.test.ts', 'fixtures/test/even.test.ts'], + coverage: { + // Include math.ts by default, exclude it in plugin config + include: ['**/math.ts'], + all: true, + reporter: 'json', + }, + }) + + const coverageMap = await readCoverageMap() + const files = coverageMap.files() + + expect(files).toMatchInlineSnapshot(` + [ + "/fixtures/src/even.ts", + "/fixtures/src/untested-file.ts", + ] + `) +}) + +test('files included and excluded in project\'s plugin\'s configureVitest are excluded', async () => { + await runVitest({ + coverage: { + // Include math.ts by default, exclude it in plugin config + include: ['**/math.ts'], + all: true, + reporter: 'json', + }, + projects: [ + { + test: { + name: 'first', + include: ['fixtures/test/math.test.ts'], + }, + plugins: [{ + name: 'coverage-options-by-runtime-plugin', + configureVitest(context) { + const coverage = context.vitest.config.coverage + assert(coverage.provider === 'v8' || coverage.provider === 'istanbul') + + coverage.include ||= [] + coverage.include.push('**/even.ts') + }, + }], + }, + { + test: { + name: 'second', + include: ['fixtures/test/even.test.ts'], + }, + plugins: [{ + name: 'coverage-options-by-runtime-plugin', + configureVitest(context) { + const coverage = context.vitest.config.coverage + assert(coverage.provider === 'v8' || coverage.provider === 'istanbul') + + coverage.include ||= [] + coverage.include.push('**/untested-file.ts') + coverage.exclude.push('**/math.ts') + }, + }], + }, + ], + }) + + const coverageMap = await readCoverageMap() + const files = coverageMap.files() + + expect(files).toMatchInlineSnapshot(` + [ + "/fixtures/src/even.ts", + "/fixtures/src/untested-file.ts", + ] + `) +})