Skip to content

Commit

Permalink
fix: use .bin/vitest by default
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed May 8, 2022
1 parent eaf8782 commit 852cb41
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import * as vscode from "vscode";
import { extensionId, getConfig } from "./config";
import { TestFileDiscoverer } from "./discover";
import { isVitestEnv } from "./pure/isVitestEnv";
import { getVitestPath, getVitestVersion } from "./pure/utils";
import {
getVitestCommand,
getVitestPath,
getVitestVersion,
} from "./pure/utils";
import { debugHandler, runHandler, updateSnapshot } from "./runHandler";
import { TestFile, WEAKMAP_TEST_DATA } from "./TestData";
import semver from "semver";
Expand Down Expand Up @@ -50,11 +54,9 @@ export async function activate(context: vscode.ExtensionContext) {
}
};

const vitestPath = getVitestPath(
const vitestVersion = await getVitestVersion(getVitestCommand(
vscode.workspace.workspaceFolders[0].uri.fsPath,
);

const vitestVersion = await getVitestVersion(vitestPath);
));
console.dir({ vitestVersion });
if (semver.gte(vitestVersion, "0.8.0")) {
registerRunHandler(ctrl);
Expand Down
8 changes: 6 additions & 2 deletions src/pure/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ export interface FormattedTestResults {
export class TestRunner {
constructor(
private workspacePath: string,
private vitestPath: string | undefined,
private vitestCommand: string | undefined,
) {}

async scheduleRun(
testFile: string[] | undefined,
testNamePattern: string | undefined,
log: (msg: string) => void = () => {},
workspaceEnv: Record<string, string> = {},
vitestCommand: string[] = ["npx", "vitest"],
vitestCommand: string[] = this.vitestCommand
? [this.vitestCommand]
: ["npx", "vitest"],
updateSnapshot = false,
): Promise<FormattedTestResults> {
const path = getTempPath();
Expand Down Expand Up @@ -108,6 +110,8 @@ export class TestRunner {
stdio: ["ignore", "pipe", "pipe"],
env,
shell: isWindows,
// https://nodejs.org/api/child_process.html#child_process_options_detached
detached: process.platform !== "win32",
});

for await (
Expand Down
29 changes: 26 additions & 3 deletions src/pure/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,37 @@ export function getVitestPath(projectRoot: string): string | undefined {
return;
}

export async function getVitestVersion(vitestPath?: string): Promise<string> {
export function getVitestCommand(projectRoot: string): string | undefined {
const node_modules = path.resolve(projectRoot, "node_modules");
if (!existsSync(node_modules)) {
return;
}

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

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

return;
}

export async function getVitestVersion(
vitestCommand?: string,
): Promise<string> {
let process;
if (vitestPath == null) {
if (vitestCommand == null) {
process = spawn("npx", ["vitest", "-v"], {
stdio: ["ignore", "pipe", "pipe"],
});
} else {
process = spawn("node", [vitestPath, "-v"], {
process = spawn(vitestCommand, ["-v"], {
stdio: ["ignore", "pipe", "pipe"],
});
}
Expand Down
10 changes: 7 additions & 3 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
getTempPath,
TestRunner,
} from "./pure/runner";
import { getVitestPath, sanitizeFilePath } from "./pure/utils";
import {
getVitestCommand,
getVitestPath,
sanitizeFilePath,
} from "./pure/utils";
import { relative } from "path";
import {
getAllTestCases,
Expand All @@ -32,7 +36,7 @@ export async function runHandler(

const runner = new TestRunner(
vscode.workspace.workspaceFolders[0].uri.fsPath,
getVitestPath(vscode.workspace.workspaceFolders[0].uri.fsPath),
getVitestCommand(vscode.workspace.workspaceFolders[0].uri.fsPath),
);

const tests = request.include ?? gatherTestItems(ctrl.items);
Expand All @@ -55,7 +59,7 @@ export async function updateSnapshot(
test = testItemIdMap.get(ctrl)!.get(test.id)!;
const runner = new TestRunner(
vscode.workspace.workspaceFolders[0].uri.fsPath,
getVitestPath(vscode.workspace.workspaceFolders[0].uri.fsPath),
getVitestCommand(vscode.workspace.workspaceFolders[0].uri.fsPath),
);

const request = new vscode.TestRunRequest([test]);
Expand Down

0 comments on commit 852cb41

Please sign in to comment.