From b6c4bf7568e2c65e61bbfbec7beb46b5c116feb7 Mon Sep 17 00:00:00 2001 From: Vladimir Date: Mon, 16 Dec 2024 14:33:40 +0100 Subject: [PATCH] fix: report coverage with negative columns (#557) Fixes #556 --- src/runner/runner.ts | 19 +++++++++++++++++++ src/testTree.ts | 6 ++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/runner/runner.ts b/src/runner/runner.ts index 138349db..6dbbe3c6 100644 --- a/src/runner/runner.ts +++ b/src/runner/runner.ts @@ -407,6 +407,25 @@ export class TestRunner extends vscode.Disposable { return const coverage = readCoverageReport(reportsDirectory) + // TODO: quick patch, coverage shouldn't report negative columns + function ensureLoc(loc: any) { + if (!loc) { + return + } + if (loc.start?.column && loc.start.column < 0) { + loc.start.column = 0 + } + if (loc.end?.column && loc.end.column < 0) { + loc.end.column = 0 + } + } + for (const file in coverage) { + for (const key in coverage[file].branchMap) { + const branch = coverage[file].branchMap[key] + ensureLoc(branch.loc) + branch.locations?.forEach((loc: any) => ensureLoc(loc)) + } + } await coverageContext.applyJson(testRun, coverage) rm(reportsDirectory, { recursive: true, force: true }).then(() => { diff --git a/src/testTree.ts b/src/testTree.ts index 97a88163..39460675 100644 --- a/src/testTree.ts +++ b/src/testTree.ts @@ -21,6 +21,7 @@ export class TestTree extends vscode.Disposable { // file test items have the project name in their id, so we need a separate map // to store all of them private testItemsByFile = new Map() + private testFiles = new Set() private watcher: ExtensionWatcher @@ -134,7 +135,7 @@ export class TestTree extends vscode.Disposable { if (cached) return cached - const fileUri = vscode.Uri.file(file) + const fileUri = vscode.Uri.file(resolve(file)) const parentItem = this.getOrCreateFolderTestItem(api, dirname(file)) const label = `${basename(file)}${project ? ` [${project}]` : ''}` const testFileItem = this.controller.createTestItem( @@ -159,10 +160,11 @@ export class TestTree extends vscode.Disposable { const cachedItems = this.testItemsByFile.get(normalizedFile) || [] cachedItems.push(testFileItem) this.testItemsByFile.set(normalizedFile, cachedItems) + this.testFiles.add(fileUri.fsPath) vscode.commands.executeCommand( 'setContext', 'vitest.testFiles', - Array.from(this.testItemsByFile.keys()), + Array.from(this.testFiles), ) return testFileItem