From ea3c16e46aaad6f597edf201ebbf22bea1f43b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ari=20Perkki=C3=B6?= Date: Wed, 17 Apr 2024 15:47:01 +0300 Subject: [PATCH] fix(coverage): prevent crash when `cleanOnRerun` is disabled (#5540) --- packages/coverage-istanbul/src/provider.ts | 9 +++++++-- packages/coverage-v8/src/provider.ts | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/coverage-istanbul/src/provider.ts b/packages/coverage-istanbul/src/provider.ts index 7b4676dc62e9..819f99232d7a 100644 --- a/packages/coverage-istanbul/src/provider.ts +++ b/packages/coverage-istanbul/src/provider.ts @@ -248,8 +248,13 @@ export class IstanbulCoverageProvider extends BaseCoverageProvider implements Co } } - await fs.rm(this.coverageFilesDirectory, { recursive: true }) - this.coverageFiles = new Map() + // In watch mode we need to preserve the previous results if cleanOnRerun is disabled + const keepResults = !this.options.cleanOnRerun && this.ctx.config.watch + + if (!keepResults) { + this.coverageFiles = new Map() + await fs.rm(this.coverageFilesDirectory, { recursive: true }) + } } async getCoverageMapForUncoveredFiles(coveredFiles: string[]) { diff --git a/packages/coverage-v8/src/provider.ts b/packages/coverage-v8/src/provider.ts index be1805020c46..856f68068982 100644 --- a/packages/coverage-v8/src/provider.ts +++ b/packages/coverage-v8/src/provider.ts @@ -239,8 +239,13 @@ export class V8CoverageProvider extends BaseCoverageProvider implements Coverage } } - this.coverageFiles = new Map() - await fs.rm(this.coverageFilesDirectory, { recursive: true }) + // In watch mode we need to preserve the previous results if cleanOnRerun is disabled + const keepResults = !this.options.cleanOnRerun && this.ctx.config.watch + + if (!keepResults) { + this.coverageFiles = new Map() + await fs.rm(this.coverageFilesDirectory, { recursive: true }) + } } private async getUntestedFiles(testedFiles: string[]): Promise {