diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap index 576b4ef0d1c7..9af54d6026c4 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/istanbul.report.test.ts.snap @@ -2,89 +2,6 @@ exports[`istanbul json report 1`] = ` { - "/../test-utils/fixtures/math.ts": { - "b": {}, - "branchMap": {}, - "f": { - "0": 1, - "1": 0, - }, - "fnMap": { - "0": { - "decl": { - "end": { - "column": 20, - "line": 1, - }, - "start": { - "column": 16, - "line": 1, - }, - }, - "loc": { - "end": { - "column": null, - "line": 3, - }, - "start": { - "column": 42, - "line": 1, - }, - }, - "name": "sum", - }, - "1": { - "decl": { - "end": { - "column": 25, - "line": 5, - }, - "start": { - "column": 16, - "line": 5, - }, - }, - "loc": { - "end": { - "column": null, - "line": 7, - }, - "start": { - "column": 47, - "line": 5, - }, - }, - "name": "multiply", - }, - }, - "path": "/../test-utils/fixtures/math.ts", - "s": { - "0": 1, - "1": 0, - }, - "statementMap": { - "0": { - "end": { - "column": null, - "line": 2, - }, - "start": { - "column": 2, - "line": 2, - }, - }, - "1": { - "end": { - "column": null, - "line": 6, - }, - "start": { - "column": 2, - "line": 6, - }, - }, - }, - }, "/src/Counter/Counter.component.ts": { "b": {}, "branchMap": {}, diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap index 68990050c7cf..08c1cc21d00e 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/v8.report.test.ts.snap @@ -2,178 +2,6 @@ exports[`v8 json report 1`] = ` { - "/../test-utils/fixtures/math.ts": { - "all": false, - "b": { - "0": [ - 1, - ], - }, - "branchMap": { - "0": { - "line": 1, - "loc": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 7, - "line": 1, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 7, - "line": 1, - }, - }, - ], - "type": "branch", - }, - }, - "f": { - "0": 1, - "1": 0, - }, - "fnMap": { - "0": { - "decl": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 7, - "line": 1, - }, - }, - "line": 1, - "loc": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 7, - "line": 1, - }, - }, - "name": "sum", - }, - "1": { - "decl": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 7, - "line": 5, - }, - }, - "line": 5, - "loc": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 7, - "line": 5, - }, - }, - "name": "multiply", - }, - }, - "path": "/../test-utils/fixtures/math.ts", - "s": { - "0": 1, - "1": 1, - "2": 1, - "3": 1, - "4": 1, - "5": 0, - "6": 0, - }, - "statementMap": { - "0": { - "end": { - "column": 43, - "line": 1, - }, - "start": { - "column": 0, - "line": 1, - }, - }, - "1": { - "end": { - "column": 14, - "line": 2, - }, - "start": { - "column": 0, - "line": 2, - }, - }, - "2": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "3": { - "end": { - "column": 0, - "line": 4, - }, - "start": { - "column": 0, - "line": 4, - }, - }, - "4": { - "end": { - "column": 48, - "line": 5, - }, - "start": { - "column": 0, - "line": 5, - }, - }, - "5": { - "end": { - "column": 14, - "line": 6, - }, - "start": { - "column": 0, - "line": 6, - }, - }, - "6": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - }, - }, "/src/Counter/Counter.component.ts": { "all": false, "b": { diff --git a/test/coverage-test/coverage-report-tests/allow-external.test.ts b/test/coverage-test/coverage-report-tests/allow-external.test.ts new file mode 100644 index 000000000000..fecca62077c0 --- /dev/null +++ b/test/coverage-test/coverage-report-tests/allow-external.test.ts @@ -0,0 +1,20 @@ +import fs from 'node:fs' +import { expect, test } from 'vitest' + +const allowExternal = import.meta.env.VITE_COVERAGE_ALLOW_EXTERNAL + +test.skipIf(!allowExternal)('{ allowExternal: true } includes files outside project root', async () => { + expect(fs.existsSync('./coverage/test-utils/fixtures/math.ts.html')).toBe(true) + + // Files inside project root should always be included + expect(fs.existsSync('./coverage/coverage-test/src/utils.ts.html')).toBe(true) +}) + +test.skipIf(allowExternal)('{ allowExternal: false } excludes files outside project root', async () => { + expect(fs.existsSync('./coverage/test-utils/fixtures/math.ts.html')).toBe(false) + expect(fs.existsSync('./test-utils/fixtures/math.ts.html')).toBe(false) + expect(fs.existsSync('./fixtures/math.ts.html')).toBe(false) + + // Files inside project root should always be included + expect(fs.existsSync('./coverage/utils.ts.html')).toBe(true) +}) diff --git a/test/coverage-test/coverage-report-tests/generic.report.test.ts b/test/coverage-test/coverage-report-tests/generic.report.test.ts index b84b2ed7e81e..350c23c3677e 100644 --- a/test/coverage-test/coverage-report-tests/generic.report.test.ts +++ b/test/coverage-test/coverage-report-tests/generic.report.test.ts @@ -10,7 +10,7 @@ import libCoverage from 'istanbul-lib-coverage' import { readCoverageJson } from './utils' test('html report', async () => { - const coveragePath = resolve('./coverage/coverage-test/src') + const coveragePath = resolve('./coverage/src') const files = fs.readdirSync(coveragePath) expect(files).toContain('index.html') @@ -31,14 +31,14 @@ test('lcov report', async () => { }) test('all includes untested files', () => { - const coveragePath = resolve('./coverage/coverage-test/src') + const coveragePath = resolve('./coverage/src') const files = fs.readdirSync(coveragePath) expect(files).toContain('untested-file.ts.html') }) test('files should not contain query parameters', () => { - const coveragePath = resolve('./coverage/coverage-test/src/Counter') + const coveragePath = resolve('./coverage/src/Counter') const files = fs.readdirSync(coveragePath) expect(files).toContain('index.html') @@ -48,19 +48,20 @@ test('files should not contain query parameters', () => { }) test('file using import.meta.env is included in report', async () => { - const coveragePath = resolve('./coverage/coverage-test/src') + const coveragePath = resolve('./coverage/src') const files = fs.readdirSync(coveragePath) expect(files).toContain('importEnv.ts.html') }) test('files should not contain a setup file', () => { - const coveragePath = resolve('./coverage/coverage-test') + const coveragePath = resolve('./coverage') const files = fs.readdirSync(coveragePath) + expect(files).not.toContain('coverage-test') expect(files).not.toContain('setup.ts.html') - const coverageSrcPath = resolve('./coverage/coverage-test/src') + const coverageSrcPath = resolve('./coverage/src') const srcFiles = fs.readdirSync(coverageSrcPath) expect(srcFiles).not.toContain('another-setup.ts.html') @@ -102,8 +103,8 @@ test('coverage provider does not conflict with built-in reporter\'s outputFile', }) test('virtual files should be excluded', () => { - const files = fs.readdirSync(resolve('./coverage/coverage-test')) - const srcFiles = fs.readdirSync(resolve('./coverage/coverage-test/src')) + const files = fs.readdirSync(resolve('./coverage')) + const srcFiles = fs.readdirSync(resolve('./coverage/src')) for (const file of [...files, ...srcFiles]) { expect(file).not.toContain('virtual:') diff --git a/test/coverage-test/coverage-report-tests/utils.ts b/test/coverage-test/coverage-report-tests/utils.ts index 3833379bd7a0..d06aa62cc71f 100644 --- a/test/coverage-test/coverage-report-tests/utils.ts +++ b/test/coverage-test/coverage-report-tests/utils.ts @@ -31,7 +31,5 @@ export async function readCoverageJson() { } export function normalizeFilename(filename: string) { - return normalize(filename) - .replace(normalize(process.cwd()), '') - .replace(normalize(process.cwd().replace('coverage-test', '')), '/../') + return normalize(filename).replace(normalize(process.cwd()), '') } diff --git a/test/coverage-test/option-tests/allow-external.test.ts b/test/coverage-test/option-tests/allow-external.test.ts new file mode 100644 index 000000000000..95299ebf5d6b --- /dev/null +++ b/test/coverage-test/option-tests/allow-external.test.ts @@ -0,0 +1,12 @@ +import { expect, test } from 'vitest' + +import { multiply } from '../src/utils' +import * as ExternalMath from '../../test-utils/fixtures/math' + +test('calling files outside project root', () => { + expect(ExternalMath.sum(2, 3)).toBe(5) +}) + +test('multiply - add some files to report', () => { + expect(multiply(2, 3)).toBe(6) +}) diff --git a/test/coverage-test/package.json b/test/coverage-test/package.json index a699b16448cb..16525ea6e1bb 100644 --- a/test/coverage-test/package.json +++ b/test/coverage-test/package.json @@ -2,11 +2,12 @@ "name": "@vitest/test-coverage", "private": true, "scripts": { - "test": "pnpm test:v8 && pnpm test:istanbul && pnpm test:custom && pnpm test:browser && pnpm test:types", + "test": "pnpm test:v8 && pnpm test:istanbul && pnpm test:custom && pnpm test:browser && pnpm test:options && pnpm test:types", "test:v8": "node ./testing.mjs --provider v8", "test:custom": "node ./testing.mjs --provider custom", "test:istanbul": "node ./testing.mjs --provider istanbul", "test:browser": "node ./testing.mjs --browser --provider istanbul", + "test:options": "node ./testing-options.mjs", "test:types": "vitest typecheck --run --reporter verbose" }, "devDependencies": { diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index f284a750a433..254d4f47902d 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -3,8 +3,6 @@ import { describe, expect, test } from 'vitest' // @ts-expect-error -- untyped virtual file provided by custom plugin import virtualFile1 from 'virtual:vitest-custom-virtual-file-1' -import * as ExternalMath from '../../test-utils/fixtures/math' - import { implicitElse } from '../src/implicitElse' import { useImportEnv } from '../src/importEnv' import { second } from '../src/function-count' @@ -60,10 +58,6 @@ describe('Multiple test suites', () => { }) }) -test('calling files outside project root', () => { - expect(ExternalMath.sum(2, 3)).toBe(5) -}) - test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => { await runDynamicFileESM() }) diff --git a/test/coverage-test/testing-options.mjs b/test/coverage-test/testing-options.mjs new file mode 100644 index 000000000000..f86568c0720e --- /dev/null +++ b/test/coverage-test/testing-options.mjs @@ -0,0 +1,69 @@ +import { startVitest } from 'vitest/node' + +/** @type {Record>[]} */ +const testCases = [ + { + testConfig: { + name: 'allowExternal: true', + include: ['option-tests/allow-external.test.ts'], + coverage: { + allowExternal: true, + include: ['**/src/**', '**/test-utils/fixtures/**'], + reporter: 'html', + }, + }, + assertionConfig: { + include: ['coverage-report-tests/allow-external.test.ts'], + env: { VITE_COVERAGE_ALLOW_EXTERNAL: true }, + }, + }, + { + testConfig: { + name: 'allowExternal: false', + include: ['option-tests/allow-external.test.ts'], + coverage: { + allowExternal: false, + include: ['**/src/**', '**/test-utils/fixtures/**'], + reporter: 'html', + }, + }, + assertionConfig: { + include: ['coverage-report-tests/allow-external.test.ts'], + }, + }, +] + +for (const provider of ['v8', 'istanbul']) { + for (const { testConfig, assertionConfig } of testCases) { + // Run test case + await startVitest('test', ['option-tests/'], { + config: false, + watch: false, + ...testConfig, + name: `${provider} - ${testConfig.name}`, + coverage: { + enabled: true, + clean: true, + provider, + ...testConfig.coverage, + }, + }) + + checkExit() + + // Check generated coverage report + await startVitest('test', ['coverage-report-tests'], { + config: false, + watch: false, + ...assertionConfig, + name: `${provider} - assert ${testConfig.name}`, + }) + + checkExit() + } +} + +function checkExit() { + if (process.exitCode) + process.exit(process.exitCode) +} diff --git a/test/coverage-test/vitest.config.ts b/test/coverage-test/vitest.config.ts index 42fb266ee1d5..258ea5e8bf7b 100644 --- a/test/coverage-test/vitest.config.ts +++ b/test/coverage-test/vitest.config.ts @@ -43,8 +43,7 @@ export default defineConfig({ coverage: { provider: provider as any, customProviderModule: provider === 'custom' ? 'custom-provider' : undefined, - include: ['**/src/**', '**/test-utils/fixtures/**'], - allowExternal: true, + include: ['src/**'], clean: true, all: true, reporter: [