Skip to content

Commit e6a18c7

Browse files
authoredDec 10, 2022
fix(coverage-istanbul): clear coverage map after use (#2466)
* test: add coverage snapshots * fix(coverage-istanbul): clear coverage map after use - Fixes issues where statement counts were duplicated
1 parent 286e9cf commit e6a18c7

File tree

3 files changed

+1613
-2
lines changed

3 files changed

+1613
-2
lines changed
 

‎packages/coverage-istanbul/src/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,11 @@ export async function getProvider() {
77

88
export function takeCoverage() {
99
// @ts-expect-error -- untyped global
10-
return globalThis[COVERAGE_STORE_KEY]
10+
const coverage = globalThis[COVERAGE_STORE_KEY]
11+
12+
// Reset coverage map to prevent duplicate results if this is called twice in row
13+
// @ts-expect-error -- untyped global
14+
globalThis[COVERAGE_STORE_KEY] = {}
15+
16+
return coverage
1117
}

‎test/coverage-test/coverage-test/__snapshots__/coverage.istanbul.test.ts.snap

+1,577
Large diffs are not rendered by default.

‎test/coverage-test/coverage-test/coverage.istanbul.test.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import fs from 'fs'
22
import { normalize, resolve } from 'pathe'
33
import { expect, test } from 'vitest'
44

5+
interface CoverageFinalJson {
6+
default: {
7+
[filename: string]: {
8+
path: string
9+
b: Record<string, number[]>
10+
// ... and more unrelated keys
11+
}
12+
}
13+
}
14+
515
test('istanbul html report', async () => {
616
const coveragePath = resolve('./coverage/src')
717
const files = fs.readdirSync(coveragePath)
@@ -23,6 +33,20 @@ test('istanbul lcov report', async () => {
2333
expect(lcovReportFiles).toContain('index.html')
2434
})
2535

36+
test('istanbul json report', async () => {
37+
// @ts-expect-error -- generated file
38+
const { default: jsonReport } = await import('./coverage/coverage-final.json') as CoverageFinalJson
39+
40+
const normalizedReport: CoverageFinalJson['default'] = {}
41+
42+
for (const [filename, coverage] of Object.entries(jsonReport)) {
43+
coverage.path = normalizeFilename(coverage.path)
44+
normalizedReport[normalizeFilename(filename)] = coverage
45+
}
46+
47+
expect(normalizedReport).toMatchSnapshot()
48+
})
49+
2650
test('all includes untested files', () => {
2751
const coveragePath = resolve('./coverage/src')
2852
const files = fs.readdirSync(coveragePath)
@@ -42,7 +66,7 @@ test('files should not contain query parameters', () => {
4266

4367
test('implicit else is included in branch count', async () => {
4468
// @ts-expect-error -- generated file
45-
const { default: coverageMap } = await import('./coverage/coverage-final.json')
69+
const { default: coverageMap } = await import('./coverage/coverage-final.json') as CoverageFinalJson
4670

4771
const filename = normalize(resolve('./src/implicitElse.ts'))
4872
const fileCoverage = coverageMap[filename]
@@ -57,3 +81,7 @@ test('file using import.meta.env is included in report', async () => {
5781

5882
expect(files).toContain('importEnv.ts.html')
5983
})
84+
85+
function normalizeFilename(filename: string) {
86+
return filename.replace(normalize(process.cwd()), '<root>')
87+
}

0 commit comments

Comments
 (0)
Please sign in to comment.