Skip to content

Commit

Permalink
fix: refine error handling and err msg
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 23, 2022
1 parent 665a275 commit febdc42
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 71 deletions.
10 changes: 5 additions & 5 deletions samples/basic/test/add.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});

Expand All @@ -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", () => {});
});
1 change: 1 addition & 0 deletions samples/basic/test/fail_to_run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test("ddd", () => {});
2 changes: 1 addition & 1 deletion samples/basic/test/mul.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, describe, it } from "vitest";

describe("mul", () => {
it.skip("run", () => {});
it.skip("run 1", () => {});
it("run", () => {});
});
127 changes: 63 additions & 64 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -62,12 +61,6 @@ interface AggregatedResult {
}

export class TestRunner {
private queue = new TaskQueue<Promise<AggregatedResult>>({
maxParallelNum: 4,
onError: (err) => {
throw err;
},
});
constructor(
private workspacePath: string,
private vitestPath: string | undefined
Expand All @@ -78,70 +71,76 @@ export class TestRunner {
testNamePattern: string | undefined,
log: (msg: string) => void = () => {}
): Promise<AggregatedResult> {
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;
}
}
}
9 changes: 8 additions & 1 deletion src/runHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit febdc42

Please sign in to comment.