Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 40 additions & 25 deletions packages/coverage-istanbul/src/index.ts
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
}
Comment on lines +12 to +19
Copy link
Member Author

@AriPerkkio AriPerkkio Nov 19, 2024

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 of stopCoverage should have better perf, as isolated tests won't ever run into this. Only non-isolated tests can have previous results in global scope.


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()
},
}
6 changes: 6 additions & 0 deletions test/coverage-test/fixtures/setup.isolation.ts
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);
});
7 changes: 7 additions & 0 deletions test/coverage-test/fixtures/src/branch.ts
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;
};
9 changes: 9 additions & 0 deletions test/coverage-test/fixtures/test/isolation-1-fixture.test.ts
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 test/coverage-test/fixtures/test/isolation-2-fixture.test.ts
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);
});
52 changes: 52 additions & 0 deletions test/coverage-test/test/isolation.test.ts
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
}
}
2 changes: 1 addition & 1 deletion test/coverage-test/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
reporters: 'basic',
reporters: 'verbose',
isolate: false,
poolOptions: {
threads: {
Expand Down