diff --git a/packages/coverage-istanbul/src/index.ts b/packages/coverage-istanbul/src/index.ts index a88aae227082..de791bc69b33 100644 --- a/packages/coverage-istanbul/src/index.ts +++ b/packages/coverage-istanbul/src/index.ts @@ -7,5 +7,11 @@ export async function getProvider() { export function takeCoverage() { // @ts-expect-error -- untyped global - return globalThis[COVERAGE_STORE_KEY] + const coverage = globalThis[COVERAGE_STORE_KEY] + + // Reset coverage map to prevent duplicate results if this is called twice in row + // @ts-expect-error -- untyped global + globalThis[COVERAGE_STORE_KEY] = {} + + return coverage } diff --git a/test/coverage-test/coverage-test/coverage.istanbul.test.ts b/test/coverage-test/coverage-test/coverage.istanbul.test.ts index ead65978cbf4..6b3858994fde 100644 --- a/test/coverage-test/coverage-test/coverage.istanbul.test.ts +++ b/test/coverage-test/coverage-test/coverage.istanbul.test.ts @@ -40,8 +40,8 @@ test('istanbul json report', async () => { const normalizedReport: CoverageFinalJson['default'] = {} for (const [filename, coverage] of Object.entries(jsonReport)) { - coverage.path = normalize(coverage.path.replace(process.cwd(), '')) - normalizedReport[filename.replace(process.cwd(), '')] = coverage + coverage.path = normalizeFilename(coverage.path) + normalizedReport[normalizeFilename(filename)] = coverage } expect(normalizedReport).toMatchSnapshot() @@ -81,3 +81,7 @@ test('file using import.meta.env is included in report', async () => { expect(files).toContain('importEnv.ts.html') }) + +function normalizeFilename(filename: string) { + return normalize(filename.replace(process.cwd(), '')) +}