Skip to content

Commit

Permalink
feat: add test runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 14, 2022
1 parent a887176 commit 3d6e1ab
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 75 deletions.
20 changes: 0 additions & 20 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,5 @@
},
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
"typescript.tsc.autoDetect": "off",
"workbench.colorCustomizations": {
"activityBar.activeBackground": "#13d75b",
"activityBar.activeBorder": "#8a4fef",
"activityBar.background": "#13d75b",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#8a4fef",
"activityBarBadge.foreground": "#e7e7e7",
"sash.hoverBorder": "#13d75b",
"statusBar.background": "#0fa847",
"statusBar.foreground": "#e7e7e7",
"statusBarItem.hoverBackground": "#13d75b",
"statusBarItem.remoteBackground": "#0fa847",
"statusBarItem.remoteForeground": "#e7e7e7",
"titleBar.activeBackground": "#0fa847",
"titleBar.activeForeground": "#e7e7e7",
"titleBar.inactiveBackground": "#0fa84799",
"titleBar.inactiveForeground": "#e7e7e799"
},
"peacock.color": "#0fa847",
"cSpell.words": ["execa"]
}
2 changes: 1 addition & 1 deletion samples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"license": "ISC",
"devDependencies": {
"vite": "^2.8.6",
"vitest": "^0.6.0"
"vitest": "^0.6.1"
}
}
8 changes: 4 additions & 4 deletions samples/basic/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,10 @@ vite@^2.7.10, vite@^2.8.6:
optionalDependencies:
fsevents "~2.3.2"

vitest@^0.6.0:
version "0.6.0"
resolved "https://registry.npmmirror.com/vitest/-/vitest-0.6.0.tgz#297e4bb1b69e797bce5f7af8a62c92b568dfbfd5"
integrity sha512-FuIkLHCQxz6rO35MQROUtVdwcBaYnt198YpPGIrJXmuNHGolfPbrZIiwpD7bek0OiETxuphK3+KR5oJ5Qi1g5A==
vitest@^0.6.1:
version "0.6.1"
resolved "https://registry.npmmirror.com/vitest/-/vitest-0.6.1.tgz#d2cbe55c7c48b5a34c3561bb16d8646c3824d5aa"
integrity sha512-rgHgTO3xHCrbgmqYdUz4ViO0g2ekHeOfV60J6+1c6Ypzk1EqCE2sN8IpF4TDGs6BOvWIsVd/ob5c6U4Ozzu92g==
dependencies:
"@types/chai" "^4.3.0"
"@types/chai-subset" "^1.3.3"
Expand Down
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import * as vscode from "vscode";
import { extensionId } from "./config";
import { discoverAllFilesInWorkspace, discoverTestFromDoc } from "./discover";
import { getVitePath as getVitestPath, TestRunner } from "./pure/runner";
import { runTest, TestCase, TestData, testData, TestFile } from "./test_data";
import { TestCase, TestData, testData, TestFile } from "./test_data";
import { runTest } from "./runTest";

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
Expand Down Expand Up @@ -76,7 +77,7 @@ async function runHandler(

const tests = request.include ?? gatherTestItems(ctrl.items);
const run = ctrl.createTestRun(request);
await Promise.all(tests.map((test) => runTest(ctrl, runner, run, test)));
await Promise.all(tests.map((test) => runTest(runner, run, test)));
}

function gatherTestItems(collection: vscode.TestItemCollection) {
Expand Down
48 changes: 48 additions & 0 deletions src/runTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as vscode from "vscode";
import { TestRunner } from "./pure/runner";
import { getAllTestCases, testData, getTestCaseId } from "./test_data";

export async function runTest(
runner: TestRunner,
run: vscode.TestRun,
item: vscode.TestItem
) {
const testCases = new Set(getAllTestCases(item));
const idMap = new Map<string, vscode.TestItem>();
testCases.forEach((testCase) => {
run.started(testCase);
idMap.set(testCase.id, testCase);
});

const data = testData.get(item)!;
const out = await runner.scheduleRun(item.uri!.fsPath, data.pattern);
console.log(out.testResults);
out.testResults.forEach((result) => {
const id = getTestCaseId(item, result.displayName!) || "";
const child = idMap.get(id);
if (!child) {
return;
}

testCases.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);
}
});

testCases.forEach((testCase) => {
run.skipped(testCase);
run.appendOutput(`Cannot find test ${testCase.id}`);
});

run.end();
}
52 changes: 4 additions & 48 deletions src/test_data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as vscode from "vscode";
import { discoverTestFromFileContent } from "./discover";
import { TestRunner } from "./pure/runner";
import { getContentFromFilesystem } from "./vscode_utils";

export const testData = new WeakMap<vscode.TestItem, TestData>();
Expand All @@ -11,7 +10,7 @@ export const testItemIdMap = new WeakMap<

export type TestData = TestFile | TestDescribe | TestCase;

function getTestCaseId(
export function getTestCaseId(
childItem: vscode.TestItem,
name: string
): string | undefined {
Expand All @@ -23,53 +22,10 @@ function getTestCaseId(
}
}

export async function runTest(
ctrl: vscode.TestController,
runner: TestRunner,
run: vscode.TestRun,
item: vscode.TestItem
export function getAllTestCases(
item: vscode.TestItem,
agg: vscode.TestItem[] = []
) {
const testCases = new Set(getAllTestCases(item));
const idMap = new Map<string, vscode.TestItem>();
testCases.forEach((testCase) => {
run.started(testCase);
idMap.set(testCase.id, testCase);
});

const data = testData.get(item)!;
const out = await runner.scheduleRun(item.uri!.fsPath, data.pattern);
console.log(out.testResults);
out.testResults.forEach((result) => {
const id = getTestCaseId(item, result.displayName!) || "";
const child = idMap.get(id);
if (!child) {
return;
}

testCases.delete(child);
switch (result.status) {
case "pass":
run.passed(child);
return;
case "fail":
run.failed(child, new vscode.TestMessage(result.failureMessage || ""));
return;
}

if (result.skipped || result.status == null) {
run.skipped(child);
}
});

testCases.forEach((testCase) => {
run.skipped(testCase);
run.appendOutput(`Cannot find test ${testCase.id}`);
});

run.end();
}

function getAllTestCases(item: vscode.TestItem, agg: vscode.TestItem[] = []) {
if (item.children.size) {
item.children.forEach((child) => {
getAllTestCases(child, agg);
Expand Down

0 comments on commit 3d6e1ab

Please sign in to comment.