Skip to content

Commit

Permalink
reuse vitest inspector config instead
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Feb 21, 2025
1 parent 112bf76 commit 0c1151a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
63 changes: 26 additions & 37 deletions packages/vitest-pool-workers/src/pool/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import inspector from "node:inspector";
import path from "node:path";
import {
formatZodError,
Expand All @@ -11,7 +10,7 @@ import { z } from "zod";
import { getProjectPath, getRelativeProjectPath, log } from "./helpers";
import type { ModuleRule, WorkerOptions } from "miniflare";
import type { ProvidedContext } from "vitest";
import type { WorkspaceProject } from "vitest/node";
import type { Vitest, WorkspaceProject } from "vitest/node";
import type { ParseParams, ZodError } from "zod";

export interface WorkersConfigPluginAPI {
Expand Down Expand Up @@ -49,9 +48,9 @@ const WorkersPoolOptionsSchema = z.object({
*/
singleWorker: z.boolean().default(false),
/**
* Specifies the inspector port to use for debugging. Defaults to 9229.
* Specifies the inspector port used to open the inspector. Defaults to 9229.
*/
inspectorPort: z.number().default(9229),
inspectorPort: z.number().optional(),
miniflare: z
.object({
workers: z.array(z.object({}).passthrough()).optional(),
Expand Down Expand Up @@ -140,18 +139,6 @@ function parseWorkerOptions(
return result;
}

function getNodeInspectorPort(): number | null {
const url = inspector.url();

if (!url) {
return null;
}

const port = new URL(url).port;

return Number(port);
}

async function parseCustomPoolOptions(
rootPath: string,
value: unknown,
Expand Down Expand Up @@ -205,27 +192,6 @@ async function parseCustomPoolOptions(
throw errorRef.value;
}

const nodeInspectorPort = getNodeInspectorPort();

if (
nodeInspectorPort === null ||
options.inspectorPort === nodeInspectorPort
) {
if (options.inspectorPort === nodeInspectorPort) {
console.warn(
`The inspector port ${nodeInspectorPort} is already in use by the Node inspector. ` +
`Update the "inspectorPort" option in the vitest config to debug your Workers tests.`
);
}

// Unset the inspector port so Miniflare doesn't try to use it
options.inspectorPort = undefined;
} else if (!options.singleWorker) {
log.warn(`Tests run in a single worker when the inspector is open.`);

options.singleWorker = true;
}

// Try to parse Wrangler config if any
if (options.wrangler?.configPath !== undefined) {
const configPath = path.resolve(rootPath, options.wrangler.configPath);
Expand Down Expand Up @@ -326,3 +292,26 @@ export async function parseProjectOptions(
);
}
}

export function setupInspector(
options: WorkersPoolOptionsWithDefines,
ctx: Vitest
): void {
if (!ctx.config.inspector.enabled) {
// If the inspector isn't enabled, unset the "inspectorPort" option
options.inspectorPort = undefined;
return;
}

if (!options.singleWorker) {
log.warn(`Tests run in a single worker when the inspector is open.`);

options.singleWorker = true;
}

// Fallback to the vitest inspector port if not specified
options.inspectorPort ??= ctx.config.inspector.port ?? 9229;

// Disable the default node inspector
ctx.config.inspector.enabled = false;
}
3 changes: 2 additions & 1 deletion packages/vitest-pool-workers/src/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { createMethodsRPC } from "vitest/node";
import { workerdBuiltinModules } from "../shared/builtin-modules";
import { createChunkingSocket } from "../shared/chunking-socket";
import { CompatibilityFlagAssertions } from "./compatibility-flag-assertions";
import { OPTIONS_PATH, parseProjectOptions } from "./config";
import { OPTIONS_PATH, parseProjectOptions, setupInspector } from "./config";
import {
getProjectPath,
getRelativeProjectPath,
Expand Down Expand Up @@ -895,6 +895,7 @@ async function executeMethod(
testFiles: new Set(),
relativePath: getRelativeProjectPath(project),
};
setupInspector(workersProject.options, ctx);
allProjects.set(projectName, workersProject);
} else if (!parsedProjectOptions.has(project)) {
workersProject.project = project;
Expand Down

0 comments on commit 0c1151a

Please sign in to comment.