From e0308f1c885a8f7972d3f632e563dd918d68e9df Mon Sep 17 00:00:00 2001 From: Vladimir Date: Tue, 17 Dec 2024 10:20:38 +0100 Subject: [PATCH] fix: reset istanbul coverage between runs (#560) --- src/api.ts | 4 ++++ src/api/rpc.ts | 1 + src/runner/runner.ts | 10 ++++++++++ src/worker/vitest.ts | 17 +++++++++++++++++ 4 files changed, 32 insertions(+) diff --git a/src/api.ts b/src/api.ts index 1513670..2fddef5 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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() } diff --git a/src/api/rpc.ts b/src/api/rpc.ts index 194ad1b..e2bdac7 100644 --- a/src/api/rpc.ts +++ b/src/api/rpc.ts @@ -18,6 +18,7 @@ export interface VitestMethods { watchTests: (files?: SerializedTestSpecification[] | string[], testNamePattern?: string) => void unwatchTests: () => void + invalidateIstanbulTestModules: (modules: string[] | null) => Promise enableCoverage: () => void disableCoverage: () => void waitForCoverageReport: () => Promise diff --git a/src/runner/runner.ts b/src/runner/runner.ts index 6dbbe3c..655124a 100644 --- a/src/runner/runner.ts +++ b/src/runner/runner.ts @@ -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) } diff --git a/src/worker/vitest.ts b/src/worker/vitest.ts index 0894b3f..3630284 100644 --- a/src/worker/vitest.ts +++ b/src/worker/vitest.ts @@ -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() } -- 2.51.2