Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/coverage-istanbul/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider<ResolvedCover
if (node.id && !this.transformedModuleIds.has(node.id)) {
moduleGraph.invalidateModule(node, seen)
}
seen.add(node) // to avoid infinite loops in circular dependencies
node.importedModules.forEach((mod) => {
this.invalidateTree(mod, moduleGraph, seen)
})
Expand Down
7 changes: 7 additions & 0 deletions test/coverage-test/fixtures/src/circularA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { circularB } from './circularB'

export const CalledB: number[] = []

export function circularA() {
return circularB()
}
5 changes: 5 additions & 0 deletions test/coverage-test/fixtures/src/circularB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CalledB } from './circularA'

export function circularB() {
return CalledB.push(CalledB.length)
}
15 changes: 15 additions & 0 deletions test/coverage-test/fixtures/test/circular.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, it } from 'vitest'
import { CalledB, circularA } from '../src/circularA'

it('circular', () => {
CalledB.length = 0

circularA()

expect(CalledB.length).toBe(1)

circularA()
circularA()

expect(CalledB).toEqual([0, 1, 2])
})
33 changes: 33 additions & 0 deletions test/coverage-test/test/run-dynamic-coverage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@ test('enableCoverage() collects coverage after being called', async () => {
expect(coverageMap.files()).toContain('<process-cwd>/fixtures/src/math.ts')
})

test('enableCoverage() invalidates circular modules', async () => {
await cleanupCoverageJson()

await expect(readCoverageMap(), 'coverage map should not be on the disk').rejects.toThrowError(/no such file/)

// Simulating user actions in the VSCode Vitest extension:
// 1. User clicks "Run Test with Coverage" to generate coverage files normally
const { ctx } = await runVitest({
include: ['fixtures/test/circular.test.ts'],
watch: false,
coverage: {
enabled: true,
reporter: 'json',
},
})

const coverageMap = await readCoverageMap()
expect(coverageMap.files()).toEqual([
'<process-cwd>/fixtures/src/circularA.ts',
'<process-cwd>/fixtures/src/circularB.ts',
])

// 2. User reruns tests with coverage
await ctx!.enableCoverage()
await ctx!.rerunFiles()

const coverageMap2 = await readCoverageMap()
expect(coverageMap2.files()).toEqual([
'<process-cwd>/fixtures/src/circularA.ts',
'<process-cwd>/fixtures/src/circularB.ts',
])
})

test('disableCoverage() stops collecting coverage going forward', async () => {
const { ctx } = await runVitest({
include: ['fixtures/test/math.test.ts'],
Expand Down
Loading