diff --git a/samples/basic/test/0.test.ts b/samples/basic/test/0.test.ts new file mode 100644 index 00000000..c76dc694 --- /dev/null +++ b/samples/basic/test/0.test.ts @@ -0,0 +1,3 @@ +describe("haha", () => { + test("f", () => {}); +}); diff --git a/src/pure/runner.ts b/src/pure/runner.ts index 16b6b4c1..2b59f2fb 100644 --- a/src/pure/runner.ts +++ b/src/pure/runner.ts @@ -88,8 +88,6 @@ export class TestRunner { windowsHide: false, cwd: this.workspacePath, }); - console.error(stderr); - console.log(stdout); } else { await execa("npx", ["vitest"].concat(args), { cwd: this.workspacePath, @@ -99,6 +97,23 @@ export class TestRunner { console.error(e); } + if (!existsSync(path)) { + return { + numFailedTests: 0, + numFailedTestSuites: 0, + numPassedTests: 0, + numPassedTestSuites: 0, + numPendingTests: 0, + numTodoTests: 0, + numPendingTestSuites: 0, + numTotalTests: 0, + numTotalTestSuites: 0, + startTime: 0, + success: false, + testResults: [], + }; + } + const file = await readFile(path, "utf-8"); const out = JSON.parse(file) as AggregatedResult; return out; diff --git a/src/runHandler.ts b/src/runHandler.ts index 75ea20f0..79b20866 100644 --- a/src/runHandler.ts +++ b/src/runHandler.ts @@ -1,6 +1,11 @@ import * as vscode from "vscode"; import { getVitestPath as getVitestPath, TestRunner } from "./pure/runner"; -import { runTest } from "./runTest"; +import { + getAllTestCases, + WEAKMAP_TEST_DATA, + getTestCaseId, + TestFile, +} from "./TestData"; export async function runHandler( ctrl: vscode.TestController, @@ -18,10 +23,90 @@ export async function runHandler( const tests = request.include ?? gatherTestItems(ctrl.items); const run = ctrl.createTestRun(request); - await Promise.all(tests.map((test) => runTest(runner, run, test))); + await Promise.allSettled(tests.map((test) => runTest(runner, run, test))); + run.end(); } + function gatherTestItems(collection: vscode.TestItemCollection) { const items: vscode.TestItem[] = []; collection.forEach((item) => items.push(item)); return items; } + +async function runTest( + runner: TestRunner, + run: vscode.TestRun, + item: vscode.TestItem +) { + const testingData = WEAKMAP_TEST_DATA.get(item); + if (!testingData) { + throw new Error("file item not found"); + } + + let file: vscode.TestItem; + if (testingData instanceof TestFile) { + file = item; + } else { + file = testingData.fileItem; + if (!file) { + throw new Error("file item not found"); + } + } + + const fileTestCases = getAllTestCases(file); + const testCaseSet = new Set(getAllTestCases(item)); + testCaseSet.forEach((testCase) => { + run.enqueued(testCase); + run.started(testCase); + }); + + const data = WEAKMAP_TEST_DATA.get(item)!; + const out = await runner.scheduleRun(item.uri!.fsPath, data.getFullPattern()); + if (out.testResults.length !== 0) { + out.testResults.forEach((result, index) => { + let child: undefined | vscode.TestItem = fileTestCases[index]; + const id = getTestCaseId(item, result.displayName!) || ""; + if (!child || !child.id.startsWith(id)) { + console.error("not match"); + console.dir(out.testResults); + console.dir(fileTestCases); + throw new Error(); + } + + if (!child || !testCaseSet.has(child)) { + return; + } + + testCaseSet.delete(child); + switch (result.status) { + case "pass": + run.passed(child, result.perfStats?.runtime); + return; + case "fail": + run.failed( + child, + new vscode.TestMessage(result.failureMessage || "") + ); + return; + } + + if (result.skipped || result.status == null) { + run.skipped(child); + } + }); + + testCaseSet.forEach((testCase) => { + run.skipped(testCase); + run.appendOutput(`Cannot find test ${testCase.id}`); + }); + } else { + testCaseSet.forEach((testCase) => { + run.errored( + testCase, + new vscode.TestMessage( + "Testing is not started correctly. Please check your configuration." + ) + ); + }); + } +} diff --git a/src/runTest.ts b/src/runTest.ts deleted file mode 100644 index e761b67a..00000000 --- a/src/runTest.ts +++ /dev/null @@ -1,87 +0,0 @@ -import * as vscode from "vscode"; -import { TestRunner } from "./pure/runner"; -import { - getAllTestCases, - WEAKMAP_TEST_DATA, - getTestCaseId, - TestFile, -} from "./TestData"; - -export async function runTest( - runner: TestRunner, - run: vscode.TestRun, - item: vscode.TestItem -) { - const testingData = WEAKMAP_TEST_DATA.get(item); - if (!testingData) { - throw new Error("file item not found"); - } - - let file: vscode.TestItem; - if (testingData instanceof TestFile) { - file = item; - } else { - file = testingData.fileItem; - if (!file) { - throw new Error("file item not found"); - } - } - - const fileTestCases = getAllTestCases(file); - const testCaseSet = new Set(getAllTestCases(item)); - testCaseSet.forEach((testCase) => { - run.started(testCase); - }); - - const data = WEAKMAP_TEST_DATA.get(item)!; - const out = await runner.scheduleRun(item.uri!.fsPath, data.getFullPattern()); - if (out.testResults.length !== 0) { - out.testResults.forEach((result, index) => { - let child: undefined | vscode.TestItem = fileTestCases[index]; - const id = getTestCaseId(item, result.displayName!) || ""; - if (!child || !child.id.startsWith(id)) { - console.error("not match"); - console.dir(out.testResults); - console.dir(fileTestCases); - throw new Error(); - } - - if (!child || !testCaseSet.has(child)) { - return; - } - - testCaseSet.delete(child); - switch (result.status) { - case "pass": - run.passed(child, result.perfStats?.runtime); - return; - case "fail": - run.failed( - child, - new vscode.TestMessage(result.failureMessage || "") - ); - return; - } - - if (result.skipped || result.status == null) { - run.skipped(child); - } - }); - - testCaseSet.forEach((testCase) => { - run.skipped(testCase); - run.appendOutput(`Cannot find test ${testCase.id}`); - }); - } else { - testCaseSet.forEach((testCase) => { - run.errored( - testCase, - new vscode.TestMessage( - "Testing is not started correctly. Please check your configuration." - ) - ); - }); - } - - run.end(); -}