From 5de9af260c0e5aab7eeebb55bce00c0c5816c04e Mon Sep 17 00:00:00 2001 From: Elby Basolis Date: Mon, 31 Jul 2023 10:56:53 -0400 Subject: [PATCH] fix(browser): multi-suite coverage (#3806) --- packages/browser/src/client/runner.ts | 4 +- .../__snapshots__/custom.report.test.ts.snap | 1 + .../istanbul.report.test.ts.snap | 83 +++++++ .../__snapshots__/v8.report.test.ts.snap | 223 ++++++++++++++++++ .../istanbul.report.test.ts | 13 + .../coverage-report-tests/v8.report.test.ts | 13 + test/coverage-test/src/multi-suite.ts | 9 + test/coverage-test/test/coverage.test.ts | 20 +- 8 files changed, 363 insertions(+), 3 deletions(-) create mode 100644 test/coverage-test/src/multi-suite.ts diff --git a/packages/browser/src/client/runner.ts b/packages/browser/src/client/runner.ts index f9319c8bb0e1..ff36a5a7c7f4 100644 --- a/packages/browser/src/client/runner.ts +++ b/packages/browser/src/client/runner.ts @@ -39,8 +39,8 @@ export function createBrowserRunner(original: any, coverageModule: CoverageHandl } } - async onAfterRunSuite() { - await super.onAfterRunSuite?.() + async onAfterRun() { + await super.onAfterRun?.() const coverage = await coverageModule?.takeCoverage?.() await rpc().onAfterSuiteRun({ coverage }) } 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 781868ce6dea..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 @@ -22,6 +22,7 @@ exports[`custom json report 1`] = ` "/src/implicitElse.ts", "/src/importEnv.ts", "/src/index.mts", + "/src/multi-suite.ts", "/src/utils.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 f29b3e75c6ac..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 @@ -1342,6 +1342,89 @@ exports[`istanbul json report 1`] = ` }, }, }, + "/src/multi-suite.ts": { + "b": {}, + "branchMap": {}, + "f": { + "0": 1, + "1": 1, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 8, + "line": 2, + }, + "start": { + "column": 2, + "line": 2, + }, + }, + "loc": { + "end": { + "column": null, + "line": 4, + }, + "start": { + "column": 21, + "line": 2, + }, + }, + "name": "(anonymous_0)", + }, + "1": { + "decl": { + "end": { + "column": 8, + "line": 6, + }, + "start": { + "column": 2, + "line": 6, + }, + }, + "loc": { + "end": { + "column": null, + "line": 8, + }, + "start": { + "column": 21, + "line": 6, + }, + }, + "name": "(anonymous_1)", + }, + }, + "path": "/src/multi-suite.ts", + "s": { + "0": 1, + "1": 1, + }, + "statementMap": { + "0": { + "end": { + "column": null, + "line": 3, + }, + "start": { + "column": 4, + "line": 3, + }, + }, + "1": { + "end": { + "column": null, + "line": 7, + }, + "start": { + "column": 4, + "line": 7, + }, + }, + }, + }, "/src/untested-file.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 1d002a4ef9c3..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 @@ -3305,6 +3305,229 @@ exports[`v8 json report 1`] = ` }, }, }, + "/src/multi-suite.ts": { + "all": false, + "b": { + "0": [ + 1, + ], + "1": [ + 1, + ], + }, + "branchMap": { + "0": { + "line": 2, + "loc": { + "end": { + "column": 4, + "line": 4, + }, + "start": { + "column": 2, + "line": 2, + }, + }, + "locations": [ + { + "end": { + "column": 4, + "line": 4, + }, + "start": { + "column": 2, + "line": 2, + }, + }, + ], + "type": "branch", + }, + "1": { + "line": 6, + "loc": { + "end": { + "column": 4, + "line": 8, + }, + "start": { + "column": 2, + "line": 6, + }, + }, + "locations": [ + { + "end": { + "column": 4, + "line": 8, + }, + "start": { + "column": 2, + "line": 6, + }, + }, + ], + "type": "branch", + }, + }, + "f": { + "0": 1, + "1": 1, + }, + "fnMap": { + "0": { + "decl": { + "end": { + "column": 4, + "line": 4, + }, + "start": { + "column": 2, + "line": 2, + }, + }, + "line": 2, + "loc": { + "end": { + "column": 4, + "line": 4, + }, + "start": { + "column": 2, + "line": 2, + }, + }, + "name": "func1", + }, + "1": { + "decl": { + "end": { + "column": 4, + "line": 8, + }, + "start": { + "column": 2, + "line": 6, + }, + }, + "line": 6, + "loc": { + "end": { + "column": 4, + "line": 8, + }, + "start": { + "column": 2, + "line": 6, + }, + }, + "name": "func2", + }, + }, + "path": "/src/multi-suite.ts", + "s": { + "0": 1, + "1": 1, + "2": 1, + "3": 1, + "4": 1, + "5": 1, + "6": 1, + "7": 1, + "8": 1, + }, + "statementMap": { + "0": { + "end": { + "column": 16, + "line": 1, + }, + "start": { + "column": 0, + "line": 1, + }, + }, + "1": { + "end": { + "column": 22, + "line": 2, + }, + "start": { + "column": 0, + "line": 2, + }, + }, + "2": { + "end": { + "column": 15, + "line": 3, + }, + "start": { + "column": 0, + "line": 3, + }, + }, + "3": { + "end": { + "column": 4, + "line": 4, + }, + "start": { + "column": 0, + "line": 4, + }, + }, + "4": { + "end": { + "column": 0, + "line": 5, + }, + "start": { + "column": 0, + "line": 5, + }, + }, + "5": { + "end": { + "column": 22, + "line": 6, + }, + "start": { + "column": 0, + "line": 6, + }, + }, + "6": { + "end": { + "column": 15, + "line": 7, + }, + "start": { + "column": 0, + "line": 7, + }, + }, + "7": { + "end": { + "column": 4, + "line": 8, + }, + "start": { + "column": 0, + "line": 8, + }, + }, + "8": { + "end": { + "column": 1, + "line": 9, + }, + "start": { + "column": 0, + "line": 9, + }, + }, + }, + }, "/src/untested-file.ts": { "all": true, "b": { diff --git a/test/coverage-test/coverage-report-tests/istanbul.report.test.ts b/test/coverage-test/coverage-report-tests/istanbul.report.test.ts index 097de24740c3..38402f18e75e 100644 --- a/test/coverage-test/coverage-report-tests/istanbul.report.test.ts +++ b/test/coverage-test/coverage-report-tests/istanbul.report.test.ts @@ -37,3 +37,16 @@ test('ignored code is excluded from the report', async () => { const utils = await import('../src/utils') expect(utils[functionName]).toBeTypeOf('function') }) + +test('tests with multiple suites are covered', async () => { + const coverageMap = await readCoverageJson() + + const filename = '/src/multi-suite.ts' + const fileCoverage = coverageMap[filename] + + // Assert that all functions are covered + expect(fileCoverage.f).toMatchObject({ + 0: 1, + 1: 1, + }) +}) diff --git a/test/coverage-test/coverage-report-tests/v8.report.test.ts b/test/coverage-test/coverage-report-tests/v8.report.test.ts index 30d8c64873ce..68be7adc6fa5 100644 --- a/test/coverage-test/coverage-report-tests/v8.report.test.ts +++ b/test/coverage-test/coverage-report-tests/v8.report.test.ts @@ -29,3 +29,16 @@ test('ignored code is marked as covered in the report', async () => { const utils = await import('../src/utils') expect(utils[functionName]).toBeTypeOf('function') }) + +test('tests with multiple suites are covered', async () => { + const coverageMap = await readCoverageJson() + + const filename = '/src/multi-suite.ts' + const fileCoverage = coverageMap[filename] + + // Assert that all functions are covered + expect(fileCoverage.f).toMatchObject({ + 0: 1, + 1: 1, + }) +}) diff --git a/test/coverage-test/src/multi-suite.ts b/test/coverage-test/src/multi-suite.ts new file mode 100644 index 000000000000..1f10f5010c12 --- /dev/null +++ b/test/coverage-test/src/multi-suite.ts @@ -0,0 +1,9 @@ +export default { + func1(data: any[]) { + return data + }, + + func2(data: any[]) { + return data + }, +} diff --git a/test/coverage-test/test/coverage.test.ts b/test/coverage-test/test/coverage.test.ts index fcf719945215..254d4f47902d 100644 --- a/test/coverage-test/test/coverage.test.ts +++ b/test/coverage-test/test/coverage.test.ts @@ -1,4 +1,4 @@ -import { expect, test } from 'vitest' +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' @@ -7,6 +7,7 @@ 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' // @ts-expect-error -- untyped virtual file provided by custom plugin import virtualFile2 from '\0vitest-custom-virtual-file-2' @@ -40,6 +41,23 @@ test('cover function counts', () => { expect(second()).toBe(2) }) +describe('Multiple test suites', () => { + describe('func1()', () => { + test('func1', () => { + const data = ['a', 'b'] + const val = MultiSuite.func1(data) + expect(val).toEqual(data) + }) + }) + describe('func2()', () => { + test('func2', () => { + const data = ['c', 'd'] + const val = MultiSuite.func2(data) + expect(val).toEqual(data) + }) + }) +}) + test.skipIf(skipDynamicFiles)('run dynamic ESM file', async () => { await runDynamicFileESM() })