From 52cb10cd7dd80ff1aa9f354e86cced698d124cb7 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Mon, 24 Apr 2023 16:49:04 +0300 Subject: [PATCH] fix(coverage): c8 to ignore vite's generated helpers --- packages/coverage-c8/package.json | 2 + packages/coverage-c8/src/provider.ts | 31 +- pnpm-lock.yaml | 6 + .../__snapshots__/c8.report.test.ts.snap | 610 ++---------------- .../generic.report.test.ts | 2 +- 5 files changed, 76 insertions(+), 575 deletions(-) diff --git a/packages/coverage-c8/package.json b/packages/coverage-c8/package.json index 959acf235654..120f0894c256 100644 --- a/packages/coverage-c8/package.json +++ b/packages/coverage-c8/package.json @@ -45,7 +45,9 @@ "vitest": ">=0.30.0 <1" }, "dependencies": { + "@ampproject/remapping": "^2.2.0", "c8": "^7.13.0", + "magic-string": "^0.30.0", "picocolors": "^1.0.0", "std-env": "^3.3.2" }, diff --git a/packages/coverage-c8/src/provider.ts b/packages/coverage-c8/src/provider.ts index d37e44661087..bc9019f5dafd 100644 --- a/packages/coverage-c8/src/provider.ts +++ b/packages/coverage-c8/src/provider.ts @@ -1,6 +1,8 @@ import { existsSync, promises as fs } from 'node:fs' import _url from 'node:url' import type { Profiler } from 'node:inspector' +import MagicString from 'magic-string' +import remapping from '@ampproject/remapping' import { extname, resolve } from 'pathe' import c from 'picocolors' import { provider } from 'std-env' @@ -18,6 +20,9 @@ import { checkCoverages } from 'c8/lib/commands/check-coverage.js' type Options = ResolvedCoverageOptions<'c8'> +// Note that this needs to match the line ending as well +const VITE_EXPORTS_LINE_PATTERN = /Object\.defineProperty\(__vite_ssr_exports__.*\n/g + export class C8CoverageProvider extends BaseCoverageProvider implements CoverageProvider { name = 'c8' @@ -169,7 +174,7 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage return { sourceMap: { - sourcemap: data.map, + sourcemap: removeViteHelpersFromSourceMaps(data.source, data.map), }, source: Array(offset).fill('.').join('') + data.source, } @@ -193,3 +198,27 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage } } } + +/** + * Remove generated code from the source maps: + * - Vite's export helpers: e.g. `Object.defineProperty(__vite_ssr_exports__, "sum", { enumerable: true, configurable: true, get(){ return sum }});` + */ +function removeViteHelpersFromSourceMaps(source: string | undefined, map: EncodedSourceMap) { + if (!source || !source.match(VITE_EXPORTS_LINE_PATTERN)) + return map + + const sourceWithoutHelpers = new MagicString(source) + sourceWithoutHelpers.replaceAll(VITE_EXPORTS_LINE_PATTERN, '\n') + + const mapWithoutHelpers = sourceWithoutHelpers.generateMap({ + hires: true, + }) + + // A merged source map where the first one excludes helpers + const combinedMap = remapping( + [{ ...mapWithoutHelpers, version: 3 }, map], + () => null, + ) + + return combinedMap +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ee5ae40273f5..ef85f7dea9f5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -873,9 +873,15 @@ importers: packages/coverage-c8: dependencies: + '@ampproject/remapping': + specifier: ^2.2.0 + version: 2.2.0 c8: specifier: ^7.13.0 version: 7.13.0 + magic-string: + specifier: ^0.30.0 + version: 0.30.0 picocolors: specifier: ^1.0.0 version: 1.0.0 diff --git a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap index 617734cbe493..e55e5fbacbf3 100644 --- a/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap +++ b/test/coverage-test/coverage-report-tests/__snapshots__/c8.report.test.ts.snap @@ -695,68 +695,10 @@ exports[`c8 json report 1`] = ` }, "/src/Counter/index.ts": { "all": false, - "b": { - "0": [ - 1, - ], - }, - "branchMap": { - "0": { - "line": 4, - "loc": { - "end": { - "column": 39, - "line": 4, - }, - "start": { - "column": 27, - "line": 4, - }, - }, - "locations": [ - { - "end": { - "column": 39, - "line": 4, - }, - "start": { - "column": 27, - "line": 4, - }, - }, - ], - "type": "branch", - }, - }, - "f": { - "0": 1, - }, - "fnMap": { - "0": { - "decl": { - "end": { - "column": 39, - "line": 4, - }, - "start": { - "column": 27, - "line": 4, - }, - }, - "line": 4, - "loc": { - "end": { - "column": 39, - "line": 4, - }, - "start": { - "column": 27, - "line": 4, - }, - }, - "name": "get", - }, - }, + "b": {}, + "branchMap": {}, + "f": {}, + "fnMap": {}, "path": "/src/Counter/index.ts", "s": { "0": 1, @@ -1312,9 +1254,6 @@ exports[`c8 json report 1`] = ` "2": [ 1, ], - "3": [ - 1, - ], }, "branchMap": { "0": { @@ -1370,32 +1309,6 @@ exports[`c8 json report 1`] = ` "type": "branch", }, "2": { - "line": 21, - "loc": { - "end": { - "column": 1, - "line": 21, - }, - "start": { - "column": 0, - "line": 21, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 21, - }, - "start": { - "column": 0, - "line": 21, - }, - }, - ], - "type": "branch", - }, - "3": { "line": 34, "loc": { "end": { @@ -1425,11 +1338,9 @@ exports[`c8 json report 1`] = ` "f": { "0": 1, "1": 1, - "2": 1, + "2": 0, "3": 0, - "4": 0, - "5": 0, - "6": 1, + "4": 1, }, "fnMap": { "0": { @@ -1481,30 +1392,6 @@ exports[`c8 json report 1`] = ` "name": "second", }, "2": { - "decl": { - "end": { - "column": 1, - "line": 21, - }, - "start": { - "column": 0, - "line": 21, - }, - }, - "line": 21, - "loc": { - "end": { - "column": 1, - "line": 21, - }, - "start": { - "column": 0, - "line": 21, - }, - }, - "name": "get", - }, - "3": { "decl": { "end": { "column": 1, @@ -1528,31 +1415,7 @@ exports[`c8 json report 1`] = ` }, "name": "third", }, - "4": { - "decl": { - "end": { - "column": 1, - "line": 26, - }, - "start": { - "column": 0, - "line": 26, - }, - }, - "line": 26, - "loc": { - "end": { - "column": 1, - "line": 26, - }, - "start": { - "column": 0, - "line": 26, - }, - }, - "name": "get", - }, - "5": { + "3": { "decl": { "end": { "column": 1, @@ -1576,7 +1439,7 @@ exports[`c8 json report 1`] = ` }, "name": "fourth", }, - "6": { + "4": { "decl": { "end": { "column": 1, @@ -2009,9 +1872,6 @@ exports[`c8 json report 1`] = ` "0": [ 1, ], - "1": [ - 1, - ], }, "branchMap": { "0": { @@ -2040,36 +1900,9 @@ exports[`c8 json report 1`] = ` ], "type": "branch", }, - "1": { - "line": 8, - "loc": { - "end": { - "column": 1, - "line": 8, - }, - "start": { - "column": 0, - "line": 8, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 8, - }, - "start": { - "column": 0, - "line": 8, - }, - }, - ], - "type": "branch", - }, }, "f": { "0": 1, - "1": 1, }, "fnMap": { "0": { @@ -2096,30 +1929,6 @@ exports[`c8 json report 1`] = ` }, "name": "implicitElse", }, - "1": { - "decl": { - "end": { - "column": 1, - "line": 8, - }, - "start": { - "column": 0, - "line": 8, - }, - }, - "line": 8, - "loc": { - "end": { - "column": 1, - "line": 8, - }, - "start": { - "column": 0, - "line": 8, - }, - }, - "name": "get", - }, }, "path": "/src/implicitElse.ts", "s": { @@ -2221,9 +2030,6 @@ exports[`c8 json report 1`] = ` "0": [ 1, ], - "1": [ - 1, - ], }, "branchMap": { "0": { @@ -2252,36 +2058,9 @@ exports[`c8 json report 1`] = ` ], "type": "branch", }, - "1": { - "line": 3, - "loc": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - ], - "type": "branch", - }, }, "f": { "0": 1, - "1": 1, }, "fnMap": { "0": { @@ -2308,30 +2087,6 @@ exports[`c8 json report 1`] = ` }, "name": "useImportEnv", }, - "1": { - "decl": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "line": 3, - "loc": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "name": "get", - }, }, "path": "/src/importEnv.ts", "s": { @@ -2378,9 +2133,6 @@ exports[`c8 json report 1`] = ` "0": [ 1, ], - "1": [ - 1, - ], }, "branchMap": { "0": { @@ -2409,47 +2161,20 @@ exports[`c8 json report 1`] = ` ], "type": "branch", }, - "1": { - "line": 7, - "loc": { + }, + "f": { + "0": 1, + }, + "fnMap": { + "0": { + "decl": { "end": { "column": 1, "line": 7, }, "start": { - "column": 0, - "line": 7, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - ], - "type": "branch", - }, - }, - "f": { - "0": 1, - "1": 1, - }, - "fnMap": { - "0": { - "decl": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 7, - "line": 5, + "column": 7, + "line": 5, }, }, "line": 5, @@ -2465,30 +2190,6 @@ exports[`c8 json report 1`] = ` }, "name": "pythagoras", }, - "1": { - "decl": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - "line": 7, - "loc": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - "name": "get", - }, }, "path": "/src/index.mts", "s": { @@ -3013,23 +2714,14 @@ exports[`c8 json report 1`] = ` 1, ], "1": [ - 1, - ], - "2": [ - 2, - ], - "3": [ 2, ], - "4": [ + "2": [ 1, ], - "5": [ + "3": [ 0, ], - "6": [ - 1, - ], }, "branchMap": { "0": { @@ -3059,32 +2751,6 @@ exports[`c8 json report 1`] = ` "type": "branch", }, "1": { - "line": 3, - "loc": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - ], - "type": "branch", - }, - "2": { "line": 5, "loc": { "end": { @@ -3110,33 +2776,7 @@ exports[`c8 json report 1`] = ` ], "type": "branch", }, - "3": { - "line": 7, - "loc": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - ], - "type": "branch", - }, - "4": { + "2": { "line": 14, "loc": { "end": { @@ -3162,7 +2802,7 @@ exports[`c8 json report 1`] = ` ], "type": "branch", }, - "5": { + "3": { "line": 16, "loc": { "end": { @@ -3188,46 +2828,14 @@ exports[`c8 json report 1`] = ` ], "type": "branch", }, - "6": { - "line": 19, - "loc": { - "end": { - "column": 1, - "line": 19, - }, - "start": { - "column": 0, - "line": 19, - }, - }, - "locations": [ - { - "end": { - "column": 1, - "line": 19, - }, - "start": { - "column": 0, - "line": 19, - }, - }, - ], - "type": "branch", - }, }, "f": { "0": 1, - "1": 1, - "10": 1, - "11": 1, - "2": 2, - "3": 2, + "1": 2, + "2": 0, + "3": 1, "4": 0, - "5": 0, - "6": 1, - "7": 1, - "8": 0, - "9": 0, + "5": 1, }, "fnMap": { "0": { @@ -3255,78 +2863,6 @@ exports[`c8 json report 1`] = ` "name": "add", }, "1": { - "decl": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "line": 3, - "loc": { - "end": { - "column": 1, - "line": 3, - }, - "start": { - "column": 0, - "line": 3, - }, - }, - "name": "get", - }, - "10": { - "decl": { - "end": { - "column": 1, - "line": 30, - }, - "start": { - "column": 7, - "line": 28, - }, - }, - "line": 28, - "loc": { - "end": { - "column": 1, - "line": 30, - }, - "start": { - "column": 7, - "line": 28, - }, - }, - "name": "ignoredFunction", - }, - "11": { - "decl": { - "end": { - "column": 1, - "line": 30, - }, - "start": { - "column": 0, - "line": 30, - }, - }, - "line": 30, - "loc": { - "end": { - "column": 1, - "line": 30, - }, - "start": { - "column": 0, - "line": 30, - }, - }, - "name": "get", - }, - "2": { "decl": { "end": { "column": 1, @@ -3350,31 +2886,7 @@ exports[`c8 json report 1`] = ` }, "name": "multiply", }, - "3": { - "decl": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - "line": 7, - "loc": { - "end": { - "column": 1, - "line": 7, - }, - "start": { - "column": 0, - "line": 7, - }, - }, - "name": "get", - }, - "4": { + "2": { "decl": { "end": { "column": 1, @@ -3398,31 +2910,7 @@ exports[`c8 json report 1`] = ` }, "name": "divide", }, - "5": { - "decl": { - "end": { - "column": 1, - "line": 12, - }, - "start": { - "column": 0, - "line": 12, - }, - }, - "line": 12, - "loc": { - "end": { - "column": 1, - "line": 12, - }, - "start": { - "column": 0, - "line": 12, - }, - }, - "name": "get", - }, - "6": { + "3": { "decl": { "end": { "column": 1, @@ -3446,31 +2934,7 @@ exports[`c8 json report 1`] = ` }, "name": "sqrt", }, - "7": { - "decl": { - "end": { - "column": 1, - "line": 19, - }, - "start": { - "column": 0, - "line": 19, - }, - }, - "line": 19, - "loc": { - "end": { - "column": 1, - "line": 19, - }, - "start": { - "column": 0, - "line": 19, - }, - }, - "name": "get", - }, - "8": { + "4": { "decl": { "end": { "column": 1, @@ -3494,29 +2958,29 @@ exports[`c8 json report 1`] = ` }, "name": "run", }, - "9": { + "5": { "decl": { "end": { "column": 1, - "line": 24, + "line": 30, }, "start": { - "column": 0, - "line": 24, + "column": 7, + "line": 28, }, }, - "line": 24, + "line": 28, "loc": { "end": { "column": 1, - "line": 24, + "line": 30, }, "start": { - "column": 0, - "line": 24, + "column": 7, + "line": 28, }, }, - "name": "get", + "name": "ignoredFunction", }, }, "path": "/src/utils.ts", 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 57b23861e73e..705a56b7b6c2 100644 --- a/test/coverage-test/coverage-report-tests/generic.report.test.ts +++ b/test/coverage-test/coverage-report-tests/generic.report.test.ts @@ -84,7 +84,7 @@ test('thresholdAutoUpdate updates thresholds', async () => { fs.writeFileSync(configFilename, updatedConfig) }) -test.skip('function count is correct', async () => { +test('function count is correct', async () => { const coverageJson = await readCoverageJson() const coverageMap = libCoverage.createCoverageMap(coverageJson as any) const fileCoverage = coverageMap.fileCoverageFor('/src/function-count.ts')