From daf97d4d349bc22937aea7cd65c6c4fc67d68ee2 Mon Sep 17 00:00:00 2001 From: Vladimir Sheremet Date: Mon, 25 Mar 2024 16:23:41 +0100 Subject: [PATCH] fix: don't collect tests several times for the same file in different projects --- src/extension.ts | 12 ++++++++++-- src/testTree.ts | 22 ++++++++-------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 55cc2a3..de62bf2 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -9,6 +9,8 @@ import { configGlob, workspaceGlob } from './constants' import { log } from './log' import { createVitestWorkspaceFile, noop } from './utils' import { resolveVitestPackages } from './api/pkg' +import type { TestFile } from './testTreeData' +import { getTestData } from './testTreeData' export async function activate(context: vscode.ExtensionContext) { const extension = new VitestExtension() @@ -139,8 +141,14 @@ class VitestExtension { // collect tests inside a test file vscode.window.visibleTextEditors.forEach(async (editor) => { const testItems = this.testTree.getFileTestItems(editor.document.uri) - for (const item of testItems) - await this.resolveTestFile(item) + const apis = new Set() + for (const item of testItems) { + const data = getTestData(item) as TestFile + if (!apis.has(data.api)) { + await this.resolveTestFile(item) + apis.add(data.api) + } + } }) } diff --git a/src/testTree.ts b/src/testTree.ts index 956fffd..c036f7d 100644 --- a/src/testTree.ts +++ b/src/testTree.ts @@ -314,19 +314,13 @@ function getAPIFromFolder(folder: vscode.TestItem): VitestFolderAPI | null { } function getAPIFromTestItem(testItem: vscode.TestItem): VitestFolderAPI | null { - let iter: vscode.TestItem | undefined = testItem + const data = getTestData(testItem) // API is stored in test files - if this is a folder, try to find a file inside, - // otherwise go up until we find a file - if (getTestData(iter) instanceof TestFolder) { - return getAPIFromFolder(iter) - } - else { - while (iter) { - const data = getTestData(iter) - if (data instanceof TestFile) - return data.api - iter = iter.parent - } - } - return null + // otherwise go up until we find a file, this should never be a folder + if (data instanceof TestFolder) + return getAPIFromFolder(testItem) + + if (data instanceof TestFile) + return data.api + return data.file.api } -- 2.54.0