Skip to content

Commit

Permalink
feat: support debug
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Mar 21, 2022
1 parent 07fe58c commit 50aafca
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "vitest-explorer",
"displayName": "Vitest",
"description": "Run Vitest tests right from vscode",
"version": "0.0.3",
"description": "Run Vitest tests right from editor",
"version": "0.1.0",
"icon": "img/icon.png",
"preview": true,
"author": "zxch3n",
Expand Down
3 changes: 2 additions & 1 deletion samples/basic/test/add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe("addition", () => {

describe("haha a a", () => {
it("run", () => {
expect(5).toBe(4);
let a = 10;
expect(a).toBe(10);
});
it("123", () => {});
});
14 changes: 13 additions & 1 deletion src/TestData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export class TestDescribe {
getFullPattern(): string {
return getFullPattern(this);
}

getFilePath(): string {
return this.fileItem.uri!.path;
}
}

export class TestCase {
Expand All @@ -59,6 +63,10 @@ export class TestCase {
getFullPattern(): string {
return getFullPattern(this);
}

getFilePath(): string {
return this.fileItem.uri!.path;
}
}

export class TestFile {
Expand Down Expand Up @@ -86,7 +94,11 @@ export class TestFile {
}

getFullPattern(): string {
return this.pattern;
return "";
}

getFilePath(): string {
return this.item.uri!.path;
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as vscode from "vscode";
import { extensionId } from "./config";
import { discoverAllFilesInWorkspace, discoverTestFromDoc } from "./discover";
import { isVitestEnv } from "./pure/isVitestEnv";
import { runHandler } from "./runHandler";
import { debugHandler, runHandler } from "./runHandler";
import { WEAKMAP_TEST_DATA, TestFile } from "./TestData";

export async function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -42,6 +42,13 @@ export async function activate(context: vscode.ExtensionContext) {
true
);

ctrl.createRunProfile(
"Debug Tests",
vscode.TestRunProfileKind.Debug,
debugHandler.bind(null, ctrl),
true
);

vscode.window.visibleTextEditors.forEach((x) =>
discoverTestFromDoc(ctrl, x.document)
);
Expand Down
13 changes: 8 additions & 5 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, readFile } from "fs-extra";
import * as path from "path";
import { tmpdir } from "os";
import { Lock, PriorityTaskQueue, TaskQueue } from "mighty-promise";
import { TaskQueue } from "mighty-promise";
import execa = require("execa");

export function getVitestPath(projectRoot: string): string | undefined {
Expand All @@ -10,12 +10,15 @@ export function getVitestPath(projectRoot: string): string | undefined {
return;
}

if (existsSync(path.resolve(node_modules, ".bin", "vitest"))) {
return path.resolve(node_modules, ".bin", "vitest");
if (existsSync(path.resolve(node_modules, "vitest", "vitest.mjs"))) {
return path.resolve(node_modules, "vitest", "vitest.mjs");
}

if (existsSync(path.resolve(node_modules, ".bin", "vitest.cmd"))) {
return path.resolve(node_modules, ".bin", "vitest.cmd");
const suffixes = [".js", "", ".cmd"];
for (const suffix of suffixes) {
if (existsSync(path.resolve(node_modules, ".bin", "vitest" + suffix))) {
return path.resolve(node_modules, ".bin", "vitest" + suffix);
}
}

return;
Expand Down
70 changes: 69 additions & 1 deletion src/runHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { resolve } from "path";
import * as vscode from "vscode";
import { getVitestPath as getVitestPath, TestRunner } from "./pure/runner";
import {
Expand All @@ -7,12 +8,79 @@ import {
TestFile,
} from "./TestData";

export async function debugHandler(
ctrl: vscode.TestController,
request: vscode.TestRunRequest
) {
if (
vscode.workspace.workspaceFolders === undefined ||
vscode.workspace.workspaceFolders.length === 0
) {
return;
}

const tests = request.include ?? [];
if (tests.length === 1) {
await debugTest(vscode.workspace.workspaceFolders[0], tests[0]);
} else {
await debugTest(vscode.workspace.workspaceFolders[0]);
}
}

async function debugTest(
workspaceFolder: vscode.WorkspaceFolder,
testItem?: vscode.TestItem
) {
let config = {
type: "pwa-node",
request: "launch",
name: "Debug Current Test File",
autoAttachChildProcesses: true,
skipFiles: ["<node_internals>/**", "**/node_modules/**"],
program: getVitestPath(workspaceFolder.uri.path),
args: [] as string[],
smartStep: true,
console: "integratedTerminal",
};

if (testItem) {
const data = WEAKMAP_TEST_DATA.get(testItem);
if (!data) {
console.error("Item not found");
return;
}

config.args = [
"run",
data.getFilePath(),
"--testNamePattern",
data.getFullPattern(),
];
} else {
config.args = ["run"];
}

if (config.program == null) {
vscode.window.showErrorMessage("Cannot find vitest");
return;
}

try {
vscode.debug.startDebugging(workspaceFolder, config);
} catch (e) {
console.error(`startDebugging error ${(e as any).toString()}`);
}
}

export async function runHandler(
ctrl: vscode.TestController,
request: vscode.TestRunRequest,
cancellation: vscode.CancellationToken
) {
if (vscode.workspace.workspaceFolders === undefined) {
if (
vscode.workspace.workspaceFolders === undefined ||
vscode.workspace.workspaceFolders.length === 0
) {
return;
}

Expand Down

0 comments on commit 50aafca

Please sign in to comment.