-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(coverage): isolate: false
with istanbul provider
#6935
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,48 @@ | ||
import type { CoverageMapData } from 'istanbul-lib-coverage' | ||
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 | ||
|
||
return coverage | ||
} | ||
// When isolated, there are no previous results | ||
if (!coverageMap) { | ||
return | ||
} | ||
|
||
const _default: { | ||
getProvider: () => Promise<IstanbulCoverageProvider> | ||
takeCoverage: () => any | ||
} = { | ||
getProvider, | ||
takeCoverage, | ||
} | ||
for (const filename in coverageMap) { | ||
const branches = coverageMap[filename].b | ||
|
||
for (const key in branches) { | ||
branches[key] = branches[key].map(() => 0) | ||
} | ||
|
||
export default _default | ||
for (const metric of ['f', 's'] as const) { | ||
const entry = coverageMap[filename][metric] | ||
|
||
for (const key in entry) { | ||
entry[key] = 0 | ||
} | ||
} | ||
} | ||
}, | ||
|
||
async 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() | ||
}, | ||
} |
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,52 @@ | ||
import type { WorkspaceSpec } from 'vitest/node' | ||
import { expect, test } from 'vitest' | ||
import { isV8Provider, readCoverageMap, runVitest } from '../utils' | ||
|
||
for (const isolate of [true, false]) { | ||
// TODO: Requires #6736 | ||
const fails = isV8Provider() && isolate === false | ||
|
||
test(`{ isolate: ${isolate} }`, { fails }, async () => { | ||
await runVitest({ | ||
include: ['fixtures/test/isolation-*'], | ||
setupFiles: ['fixtures/setup.isolation.ts'], | ||
sequence: { sequencer: Sorter }, | ||
|
||
isolate, | ||
fileParallelism: false, | ||
|
||
coverage: { | ||
all: false, | ||
reporter: 'json', | ||
}, | ||
}) | ||
|
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing the reset here on
startCoverage
instead ofstopCoverage
should have better perf, as isolated tests won't ever run into this. Only non-isolated tests can have previous results in global scope.