Skip to content

Commit 71ad4df

Browse files
authored
fix(coverage): v8 to handle sub process script coverages separately (#11890)
1 parent 4f91e76 commit 71ad4df

5 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎packages/coverage-v8/src/provider.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,27 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage
7272

7373
const coverageMap = this.createCoverageMap()
7474

75-
const mergedScripts = new Map<ScriptCoverageWithOffset['url'], ScriptCoverageWithOffset>()
75+
const mergedScripts = new Map<string, ScriptCoverageWithOffset>()
7676
const autoAttachSubprocess = this.options.autoAttachSubprocess
7777

7878
await this.readCoverageFiles<RawCoverage>({
7979
onFileRead(coverage) {
8080
for (const script of coverage.result) {
81-
const previous = mergedScripts.get(script.url)
82-
const merged: typeof script = mergeScriptCovs(previous ? [previous, script] : [script])
81+
const isExtendedContext = autoAttachSubprocess && script.isExtendedContext
82+
83+
// Same file can run in both module runner and child process, but with different source code
84+
const key = isExtendedContext ? `extended-context:${script.url}` : script.url
8385

84-
const startOffset = previous?.startOffset || script.startOffset || 0
85-
const isExtendedContext = previous?.isExtendedContext || script.isExtendedContext
86+
const previous = mergedScripts.get(key)
87+
const merged: typeof script = mergeScriptCovs(previous ? [previous, script] : [script])
8688

87-
merged.startOffset ||= startOffset
89+
merged.startOffset ||= previous?.startOffset || script.startOffset || 0
8890

89-
if (autoAttachSubprocess && isExtendedContext) {
91+
if (isExtendedContext) {
9092
merged.isExtendedContext = true
9193
}
9294

93-
mergedScripts.set(merged.url, merged)
95+
mergedScripts.set(key, merged)
9496
}
9597
},
9698
onFinished: async (project, environment) => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { inChild } from './worker-and-process.js'
2+
3+
process.send({ result: inChild() })
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function inWorker() {
2+
return 'b'
3+
}
4+
5+
export function inChild() {
6+
return 'child'
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, test } from 'vitest'
2+
// @ts-expect-error -- untyped
3+
import { inWorker } from '../src/worker-and-process.js'
4+
import { runFork } from '../src/start-fork-and-thread'
5+
6+
test('same file in worker and child process', async () => {
7+
expect(inWorker()).toBe('b')
8+
9+
const { result } = await runFork('worker-and-process.child.js')
10+
expect(result).toBe('child')
11+
})

‎test/coverage-test/test/extended-run-context.v8.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,29 @@ describe.each(['child process', 'worker thread'] as const)('%s', (runtime) => {
206206
})
207207
})
208208

209+
test('{ autoAttachSubprocess: true } same file in worker and child process', async () => {
210+
await runVitest({
211+
include: ['fixtures/test/worker-and-child-process.test.ts'],
212+
pool: 'forks',
213+
coverage: {
214+
autoAttachSubprocess: true,
215+
reporter: 'json',
216+
},
217+
})
218+
const coverageMap = await readCoverageMap()
219+
220+
/* See {@link file://./../fixtures/src/worker-and-process.js} */
221+
const fileCoverage = coverageMap.fileCoverageFor(
222+
'<process-cwd>/fixtures/src/worker-and-process.js',
223+
)
224+
expect(fileCoverage.getLineCoverage()).toMatchInlineSnapshot(`
225+
{
226+
"2": 1,
227+
"6": 1,
228+
}
229+
`)
230+
})
231+
209232
function assertMath(
210233
coverageMap: Awaited<ReturnType<typeof readCoverageMap>>,
211234
filename: 'math.ts' | 'math-in-js.js' = 'math.ts',

0 commit comments

Comments
 (0)