Skip to content

Chrome DevTools for coding agents

build: define a node_script_test target for the test suite (#3347)

Nikolay Vitkova32fef39f366
diff --git a/BUILD.gn b/BUILD.gn
index 5b69e71b..8724a3d1 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -2,10 +2,12 @@
# SPDX-License-Identifier: Apache-2.0
group("default") {
+ testonly = true
deps = [
":devtools_deps",
"//src:all",
"//tests",
+ "//tests:run_chrome_devtools_mcp_tests",
]
}
diff --git a/scripts/test.js b/scripts/test.js
index afbc8807..b47bb49a 100644
--- a/scripts/test.js
+++ b/scripts/test.js
@@ -8,42 +8,78 @@
// Node 20 does not support --experimental-strip-types flag.
import {spawn} from 'node:child_process';
-import {readFile} from 'node:fs/promises';
+import {glob, readFile} from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
+import {parseArgs} from 'node:util';
-const args = process.argv.slice(2);
-const userArgs = args.filter(arg => !arg.startsWith('-'));
-const flags = args.filter(arg => arg.startsWith('-'));
+const callerCwd = process.cwd();
+const projectRoot = path.resolve(import.meta.dirname, '..');
-const files = [];
+const ownOptions = {
+ retry: {
+ type: 'boolean',
+ default: false,
+ },
+ 'build-dir': {
+ type: 'string',
+ },
+};
-let shouldRetry = false;
-const retryIndex = flags.indexOf('--retry');
-if (retryIndex !== -1) {
- shouldRetry = true;
- flags.splice(retryIndex, 1);
-}
+const {
+ values,
+ positionals: userArgs,
+ tokens,
+} = parseArgs({
+ options: ownOptions,
+ strict: false,
+ allowPositionals: true,
+ tokens: true,
+});
+
+const shouldRetry = values.retry;
+const buildDir = values['build-dir']
+ ? path.resolve(callerCwd, values['build-dir'])
+ : path.join(projectRoot, 'out', 'Default');
+
+const flags = tokens
+ .filter(token => token.kind === 'option' && !(token.name in ownOptions))
+ .map(token =>
+ token.value !== undefined
+ ? `${token.rawName}=${token.value}`
+ : token.rawName,
+ );
+
+const relGenDir = path
+ .relative(projectRoot, path.join(buildDir, 'gen'))
+ .replaceAll('\\', '/');
+const relGenTestsDir = `${relGenDir}/tests`;
+
+const files = [];
if (userArgs.length > 0) {
for (const arg of userArgs) {
- // Map .ts files to out/Default/gen/ .js files
+ // Map .ts files to <buildDir>/gen/ .js files
let testPath = arg;
if (testPath.endsWith('.ts')) {
testPath = testPath.replace(/\.ts$/, '.js');
- if (!testPath.startsWith('out/Default/gen/')) {
- testPath = path.join('out/Default/gen', testPath);
+ if (!testPath.startsWith(`${relGenDir}/`)) {
+ testPath = path.posix.join(relGenDir, testPath.replaceAll('\\', '/'));
}
}
files.push(testPath);
}
} else if (flags.includes('--test-only')) {
- const {glob} = await import('node:fs/promises');
- for await (const tsFile of glob('tests/**/*.test.ts')) {
- const content = await readFile(tsFile, 'utf8');
+ for await (const tsFile of glob('tests/**/*.test.ts', {cwd: projectRoot})) {
+ const content = await readFile(path.join(projectRoot, tsFile), 'utf8');
if (content.includes('.only(')) {
- files.push(path.join('out/Default/gen', tsFile.replace(/\.ts$/, '.js')));
+ files.push(
+ path.posix.join(
+ relGenDir,
+ tsFile.replaceAll('\\', '/').replace(/\.ts$/, '.js'),
+ ),
+ );
}
}
if (files.length === 0) {
@@ -51,7 +87,7 @@ if (userArgs.length > 0) {
process.exit(0);
}
} else if (files.length === 0) {
- files.push('out/Default/gen/tests/**/*.test.js');
+ files.push(`${relGenTestsDir}/**/*.test.js`);
}
// Node runs `availableParallelism() - 1` test files at a time by default, so
@@ -66,7 +102,7 @@ if (!flags.some(flag => flag.startsWith('--test-concurrency'))) {
const nodeArgs = [
'--import',
- './out/Default/gen/tests/setup.js',
+ `./${relGenTestsDir}/setup.js`,
'--no-warnings=ExperimentalWarning',
'--test-reporter',
(process.env['NODE_TEST_REPORTER'] ?? process.env['CI']) ? 'spec' : 'dot',
@@ -82,7 +118,8 @@ async function runTests(attempt) {
console.log(`\nRun attempt ${attempt}...\n`);
}
return await new Promise(resolve => {
- const child = spawn('node', nodeArgs, {
+ const child = spawn(process.execPath, nodeArgs, {
+ cwd: projectRoot,
stdio: 'inherit',
env: {
...process.env,
diff --git a/tests/BUILD.gn b/tests/BUILD.gn
index 51803b91..2859e721 100644
--- a/tests/BUILD.gn
+++ b/tests/BUILD.gn
@@ -1,10 +1,12 @@
# Copyright 2026 Google LLC
# SPDX-License-Identifier: Apache-2.0
+import("//third_party/devtools-frontend/scripts/build/ninja/script_test.gni")
import(
"//third_party/devtools-frontend/scripts/build/typescript/typescript.gni")
ts_library("tests") {
+ testonly = true
runs_in = "node"
es_libs = [
@@ -122,3 +124,24 @@ ts_library("tests") {
"//src:third_party",
]
}
+
+node_script_test("run_chrome_devtools_mcp_tests") {
+ script = "//scripts/test.js"
+ args = [
+ "--build-dir",
+ "@WrappedPath(.)",
+
+ # THIRD_PARTY_NOTICES is generated by `npm run bundle`, not by GN.
+ "--test-skip-pattern=THIRD_PARTY_NOTICES",
+ ]
+ data = [
+ "//node_modules/",
+ "//package.json",
+ "//tests/",
+ ]
+ data_deps = [
+ ":tests",
+ "//:devtools_deps",
+ "//src:all",
+ ]
+}
diff --git a/tests/McpServer.test.ts b/tests/McpServer.test.ts
index 5dd971ff..e488b15e 100644
--- a/tests/McpServer.test.ts
+++ b/tests/McpServer.test.ts
@@ -866,7 +866,9 @@ describe('McpServer', () => {
async function getToolsWithFilteredCategories(
filterOutCategories: ToolCategory[] = [],
): Promise<string[]> {
- const files = fs.readdirSync('out/Default/gen/src/tools');
+ const files = fs.readdirSync(
+ path.resolve(import.meta.dirname, '../src/tools'),
+ );
const definedNames = [];
for (const file of files) {
if (
diff --git a/tests/e2e/telemetry.test.ts b/tests/e2e/telemetry.test.ts
index f6cc456b..1948dcdf 100644
--- a/tests/e2e/telemetry.test.ts
+++ b/tests/e2e/telemetry.test.ts
@@ -14,7 +14,8 @@ import {describe, it} from 'node:test';
import type {ChromeDevToolsMcpExtension} from '../../src/telemetry/types';
const SERVER_PATH = path.resolve(
- 'out/Default/gen/src/bin/chrome-devtools-mcp.js',
+ import.meta.dirname,
+ '../../src/bin/chrome-devtools-mcp.js',
);
interface MockServerContext {
diff --git a/tests/index.test.ts b/tests/index.test.ts
index 8fae9207..93013afc 100644
--- a/tests/index.test.ts
+++ b/tests/index.test.ts
@@ -32,9 +32,12 @@ describe('e2e', {concurrency: true}, () => {
let attempt = 1;
while (attempt <= 3) {
const transport = new StdioClientTransport({
- command: 'node',
+ command: process.execPath,
args: [
- 'out/Default/gen/src/bin/chrome-devtools-mcp.js',
+ path.resolve(
+ import.meta.dirname,
+ '../src/bin/chrome-devtools-mcp.js',
+ ),
'--headless',
'--isolated',
'--executable-path',
diff --git a/tests/setup.ts b/tests/setup.ts
index 86b689c5..dc4a4cde 100644
--- a/tests/setup.ts
+++ b/tests/setup.ts
@@ -20,13 +20,13 @@ afterEach(() => {
// This is run by Node when we execute the tests via the --import flag.
it.snapshot.setResolveSnapshotPath(testPath => {
+ if (!testPath) {
+ return '';
+ }
// By default the snapshots go into the build directory, but we want them
- // in the tests/ directory.
- const correctPath = testPath?.replace(
- path.join('out', 'Default', 'gen', 'tests'),
- 'tests',
- );
- return correctPath + '.snapshot';
+ // in the source tests/ directory regardless of the GN output directory.
+ const relativePath = path.relative(import.meta.dirname, testPath);
+ return path.join(path.resolve('tests'), `${relativePath}.snapshot`);
});
// The default serializer is JSON.stringify which outputs a very hard to read
diff --git a/tests/shutdown.test.ts b/tests/shutdown.test.ts
index ac1a950f..ee48dcc3 100644
--- a/tests/shutdown.test.ts
+++ b/tests/shutdown.test.ts
@@ -6,6 +6,7 @@
import assert from 'node:assert';
import {spawn, type ChildProcessByStdio} from 'node:child_process';
+import path from 'node:path';
import type {Readable, Writable} from 'node:stream';
import {describe, it} from 'node:test';
@@ -23,9 +24,9 @@ const EXIT_TIMEOUT_MS = 15000;
async function spawnServer(): Promise<Server> {
const child = spawn(
- 'node',
+ process.execPath,
[
- 'out/Default/gen/src/bin/chrome-devtools-mcp.js',
+ path.resolve(import.meta.dirname, '../src/bin/chrome-devtools-mcp.js'),
'--headless',
'--isolated',
'--executable-path',
diff --git a/tests/third_party_notices.test.ts b/tests/third_party_notices.test.ts
index 1c9b1986..18174a17 100644
--- a/tests/third_party_notices.test.ts
+++ b/tests/third_party_notices.test.ts
@@ -10,9 +10,9 @@ import {describe, it} from 'node:test';
describe('THIRD_PARTY_NOTICES', () => {
it('matches snapshot', t => {
- const noticesPath = path.join(
- process.cwd(),
- 'out/Default/gen/src/third_party/THIRD_PARTY_NOTICES',
+ const noticesPath = path.resolve(
+ import.meta.dirname,
+ '../src/third_party/THIRD_PARTY_NOTICES',
);
if (!fs.existsSync(noticesPath)) {
throw new Error(
diff --git a/tests/utils.ts b/tests/utils.ts
index 622988d3..ca52a165 100644
--- a/tests/utils.ts
+++ b/tests/utils.ts
@@ -475,7 +475,8 @@ export function getMockBrowser(options?: {
}
export const CLI_PATH: string = path.resolve(
- 'out/Default/gen/src/bin/chrome-devtools.js',
+ import.meta.dirname,
+ '../src/bin/chrome-devtools.js',
);
export async function runCli(
@@ -489,7 +490,7 @@ export async function runCli(
// Prepend so that the flag is not swallowed by a trailing `--`.
finalArgs.unshift('--sessionId', sessionId);
}
- const child = spawn('node', [CLI_PATH, ...finalArgs], {
+ const child = spawn(process.execPath, [CLI_PATH, ...finalArgs], {
env: process.env,
cwd: options?.cwd,
});