From 550df6496089ce2e6650ec9466694b7e83c8fc5e Mon Sep 17 00:00:00 2001 From: Stefan Vojvodic Date: Sat, 5 Aug 2023 13:27:26 +0200 Subject: [PATCH 1/9] feat: add allowExternal option for coverage providers --- packages/coverage-istanbul/src/provider.ts | 2 ++ packages/coverage-v8/src/provider.ts | 2 ++ packages/vitest/src/defaults.ts | 1 + packages/vitest/src/types/coverage.ts | 7 +++++++ 4 files changed, 12 insertions(+) diff --git a/packages/coverage-istanbul/src/provider.ts b/packages/coverage-istanbul/src/provider.ts index 84af13763de9..89cc3752a9c2 100644 --- a/packages/coverage-istanbul/src/provider.ts +++ b/packages/coverage-istanbul/src/provider.ts @@ -23,6 +23,7 @@ interface TestExclude { exclude?: string | string[] extension?: string | string[] excludeNodeModules?: boolean + relativePath?: boolean }): { shouldInstrument(filePath: string): boolean glob(cwd: string): Promise @@ -79,6 +80,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co exclude: [...defaultExclude, ...defaultInclude, ...this.options.exclude], excludeNodeModules: true, extension: this.options.extension, + relativePath: !this.options.allowExternal, }) } diff --git a/packages/coverage-v8/src/provider.ts b/packages/coverage-v8/src/provider.ts index 7733007804e3..7b3c28fa3095 100644 --- a/packages/coverage-v8/src/provider.ts +++ b/packages/coverage-v8/src/provider.ts @@ -30,6 +30,7 @@ interface TestExclude { exclude?: string | string[] extension?: string | string[] excludeNodeModules?: boolean + relativePath?: boolean }): { shouldInstrument(filePath: string): boolean glob(cwd: string): Promise @@ -79,6 +80,7 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage exclude: [...defaultExclude, ...defaultInclude, ...this.options.exclude], excludeNodeModules: true, extension: this.options.extension, + relativePath: !this.options.allowExternal, }) } diff --git a/packages/vitest/src/defaults.ts b/packages/vitest/src/defaults.ts index 2f80f9e85321..6250ff788285 100644 --- a/packages/vitest/src/defaults.ts +++ b/packages/vitest/src/defaults.ts @@ -38,6 +38,7 @@ export const coverageConfigDefaults: ResolvedCoverageOptions = { reportOnFailure: false, reporter: [['text', {}], ['html', {}], ['clover', {}], ['json', {}]], extension: ['.js', '.cjs', '.mjs', '.ts', '.mts', '.cts', '.tsx', '.jsx', '.vue', '.svelte'], + allowExternal: false, } export const fakeTimersDefaults = { diff --git a/packages/vitest/src/types/coverage.ts b/packages/vitest/src/types/coverage.ts index 11c14578b0c5..955ba26550cc 100644 --- a/packages/vitest/src/types/coverage.ts +++ b/packages/vitest/src/types/coverage.ts @@ -216,6 +216,13 @@ export interface BaseCoverageOptions { * @default false */ reportOnFailure?: boolean + + /** + * Allow files from outside of your cwd. + * + * @default false + */ + allowExternal?: boolean } export interface CoverageIstanbulOptions extends BaseCoverageOptions { From fe305e03e2da7d8cef5fe00d1512a1051913cea9 Mon Sep 17 00:00:00 2001 From: Stefan Vojvodic Date: Sat, 5 Aug 2023 15:29:03 +0200 Subject: [PATCH 2/9] Add tests --- packages/vitest/src/node/config.ts | 2 +- packages/vitest/src/types/coverage.ts | 1 + .../__snapshots__/custom.report.test.ts.snap | 1 + .../istanbul.report.test.ts.snap | 131 ++++++++ .../__snapshots__/v8.report.test.ts.snap | 297 ++++++++++++++++++ test/coverage-test/external/file.external.ts | 7 + test/coverage-test/external/index.ts | 1 + test/coverage-test/src/callExternal.ts | 5 + .../test/configuration-options.test-d.ts | 1 + test/coverage-test/test/coverage.test.ts | 5 + test/coverage-test/vitest.config.ts | 3 +- 11 files changed, 452 insertions(+), 2 deletions(-) create mode 100644 test/coverage-test/external/file.external.ts create mode 100644 test/coverage-test/external/index.ts create mode 100644 test/coverage-test/src/callExternal.ts diff --git a/packages/vitest/src/node/config.ts b/packages/vitest/src/node/config.ts index 51bf4e999ae7..774b1dda25fc 100644 --- a/packages/vitest/src/node/config.ts +++ b/packages/vitest/src/node/config.ts @@ -273,7 +273,7 @@ export function resolveConfig( ?? resolve(resolved.root, file), ), ) - resolved.coverage.exclude.push(...resolved.setupFiles.map(file => relative(resolved.root, file))) + resolved.coverage.exclude.push(...resolved.setupFiles.map(file => `${resolved.coverage.allowExternal ? '**/' : ''}${relative(resolved.root, file)}`)) resolved.forceRerunTriggers = [ ...resolved.forceRerunTriggers, diff --git a/packages/vitest/src/types/coverage.ts b/packages/vitest/src/types/coverage.ts index 955ba26550cc..5885c3b10d48 100644 --- a/packages/vitest/src/types/coverage.ts +++ b/packages/vitest/src/types/coverage.ts @@ -78,6 +78,7 @@ type FieldsWithDefaultValues = | 'exclude' | 'extension' | 'reportOnFailure' + | 'allowExternal' export type ResolvedCoverageOptions = & CoverageOptions diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap index e3973b7d00c1..2edf07015a17 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap @@ -16,6 +16,7 @@ exports[`custom json report 1`] = ` "/src/Defined.vue", "/src/Hello.vue", "/src/another-setup.ts", + "/src/callExternal.ts", "/src/dynamic-file-esm.ignore.mjs", "/src/dynamic-files.ts", "/src/function-count.ts", 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 9af54d6026c4..aeeb63281d24 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,6 +2,89 @@ exports[`istanbul json report 1`] = ` { + "/external/file.external.ts": { + "b": {}, + "branchMap": {}, + "f": { + "0": 1, + "1": 0, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 34, + "line": 1, + }, + "start": { + "column": 16, + "line": 1, + }, + }, + "loc": { + "end": { + "column": null, + "line": 3, + }, + "start": { + "column": 34, + "line": 1, + }, + }, + "name": "externalCovered", + }, + "1": { + "decl": { + "end": { + "column": 36, + "line": 5, + }, + "start": { + "column": 16, + "line": 5, + }, + }, + "loc": { + "end": { + "column": null, + "line": 7, + }, + "start": { + "column": 36, + "line": 5, + }, + }, + "name": "externalUncovered", + }, + }, + "path": "/external/file.external.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": {}, @@ -519,6 +602,54 @@ exports[`istanbul json report 1`] = ` }, }, }, + "/src/callExternal.ts": { + "b": {}, + "branchMap": {}, + "f": { + "0": 1, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 31, + "line": 3, + }, + "start": { + "column": 16, + "line": 3, + }, + }, + "loc": { + "end": { + "column": null, + "line": 5, + }, + "start": { + "column": 31, + "line": 3, + }, + }, + "name": "callExternal", + }, + }, + "path": "/src/callExternal.ts", + "s": { + "0": 1, + }, + "statementMap": { + "0": { + "end": { + "column": null, + "line": 4, + }, + "start": { + "column": 2, + "line": 4, + }, + }, + }, + }, "/src/dynamic-files.ts": { "b": { "0": [ 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 08c1cc21d00e..8678f922185e 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,6 +2,178 @@ exports[`v8 json report 1`] = ` { + "/external/file.external.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": "externalCovered", + }, + "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": "externalUncovered", + }, + }, + "path": "/external/file.external.ts", + "s": { + "0": 1, + "1": 1, + "2": 1, + "3": 1, + "4": 1, + "5": 0, + "6": 0, + }, + "statementMap": { + "0": { + "end": { + "column": 35, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "1": { + "end": { + "column": 38, + "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": 37, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "5": { + "end": { + "column": 42, + "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": { @@ -1231,6 +1403,131 @@ exports[`v8 json report 1`] = ` }, }, }, + "/src/callExternal.ts": { + "all": false, + "b": { + "0": [ + 1, + ], + }, + "branchMap": { + "0": { + "line": 3, + "loc": { + "end": { + "column": 1, + "line": 5, + }, + "start": { + "column": 7, + "line": 3, + }, + }, + "locations": [ + { + "end": { + "column": 1, + "line": 5, + }, + "start": { + "column": 7, + "line": 3, + }, + }, + ], + "type": "branch", + }, + }, + "f": { + "0": 1, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 1, + "line": 5, + }, + "start": { + "column": 7, + "line": 3, + }, + }, + "line": 3, + "loc": { + "end": { + "column": 1, + "line": 5, + }, + "start": { + "column": 7, + "line": 3, + }, + }, + "name": "callExternal", + }, + }, + "path": "/src/callExternal.ts", + "s": { + "0": 1, + "1": 1, + "2": 1, + "3": 1, + "4": 1, + }, + "statementMap": { + "0": { + "end": { + "column": 45, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "1": { + "end": { + "column": 0, + "line": 2, + }, + "start": { + "column": 0, + "line": 2, + }, + }, + "2": { + "end": { + "column": 32, + "line": 3, + }, + "start": { + "column": 0, + "line": 3, + }, + }, + "3": { + "end": { + "column": 26, + "line": 4, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "4": { + "end": { + "column": 1, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + }, + }, "/src/dynamic-file-cjs.ignore.js": { "all": false, "b": { diff --git a/test/coverage-test/external/file.external.ts b/test/coverage-test/external/file.external.ts new file mode 100644 index 000000000000..d155a4cc032b --- /dev/null +++ b/test/coverage-test/external/file.external.ts @@ -0,0 +1,7 @@ +export function externalCovered() { + return 'This line should be covered' +} + +export function externalUncovered() { + return 'This line should not be covered' +} diff --git a/test/coverage-test/external/index.ts b/test/coverage-test/external/index.ts new file mode 100644 index 000000000000..03bffd41eb5b --- /dev/null +++ b/test/coverage-test/external/index.ts @@ -0,0 +1 @@ +export * from './file.external' diff --git a/test/coverage-test/src/callExternal.ts b/test/coverage-test/src/callExternal.ts new file mode 100644 index 000000000000..a2cd2b36db16 --- /dev/null +++ b/test/coverage-test/src/callExternal.ts @@ -0,0 +1,5 @@ +import { externalCovered } from '../external' + +export function callExternal() { + return externalCovered() +} diff --git a/test/coverage-test/test/configuration-options.test-d.ts b/test/coverage-test/test/configuration-options.test-d.ts index f4b0731a84e3..a0f42c0d4c62 100644 --- a/test/coverage-test/test/configuration-options.test-d.ts +++ b/test/coverage-test/test/configuration-options.test-d.ts @@ -103,6 +103,7 @@ test('provider module', () => { reporter: [['html', {}], ['json', { file: 'string' }]], reportsDirectory: 'string', reportOnFailure: true, + allowExternal: true, } }, clean(_: boolean) {}, diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index 254d4f47902d..a43045805cf8 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -8,6 +8,7 @@ import { useImportEnv } from '../src/importEnv' import { second } from '../src/function-count' import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files' import MultiSuite from '../src/multi-suite' +import { callExternal } from '../src/callExternal' // @ts-expect-error -- untyped virtual file provided by custom plugin import virtualFile2 from '\0vitest-custom-virtual-file-2' @@ -58,6 +59,10 @@ describe('Multiple test suites', () => { }) }) +test('calling external files', () => { + expect(callExternal()).toBe('This line should be covered') +}) + test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => { await runDynamicFileESM() }) diff --git a/test/coverage-test/vitest.config.ts b/test/coverage-test/vitest.config.ts index 258ea5e8bf7b..f3e42c0f2bcf 100644 --- a/test/coverage-test/vitest.config.ts +++ b/test/coverage-test/vitest.config.ts @@ -43,7 +43,8 @@ export default defineConfig({ coverage: { provider: provider as any, customProviderModule: provider === 'custom' ? 'custom-provider' : undefined, - include: ['src/**'], + include: ['**/src/**', '**/external/**.external.ts'], + allowExternal: true, clean: true, all: true, reporter: [ From 2e0fcc2106677fffa06ad08cc53cd66a69e476e4 Mon Sep 17 00:00:00 2001 From: Stefan Vojvodic Date: Sat, 5 Aug 2023 15:40:04 +0200 Subject: [PATCH 3/9] add docs --- docs/config/index.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/config/index.md b/docs/config/index.md index 54922d6b2d8d..fc7e59dda1b4 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -980,6 +980,15 @@ Since Vitest 0.31.0, you can check your coverage report in Vitest UI: check [Vit Generate coverage report even when tests fail. +#### coverage.allowExternal + +- **Type:** `boolean` +- **Default:** `false` +- **Available for providers:** `'v8' | 'istanbul'` +- **CLI:** `--coverage.allowExternal`, `--coverage.allowExternal=false` + +Generate coverage report even when tests fail. + #### coverage.skipFull - **Type:** `boolean` From 43438c6f10133653dec8f8a29e908e638a1516e4 Mon Sep 17 00:00:00 2001 From: Stefan Vojvodic Date: Sat, 12 Aug 2023 12:28:29 +0200 Subject: [PATCH 4/9] Update docs --- docs/config/index.md | 2 +- packages/vitest/src/types/coverage.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/config/index.md b/docs/config/index.md index fc7e59dda1b4..d8fd70d0f8da 100644 --- a/docs/config/index.md +++ b/docs/config/index.md @@ -987,7 +987,7 @@ Generate coverage report even when tests fail. - **Available for providers:** `'v8' | 'istanbul'` - **CLI:** `--coverage.allowExternal`, `--coverage.allowExternal=false` -Generate coverage report even when tests fail. +Collect coverage of files outside the [project `root`](https://vitest.dev/config/#root). #### coverage.skipFull diff --git a/packages/vitest/src/types/coverage.ts b/packages/vitest/src/types/coverage.ts index 5885c3b10d48..e5caf29cd2f9 100644 --- a/packages/vitest/src/types/coverage.ts +++ b/packages/vitest/src/types/coverage.ts @@ -219,7 +219,7 @@ export interface BaseCoverageOptions { reportOnFailure?: boolean /** - * Allow files from outside of your cwd. + * Collect coverage of files outside the project `root`. * * @default false */ From ee2ad6994f43b03ef73c93dda6ab790a54d8058b Mon Sep 17 00:00:00 2001 From: Stefan Vojvodic Date: Sat, 12 Aug 2023 13:00:49 +0200 Subject: [PATCH 5/9] Update tests to include external files --- .../__snapshots__/custom.report.test.ts.snap | 1 - .../istanbul.report.test.ts.snap | 64 +------- .../__snapshots__/v8.report.test.ts.snap | 141 +----------------- .../generic.report.test.ts | 14 +- .../coverage-report-tests/utils.ts | 4 +- test/coverage-test/external/file.external.ts | 7 - test/coverage-test/external/index.ts | 1 - test/coverage-test/src/callExternal.ts | 5 - test/coverage-test/test/coverage.test.ts | 7 +- test/coverage-test/vitest.config.ts | 2 +- test/test-utils/fixtures/math.ts | 7 + 11 files changed, 38 insertions(+), 215 deletions(-) delete mode 100644 test/coverage-test/external/file.external.ts delete mode 100644 test/coverage-test/external/index.ts delete mode 100644 test/coverage-test/src/callExternal.ts create mode 100644 test/test-utils/fixtures/math.ts diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap index 2edf07015a17..e3973b7d00c1 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/custom.report.test.ts.snap @@ -16,7 +16,6 @@ exports[`custom json report 1`] = ` "/src/Defined.vue", "/src/Hello.vue", "/src/another-setup.ts", - "/src/callExternal.ts", "/src/dynamic-file-esm.ignore.mjs", "/src/dynamic-files.ts", "/src/function-count.ts", 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 aeeb63281d24..576b4ef0d1c7 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,7 +2,7 @@ exports[`istanbul json report 1`] = ` { - "/external/file.external.ts": { + "/../test-utils/fixtures/math.ts": { "b": {}, "branchMap": {}, "f": { @@ -13,7 +13,7 @@ exports[`istanbul json report 1`] = ` "0": { "decl": { "end": { - "column": 34, + "column": 20, "line": 1, }, "start": { @@ -27,16 +27,16 @@ exports[`istanbul json report 1`] = ` "line": 3, }, "start": { - "column": 34, + "column": 42, "line": 1, }, }, - "name": "externalCovered", + "name": "sum", }, "1": { "decl": { "end": { - "column": 36, + "column": 25, "line": 5, }, "start": { @@ -50,14 +50,14 @@ exports[`istanbul json report 1`] = ` "line": 7, }, "start": { - "column": 36, + "column": 47, "line": 5, }, }, - "name": "externalUncovered", + "name": "multiply", }, }, - "path": "/external/file.external.ts", + "path": "/../test-utils/fixtures/math.ts", "s": { "0": 1, "1": 0, @@ -602,54 +602,6 @@ exports[`istanbul json report 1`] = ` }, }, }, - "/src/callExternal.ts": { - "b": {}, - "branchMap": {}, - "f": { - "0": 1, - }, - "fnMap": { - "0": { - "decl": { - "end": { - "column": 31, - "line": 3, - }, - "start": { - "column": 16, - "line": 3, - }, - }, - "loc": { - "end": { - "column": null, - "line": 5, - }, - "start": { - "column": 31, - "line": 3, - }, - }, - "name": "callExternal", - }, - }, - "path": "/src/callExternal.ts", - "s": { - "0": 1, - }, - "statementMap": { - "0": { - "end": { - "column": null, - "line": 4, - }, - "start": { - "column": 2, - "line": 4, - }, - }, - }, - }, "/src/dynamic-files.ts": { "b": { "0": [ 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 8678f922185e..68990050c7cf 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,7 +2,7 @@ exports[`v8 json report 1`] = ` { - "/external/file.external.ts": { + "/../test-utils/fixtures/math.ts": { "all": false, "b": { "0": [ @@ -64,7 +64,7 @@ exports[`v8 json report 1`] = ` "line": 1, }, }, - "name": "externalCovered", + "name": "sum", }, "1": { "decl": { @@ -88,10 +88,10 @@ exports[`v8 json report 1`] = ` "line": 5, }, }, - "name": "externalUncovered", + "name": "multiply", }, }, - "path": "/external/file.external.ts", + "path": "/../test-utils/fixtures/math.ts", "s": { "0": 1, "1": 1, @@ -104,7 +104,7 @@ exports[`v8 json report 1`] = ` "statementMap": { "0": { "end": { - "column": 35, + "column": 43, "line": 1, }, "start": { @@ -114,7 +114,7 @@ exports[`v8 json report 1`] = ` }, "1": { "end": { - "column": 38, + "column": 14, "line": 2, }, "start": { @@ -144,7 +144,7 @@ exports[`v8 json report 1`] = ` }, "4": { "end": { - "column": 37, + "column": 48, "line": 5, }, "start": { @@ -154,7 +154,7 @@ exports[`v8 json report 1`] = ` }, "5": { "end": { - "column": 42, + "column": 14, "line": 6, }, "start": { @@ -1403,131 +1403,6 @@ exports[`v8 json report 1`] = ` }, }, }, - "/src/callExternal.ts": { - "all": false, - "b": { - "0": [ - 1, - ], - }, - "branchMap": { - "0": { - "line": 3, - "loc": { - "end": { - "column": 1, - "line": 5, - }, - "start": { - "column": 7, - "line": 3, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 5, - }, - "start": { - "column": 7, - "line": 3, - }, - }, - ], - "type": "branch", - }, - }, - "f": { - "0": 1, - }, - "fnMap": { - "0": { - "decl": { - "end": { - "column": 1, - "line": 5, - }, - "start": { - "column": 7, - "line": 3, - }, - }, - "line": 3, - "loc": { - "end": { - "column": 1, - "line": 5, - }, - "start": { - "column": 7, - "line": 3, - }, - }, - "name": "callExternal", - }, - }, - "path": "/src/callExternal.ts", - "s": { - "0": 1, - "1": 1, - "2": 1, - "3": 1, - "4": 1, - }, - "statementMap": { - "0": { - "end": { - "column": 45, - "line": 1, - }, - "start": { - "column": 0, - "line": 1, - }, - }, - "1": { - "end": { - "column": 0, - "line": 2, - }, - "start": { - "column": 0, - "line": 2, - }, - }, - "2": { - "end": { - "column": 32, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "3": { - "end": { - "column": 26, - "line": 4, - }, - "start": { - "column": 0, - "line": 4, - }, - }, - "4": { - "end": { - "column": 1, - "line": 5, - }, - "start": { - "column": 0, - "line": 5, - }, - }, - }, - }, "/src/dynamic-file-cjs.ignore.js": { "all": false, "b": { 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 350c23c3677e..62ded8002eff 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/src') + const coveragePath = resolve('./coverage/coverage-test/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/src') + const coveragePath = resolve('./coverage/coverage-test/src') const files = fs.readdirSync(coveragePath) expect(files).toContain('untested-file.ts.html') }) test('files should not contain query parameters', () => { - const coveragePath = resolve('./coverage/src/Counter') + const coveragePath = resolve('./coverage/coverage-test/src/Counter') const files = fs.readdirSync(coveragePath) expect(files).toContain('index.html') @@ -48,7 +48,7 @@ test('files should not contain query parameters', () => { }) test('file using import.meta.env is included in report', async () => { - const coveragePath = resolve('./coverage/src') + const coveragePath = resolve('./coverage/coverage-test/src') const files = fs.readdirSync(coveragePath) expect(files).toContain('importEnv.ts.html') @@ -58,10 +58,10 @@ test('files should not contain a setup file', () => { const coveragePath = resolve('./coverage') const files = fs.readdirSync(coveragePath) - expect(files).not.toContain('coverage-test') + // expect(files).not.toContain('coverage-test') expect(files).not.toContain('setup.ts.html') - const coverageSrcPath = resolve('./coverage/src') + const coverageSrcPath = resolve('./coverage/coverage-test/src') const srcFiles = fs.readdirSync(coverageSrcPath) expect(srcFiles).not.toContain('another-setup.ts.html') @@ -104,7 +104,7 @@ test('coverage provider does not conflict with built-in reporter\'s outputFile', test('virtual files should be excluded', () => { const files = fs.readdirSync(resolve('./coverage')) - const srcFiles = fs.readdirSync(resolve('./coverage/src')) + const srcFiles = fs.readdirSync(resolve('./coverage/coverage-test/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 d06aa62cc71f..3833379bd7a0 100644 --- a/test/coverage-test/coverage-report-tests/utils.ts +++ b/test/coverage-test/coverage-report-tests/utils.ts @@ -31,5 +31,7 @@ export async function readCoverageJson() { } export function normalizeFilename(filename: string) { - return normalize(filename).replace(normalize(process.cwd()), '') + return normalize(filename) + .replace(normalize(process.cwd()), '') + .replace(normalize(process.cwd().replace('coverage-test', '')), '/../') } diff --git a/test/coverage-test/external/file.external.ts b/test/coverage-test/external/file.external.ts deleted file mode 100644 index d155a4cc032b..000000000000 --- a/test/coverage-test/external/file.external.ts +++ /dev/null @@ -1,7 +0,0 @@ -export function externalCovered() { - return 'This line should be covered' -} - -export function externalUncovered() { - return 'This line should not be covered' -} diff --git a/test/coverage-test/external/index.ts b/test/coverage-test/external/index.ts deleted file mode 100644 index 03bffd41eb5b..000000000000 --- a/test/coverage-test/external/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './file.external' diff --git a/test/coverage-test/src/callExternal.ts b/test/coverage-test/src/callExternal.ts deleted file mode 100644 index a2cd2b36db16..000000000000 --- a/test/coverage-test/src/callExternal.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { externalCovered } from '../external' - -export function callExternal() { - return externalCovered() -} diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index a43045805cf8..f284a750a433 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -3,12 +3,13 @@ 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' import { runDynamicFileCJS, runDynamicFileESM } from '../src/dynamic-files' import MultiSuite from '../src/multi-suite' -import { callExternal } from '../src/callExternal' // @ts-expect-error -- untyped virtual file provided by custom plugin import virtualFile2 from '\0vitest-custom-virtual-file-2' @@ -59,8 +60,8 @@ describe('Multiple test suites', () => { }) }) -test('calling external files', () => { - expect(callExternal()).toBe('This line should be covered') +test('calling files outside project root', () => { + expect(ExternalMath.sum(2, 3)).toBe(5) }) test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => { diff --git a/test/coverage-test/vitest.config.ts b/test/coverage-test/vitest.config.ts index f3e42c0f2bcf..42fb266ee1d5 100644 --- a/test/coverage-test/vitest.config.ts +++ b/test/coverage-test/vitest.config.ts @@ -43,7 +43,7 @@ export default defineConfig({ coverage: { provider: provider as any, customProviderModule: provider === 'custom' ? 'custom-provider' : undefined, - include: ['**/src/**', '**/external/**.external.ts'], + include: ['**/src/**', '**/test-utils/fixtures/**'], allowExternal: true, clean: true, all: true, diff --git a/test/test-utils/fixtures/math.ts b/test/test-utils/fixtures/math.ts new file mode 100644 index 000000000000..0859841e5814 --- /dev/null +++ b/test/test-utils/fixtures/math.ts @@ -0,0 +1,7 @@ +export function sum(a: number, b: number) { + return a + b +} + +export function multiply(a: number, b: number) { + return a * b +} From 4b4e4e8d8109e617e6e1863b11493fad5f63dd71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 15 Aug 2023 19:13:00 +0300 Subject: [PATCH 6/9] test: update paths --- test/coverage-test/coverage-report-tests/generic.report.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 62ded8002eff..719d62412313 100644 --- a/test/coverage-test/coverage-report-tests/generic.report.test.ts +++ b/test/coverage-test/coverage-report-tests/generic.report.test.ts @@ -55,7 +55,7 @@ test('file using import.meta.env is included in report', async () => { }) test('files should not contain a setup file', () => { - const coveragePath = resolve('./coverage') + const coveragePath = resolve('./coverage/coverage-test') const files = fs.readdirSync(coveragePath) // expect(files).not.toContain('coverage-test') From 43d65677c8cb9ae9ce53e2b4e4e0ea855f2c4080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 15 Aug 2023 19:13:08 +0300 Subject: [PATCH 7/9] test: update paths --- test/coverage-test/coverage-report-tests/generic.report.test.ts | 1 - 1 file changed, 1 deletion(-) 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 719d62412313..f1c1c8e38c4b 100644 --- a/test/coverage-test/coverage-report-tests/generic.report.test.ts +++ b/test/coverage-test/coverage-report-tests/generic.report.test.ts @@ -58,7 +58,6 @@ test('files should not contain a setup file', () => { const coveragePath = resolve('./coverage/coverage-test') 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') From 0ff798a2f240be75ef01e35a4b4c94051c4eab4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Tue, 15 Aug 2023 19:13:16 +0300 Subject: [PATCH 8/9] test: update paths --- test/coverage-test/coverage-report-tests/generic.report.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f1c1c8e38c4b..b84b2ed7e81e 100644 --- a/test/coverage-test/coverage-report-tests/generic.report.test.ts +++ b/test/coverage-test/coverage-report-tests/generic.report.test.ts @@ -102,7 +102,7 @@ test('coverage provider does not conflict with built-in reporter\'s outputFile', }) test('virtual files should be excluded', () => { - const files = fs.readdirSync(resolve('./coverage')) + const files = fs.readdirSync(resolve('./coverage/coverage-test')) const srcFiles = fs.readdirSync(resolve('./coverage/coverage-test/src')) for (const file of [...files, ...srcFiles]) { From 25cf08b5ba6cc5585485b9938cd3a09210dfb915 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Wed, 16 Aug 2023 18:54:06 +0300 Subject: [PATCH 9/9] test(coverage): option specific test setup --- .../istanbul.report.test.ts.snap | 83 --------- .../__snapshots__/v8.report.test.ts.snap | 172 ------------------ .../allow-external.test.ts | 20 ++ .../generic.report.test.ts | 17 +- .../coverage-report-tests/utils.ts | 4 +- .../option-tests/allow-external.test.ts | 12 ++ test/coverage-test/package.json | 3 +- test/coverage-test/test/coverage.test.ts | 6 - test/coverage-test/testing-options.mjs | 69 +++++++ test/coverage-test/vitest.config.ts | 3 +- 10 files changed, 114 insertions(+), 275 deletions(-) create mode 100644 test/coverage-test/coverage-report-tests/allow-external.test.ts create mode 100644 test/coverage-test/option-tests/allow-external.test.ts create mode 100644 test/coverage-test/testing-options.mjs 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: [