-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(coverage): correct coverage when
isolate: false
is used (#6957)
- Loading branch information
1 parent
fa75409
commit 426ce6d
Showing
16 changed files
with
193 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,49 @@ | ||
import type { CoverageMapData } from 'istanbul-lib-coverage' | ||
import type { CoverageProviderModule } from 'vitest/node' | ||
import type { IstanbulCoverageProvider } from './provider' | ||
import { COVERAGE_STORE_KEY } from './constants' | ||
|
||
export async function getProvider(): Promise<IstanbulCoverageProvider> { | ||
// to not bundle the provider | ||
const providerPath = './provider.js' | ||
const { IstanbulCoverageProvider } = (await import( | ||
/* @vite-ignore */ | ||
providerPath | ||
)) as typeof import('./provider') | ||
return new IstanbulCoverageProvider() | ||
} | ||
|
||
export function takeCoverage(): any { | ||
// @ts-expect-error -- untyped global | ||
const coverage = globalThis[COVERAGE_STORE_KEY] | ||
export default { | ||
takeCoverage() { | ||
// @ts-expect-error -- untyped global | ||
return 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] = {} | ||
startCoverage() { | ||
// @ts-expect-error -- untyped global | ||
const coverageMap = globalThis[COVERAGE_STORE_KEY] as CoverageMapData | ||
|
||
// When isolated, there are no previous results | ||
if (!coverageMap) { | ||
return | ||
} | ||
|
||
for (const filename in coverageMap) { | ||
const branches = coverageMap[filename].b | ||
|
||
for (const key in branches) { | ||
branches[key] = branches[key].map(() => 0) | ||
} | ||
|
||
for (const metric of ['f', 's'] as const) { | ||
const entry = coverageMap[filename][metric] | ||
|
||
return coverage | ||
} | ||
for (const key in entry) { | ||
entry[key] = 0 | ||
} | ||
} | ||
} | ||
}, | ||
|
||
const _default: { | ||
getProvider: () => Promise<IstanbulCoverageProvider> | ||
takeCoverage: () => any | ||
} = { | ||
getProvider, | ||
takeCoverage, | ||
} | ||
async getProvider(): Promise<IstanbulCoverageProvider> { | ||
// to not bundle the provider | ||
const providerPath = './provider.js' | ||
const { IstanbulCoverageProvider } = (await import( | ||
/* @vite-ignore */ | ||
providerPath | ||
)) as typeof import('./provider') | ||
|
||
export default _default | ||
return new IstanbulCoverageProvider() | ||
}, | ||
} satisfies CoverageProviderModule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { beforeAll } from "vitest"; | ||
import { branch } from "./src/branch"; | ||
|
||
beforeAll(() => { | ||
branch(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const branch = async (a: number) => { | ||
if (a === 15) { | ||
return true; | ||
} | ||
|
||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { test } from "vitest"; | ||
import { multiply, remainder, subtract, sum } from "../src/math"; | ||
|
||
test("Should run function sucessfully", async () => { | ||
sum(1, 1); | ||
subtract(1,2) | ||
multiply(3,4) | ||
remainder(6,7) | ||
}); |
10 changes: 10 additions & 0 deletions
10
test/coverage-test/fixtures/test/isolation-2-fixture.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { test } from "vitest"; | ||
import { branch } from "../src/branch"; | ||
|
||
test("cover some lines", async () => { | ||
branch(15); | ||
}); | ||
|
||
test("cover lines", async () => { | ||
branch(2); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { WorkspaceSpec } from 'vitest/node' | ||
import { expect, test } from 'vitest' | ||
import { readCoverageMap, runVitest } from '../utils' | ||
|
||
const pools = ['forks'] | ||
|
||
if (!process.env.COVERAGE_BROWSER) { | ||
pools.push('threads') | ||
|
||
const [major] = process.version.slice(1).split('.').map(num => Number(num)) | ||
|
||
if (major < 22) { | ||
pools.push('vmForks', 'vmThreads') | ||
} | ||
} | ||
|
||
for (const isolate of [true, false]) { | ||
for (const pool of pools) { | ||
test(`{ isolate: ${isolate}, pool: "${pool}" }`, async () => { | ||
await runVitest({ | ||
include: ['fixtures/test/isolation-*'], | ||
setupFiles: ['fixtures/setup.isolation.ts'], | ||
sequence: { sequencer: Sorter }, | ||
|
||
pool, | ||
isolate, | ||
fileParallelism: false, | ||
|
||
coverage: { | ||
all: false, | ||
reporter: ['json', 'html'], | ||
}, | ||
|
||
// @ts-expect-error -- merged in runVitest | ||
browser: { | ||
isolate, | ||
}, | ||
}) | ||
|
||
const coverageMap = await readCoverageMap() | ||
|
||
const branches = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/branch.ts') | ||
expect(branches.toSummary().lines.pct).toBe(100) | ||
expect(branches.toSummary().statements.pct).toBe(100) | ||
expect(branches.toSummary().functions.pct).toBe(100) | ||
expect(branches.toSummary().branches.pct).toBe(100) | ||
|
||
const math = coverageMap.fileCoverageFor('<process-cwd>/fixtures/src/math.ts') | ||
expect(math.toSummary().lines.pct).toBe(100) | ||
expect(math.toSummary().statements.pct).toBe(100) | ||
expect(math.toSummary().functions.pct).toBe(100) | ||
expect(math.toSummary().branches.pct).toBe(100) | ||
}) | ||
} | ||
} | ||
|
||
class Sorter { | ||
sort(files: WorkspaceSpec[]) { | ||
return files.sort((a) => { | ||
if (a.moduleId.includes('isolation-1')) { | ||
return -1 | ||
} | ||
return 1 | ||
}) | ||
} | ||
|
||
shard(files: WorkspaceSpec[]) { | ||
return files | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.