From b97a60ff1bfeea551840fe80b1d3ae0111b37148 Mon Sep 17 00:00:00 2001 From: AriPerkkio Date: Mon, 24 Apr 2023 11:11:40 +0300 Subject: [PATCH] fix(coverage): c8 to ignore vite's generated helpers --- packages/coverage-c8/src/provider.ts | 26 +- .../__snapshots__/c8.report.test.ts.snap | 610 ++---------------- .../generic.report.test.ts | 2 +- 3 files changed, 63 insertions(+), 575 deletions(-) diff --git a/packages/coverage-c8/src/provider.ts b/packages/coverage-c8/src/provider.ts index d37e44661087..10862c982011 100644 --- a/packages/coverage-c8/src/provider.ts +++ b/packages/coverage-c8/src/provider.ts @@ -169,7 +169,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 +193,27 @@ export class C8CoverageProvider extends BaseCoverageProvider implements Coverage } } } + +function removeViteHelpersFromSourceMaps(source: string | undefined, map: EncodedSourceMap): EncodedSourceMap { + if (!source?.includes('__vite_ssr_exports__')) + return map + + const exportIndexes = source.split('\n').map((line, index) => { + if (/Object\.defineProperty\(__vite_ssr_exports__/.test(line)) + return index + return -1 + }).filter(index => index !== -1) + + if (exportIndexes.length === 0) + return map + + const mappings = map.mappings.split(';') + exportIndexes.forEach((index) => { + mappings[index] = '' + }) + + return { + ...map, + mappings: mappings.join(';'), + } +} 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')