Skip to content

Commit 373b3bf

Browse files
committed
fix(test): explain missing vitest dependency
1 parent d5bf325 commit 373b3bf

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎scripts/run-vitest.mjs‎

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { spawn } from "node:child_process";
22
import fs from "node:fs";
33
import { createRequire } from "node:module";
44
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
56
import { isUiTestTarget, isUnitUiTestTarget } from "../test/vitest/vitest.ui-paths.mjs";
67
import { resolveLocalVitestEnv } from "./lib/vitest-local-scheduling.mjs";
78
import { spawnPnpmRunner } from "./pnpm-runner.mjs";
@@ -73,6 +74,7 @@ const VITEST_DOTTED_OPTIONS_WITH_VALUE_PREFIXES = [
7374
"--typecheck.",
7475
];
7576
const require = createRequire(import.meta.url);
77+
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
7678

7779
function isTruthyEnvValue(value) {
7880
return TRUTHY_ENV_VALUES.has(value?.trim().toLowerCase() ?? "");
@@ -91,8 +93,43 @@ export function resolveVitestNodeArgs(env = process.env) {
9193
return ["--no-maglev"];
9294
}
9395

94-
export function resolveVitestCliEntry() {
95-
const vitestPackageJson = require.resolve("vitest/package.json");
96+
function isMissingVitestResolveError(error) {
97+
return (
98+
error instanceof Error &&
99+
error.code === "MODULE_NOT_FOUND" &&
100+
error.message.includes("vitest/package.json")
101+
);
102+
}
103+
104+
export function resolveMissingVitestDependencyMessage(baseDir = repoRoot, fsImpl = fs) {
105+
const hasNodeModules = fsImpl.existsSync(path.join(baseDir, "node_modules"));
106+
const reason = hasNodeModules
107+
? "[vitest] Vitest is not installed in node_modules."
108+
: "[vitest] node_modules is missing; Vitest cannot be resolved.";
109+
return [
110+
reason,
111+
"Install dependencies before running scripts/run-vitest.mjs:",
112+
" pnpm install --frozen-lockfile",
113+
"For raw Crabbox/AWS macOS source syncs, hydrate or install dependencies before this runner.",
114+
].join("\n");
115+
}
116+
117+
export function resolveVitestCliEntry({
118+
baseDir = repoRoot,
119+
fsImpl = fs,
120+
requireResolve = require.resolve.bind(require),
121+
} = {}) {
122+
let vitestPackageJson;
123+
try {
124+
vitestPackageJson = requireResolve("vitest/package.json");
125+
} catch (error) {
126+
if (isMissingVitestResolveError(error)) {
127+
const wrappedError = new Error(resolveMissingVitestDependencyMessage(baseDir, fsImpl));
128+
wrappedError.code = "OPENCLAW_MISSING_VITEST";
129+
throw wrappedError;
130+
}
131+
throw error;
132+
}
96133
return path.join(path.dirname(vitestPackageJson), "vitest.mjs");
97134
}
98135

@@ -529,12 +566,23 @@ function main(argv = process.argv.slice(2), env = process.env) {
529566

530567
const vitestArgs = resolveImplicitVitestArgs(argv);
531568
const spawnEnv = resolveRunVitestSpawnEnv(env, vitestArgs);
569+
let vitestCliEntry;
570+
try {
571+
vitestCliEntry = resolveVitestCliEntry();
572+
} catch (error) {
573+
if (error instanceof Error && error.code === "OPENCLAW_MISSING_VITEST") {
574+
console.error(error.message);
575+
process.exit(1);
576+
}
577+
throw error;
578+
}
579+
532580
const { child, teardown } = spawnWatchedVitestProcess({
533581
pnpmArgs: [
534582
"exec",
535583
"node",
536584
...resolveVitestNodeArgs(env),
537-
resolveVitestCliEntry(),
585+
vitestCliEntry,
538586
...vitestArgs,
539587
],
540588
spawnParams: resolveVitestSpawnParams(spawnEnv),

‎test/scripts/run-vitest.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
installVitestNoOutputWatchdog,
55
resolveDirectNodeVitestArgs,
66
resolveImplicitVitestArgs,
7+
resolveMissingVitestDependencyMessage,
78
resolveMissingExplicitTestFiles,
89
resolveRunVitestSpawnEnv,
10+
resolveVitestCliEntry,
911
resolveVitestNodeArgs,
1012
resolveVitestNoOutputTimeoutMs,
1113
resolveVitestSpawnParams,
@@ -29,6 +31,36 @@ describe("scripts/run-vitest", () => {
2931
expect(resolveDirectNodeVitestArgs(["exec", "vitest", "run"])).toBeNull();
3032
});
3133

34+
it("reports an actionable error when Vitest cannot be resolved", () => {
35+
const error = new Error("Cannot find module 'vitest/package.json'");
36+
(error as NodeJS.ErrnoException).code = "MODULE_NOT_FOUND";
37+
38+
expect(() =>
39+
resolveVitestCliEntry({
40+
baseDir: "/repo",
41+
fsImpl: { existsSync: () => false },
42+
requireResolve: () => {
43+
throw error;
44+
},
45+
}),
46+
).toThrow(
47+
[
48+
"[vitest] node_modules is missing; Vitest cannot be resolved.",
49+
"Install dependencies before running scripts/run-vitest.mjs:",
50+
" pnpm install --frozen-lockfile",
51+
"For raw Crabbox/AWS macOS source syncs, hydrate or install dependencies before this runner.",
52+
].join("\n"),
53+
);
54+
});
55+
56+
it("distinguishes missing Vitest from a completely missing dependency install", () => {
57+
expect(
58+
resolveMissingVitestDependencyMessage("/repo", {
59+
existsSync: (filePath: string) => filePath.replaceAll("\\", "/").endsWith("node_modules"),
60+
}),
61+
).toContain("[vitest] Vitest is not installed in node_modules.");
62+
});
63+
3264
it("routes explicit unit ui tests through the narrow unit ui config", () => {
3365
expect(
3466
resolveImplicitVitestArgs([

0 commit comments

Comments
 (0)