Skip to content

Commit

Permalink
fix: reset istanbul coverage between runs
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Dec 17, 2024
1 parent af1aa46 commit 9cf79d2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ export class VitestFolderAPI {
return this.meta.rpc.waitForCoverageReport()
}

async invalidateIstanbulTestModules(modules: string[] | null) {
await this.meta.rpc.invalidateIstanbulTestModules(modules)
}

async enableCoverage() {
await this.meta.rpc.enableCoverage()
}
Expand Down
1 change: 1 addition & 0 deletions src/api/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface VitestMethods {
watchTests: (files?: SerializedTestSpecification[] | string[], testNamePattern?: string) => void
unwatchTests: () => void

invalidateIstanbulTestModules: (modules: string[] | null) => Promise<void>
enableCoverage: () => void
disableCoverage: () => void
waitForCoverageReport: () => Promise<string | null>
Expand Down
10 changes: 10 additions & 0 deletions src/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ export class TestRunner extends vscode.Disposable {
}),
)

const modules = !request.include
? null
: getTestFiles(request.include).map((f) => {
if (typeof f === 'string') {
return f
}
return f[1]
})

await this.api.invalidateIstanbulTestModules(modules)
await this.runTests(request, token)
}

Expand Down
17 changes: 17 additions & 0 deletions src/worker/vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,23 @@ export class Vitest implements VitestMethods {
this.watcher.trackEveryFile()
}

// we need to invalidate the modules because Vitest caches the code injected by istanbul
async invalidateIstanbulTestModules(modules: string[] | null) {
if (!this.coverage.enabled || this.coverage.config.provider !== 'istanbul') {
return
}
if (!modules) {
this.ctx.server.moduleGraph.invalidateAll()
return
}
modules.forEach((moduleId) => {
const mod = this.ctx.server.moduleGraph.getModuleById(moduleId)
if (mod) {
this.invalidateTree(mod)
}
})
}

disableCoverage() {
return this.coverage.disable()
}
Expand Down

0 comments on commit 9cf79d2

Please sign in to comment.