Skip to content

Commit

Permalink
fix: end run after all tests settled
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 21, 2022
1 parent 634326d commit f538b2b
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 91 deletions.
3 changes: 3 additions & 0 deletions samples/basic/test/0.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe("haha", () => {
test("f", () => {});
});
19 changes: 17 additions & 2 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
89 changes: 87 additions & 2 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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."
)
);
});
}
}
87 changes: 0 additions & 87 deletions src/runTest.ts

This file was deleted.

0 comments on commit f538b2b

Please sign in to comment.