From febdc42caf10617cf2da52a7b46414f620144474 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Wed, 23 Mar 2022 10:53:08 +0800 Subject: [PATCH] fix: refine error handling and err msg --- samples/basic/test/add.test.ts | 10 +- samples/basic/test/fail_to_run.test.ts | 1 + samples/basic/test/mul.test.ts | 2 +- src/pure/runner.ts | 127 ++++++++++++------------- src/runHandler.ts | 9 +- 5 files changed, 78 insertions(+), 71 deletions(-) create mode 100644 samples/basic/test/fail_to_run.test.ts diff --git a/samples/basic/test/add.test.ts b/samples/basic/test/add.test.ts index 71b53285..fc005a94 100644 --- a/samples/basic/test/add.test.ts +++ b/samples/basic/test/add.test.ts @@ -1,12 +1,12 @@ import { expect, describe, it } from "vitest"; describe("addition", () => { - it("run", () => { + it("run 1", () => { console.log("run"); - expect(1 + 1).toBe(3); + expect(1 + 1).toBe(2); }); - it("failed", () => { + it("should failed", () => { expect(1 + 2).toBe(2); }); @@ -19,9 +19,9 @@ describe("addition", () => { }); describe("testing", () => { - it("run", () => { + it("run 2", () => { let a = 10; expect(a).toBe(10); }); - it("same name", () => {}); + it("same name 2", () => {}); }); diff --git a/samples/basic/test/fail_to_run.test.ts b/samples/basic/test/fail_to_run.test.ts new file mode 100644 index 00000000..12096f21 --- /dev/null +++ b/samples/basic/test/fail_to_run.test.ts @@ -0,0 +1 @@ +test("ddd", () => {}); diff --git a/samples/basic/test/mul.test.ts b/samples/basic/test/mul.test.ts index 23a9032b..7e85ddab 100644 --- a/samples/basic/test/mul.test.ts +++ b/samples/basic/test/mul.test.ts @@ -1,6 +1,6 @@ import { expect, describe, it } from "vitest"; describe("mul", () => { - it.skip("run", () => {}); + it.skip("run 1", () => {}); it("run", () => {}); }); diff --git a/src/pure/runner.ts b/src/pure/runner.ts index 86af9502..c19b5acb 100644 --- a/src/pure/runner.ts +++ b/src/pure/runner.ts @@ -1,6 +1,5 @@ import { spawn } from "child_process"; import { existsSync, readFile } from "fs-extra"; -import { TaskQueue } from "mighty-promise"; import { tmpdir } from "os"; import * as path from "path"; @@ -62,12 +61,6 @@ interface AggregatedResult { } export class TestRunner { - private queue = new TaskQueue>({ - maxParallelNum: 4, - onError: (err) => { - throw err; - }, - }); constructor( private workspacePath: string, private vitestPath: string | undefined @@ -78,70 +71,76 @@ export class TestRunner { testNamePattern: string | undefined, log: (msg: string) => void = () => {} ): Promise { - return this.queue.push(async () => { - const path = getTempPath(); - const args = [ - "--reporter=json", - "--reporter=verbose", - "--outputFile", - path, - "--run", - ] as string[]; - if (testFile) { - args.push(...testFile); - } - if (testNamePattern) { - args.push("-t", testNamePattern); - } - - let child; - let error: any; - let outputs: string[] = []; - try { - // it will throw when test failed or the testing is failed to run - if (this.vitestPath) { - child = spawn(this.vitestPath, args, { - cwd: this.workspacePath, - stdio: ["ignore", "pipe", "pipe"], - }); - } else { - child = spawn("npx", ["vitest"].concat(args), { - cwd: this.workspacePath, - stdio: ["ignore", "pipe", "pipe"], - }); - } - - for await (const line of chunksToLinesAsync(child.stdout)) { - log(line + "\r\n"); - outputs.push(line); - } - } catch (e) { - error = e; - } + const path = getTempPath(); + const args = [ + "--reporter=json", + "--reporter=verbose", + "--outputFile", + path, + "--run", + ] as string[]; + if (testFile) { + args.push(...testFile); + } + if (testNamePattern) { + args.push("-t", testNamePattern); + } - if (!existsSync(path)) { - handleError(); + const workspacePath = this.workspacePath; + let child; + let error: any; + let outputs: string[] = []; + try { + // it will throw when test failed or the testing is failed to run + if (this.vitestPath) { + child = spawn(this.vitestPath, args, { + cwd: workspacePath, + stdio: ["ignore", "pipe", "pipe"], + }); + } else { + child = spawn("npx", ["vitest"].concat(args), { + cwd: this.workspacePath, + stdio: ["ignore", "pipe", "pipe"], + }); } - const file = await readFile(path, "utf-8"); - const out = JSON.parse(file) as AggregatedResult; - if (out.testResults.length === 0) { - handleError(); + for await (const line of chunksToLinesAsync(child.stdout)) { + log(line + "\r\n"); + outputs.push(line); } + } catch (e) { + error = e; + } - return out; + if (!existsSync(path)) { + handleError(); + } - function handleError() { - if (error) { - console.error("scheduleRun error", error.toString()); - console.error(error.stack); - } else { - error = new Error(outputs.join("\n")); - } + const file = await readFile(path, "utf-8"); + const out = JSON.parse(file) as AggregatedResult; + if (out.testResults.length === 0) { + handleError(); + } - console.error(outputs.join("\n")); - return error as Error; + return out; + + function handleError() { + const prefix = + `When running:\n` + + ` npx vitest ${args.join(" ")}\n` + + `cwd: ${workspacePath}`; + if (error) { + console.error("scheduleRun error", error.toString()); + console.error(error.stack); + const e = error; + error = new Error(prefix + "\n" + error.toString()); + error.stack = e.stack; + } else { + error = new Error(prefix + "\nLog:\n" + outputs.join("\n")); } - }); + + console.error(outputs.join("\n")); + throw error; + } } } diff --git a/src/runHandler.ts b/src/runHandler.ts index 6d1d783f..b73f59e0 100644 --- a/src/runHandler.ts +++ b/src/runHandler.ts @@ -210,7 +210,14 @@ async function runTest( } ); testCaseSet.forEach((testCase) => { - run.skipped(testCase); + run.errored( + testCase, + new vscode.TestMessage( + `Test result not found. \n` + + `Can you run vitest successfully on this file? Does it need custom option to run?\n` + + `Does this file contain test case with the same name? \n` + ) + ); run.appendOutput(`Cannot find test ${testCase.id}`); }); } else {