Skip to content

Chrome DevTools for coding agents

test: load DevTools in test setup only for files that use it (#3178)

Alex Rudenko6cfbdf933b8b
diff --git a/src/utils/url.ts b/src/utils/url.ts
index 08aa7953..29cea472 100644
--- a/src/utils/url.ts
+++ b/src/utils/url.ts
@@ -4,6 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/
+// Node 22 has no global URLPattern. Loading the third-party bundle installs
+// the URLPattern polyfill that Puppeteer ships.
+import '../third_party/index.js';
+
const ALLOWED_PROTOCOLS = new Set(['http:', 'https:', 'ws:', 'wss:']);
const IPV4_LOOPBACK_REGEX = /^127(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}$/;
diff --git a/tests/mocks.ts b/tests/mocks.ts
index e36c0e08..c56efe1d 100644
--- a/tests/mocks.ts
+++ b/tests/mocks.ts
@@ -22,6 +22,8 @@
* sinon.assert.calledOnceWithExactly(page.emulate, {networkConditions: 'Slow 3G'});
*/
+import './setupDevTools.js';
+
import type {Frame} from 'puppeteer-core';
import sinon from 'sinon';
diff --git a/tests/setup.ts b/tests/setup.ts
index aac0b8d4..2f01f9db 100644
--- a/tests/setup.ts
+++ b/tests/setup.ts
@@ -4,18 +4,11 @@
* SPDX-License-Identifier: Apache-2.0
*/
-import '../src/utils/polyfill.js';
-
import path from 'node:path';
-import {before, it} from 'node:test';
-
-import {overrideDevToolsGlobals} from '../src/devtools/DevtoolsUtils.js';
+import {it} from 'node:test';
-before(() => {
- overrideDevToolsGlobals({
- loadResource: async () => '',
- });
-});
+// This module runs for every test file, so it must not import src code. Test
+// files that use DevTools import tests/setupDevTools.ts.
// This is run by Node when we execute the tests via the --import flag.
it.snapshot.setResolveSnapshotPath(testPath => {
diff --git a/tests/setupDevTools.ts b/tests/setupDevTools.ts
new file mode 100644
index 00000000..39a3d212
--- /dev/null
+++ b/tests/setupDevTools.ts
@@ -0,0 +1,25 @@
+/**
+ * @license
+ * Copyright 2026 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/**
+ * Prepares the polyfills and the DevTools frontend globals for tests.
+ *
+ * Import this module from each test file that uses DevTools code.
+ * tests/utils.ts and tests/mocks.ts import it, so test files that import
+ * them get it automatically.
+ *
+ * This module is not part of tests/setup.ts because it loads
+ * src/third_party/index.js, which takes about 0.5 s. Many test files do not
+ * need it.
+ */
+
+import '../src/utils/polyfill.js';
+
+import {overrideDevToolsGlobals} from '../src/devtools/DevtoolsUtils.js';
+
+overrideDevToolsGlobals({
+ loadResource: async () => '',
+});
diff --git a/tests/trace-processing/parse.test.ts b/tests/trace-processing/parse.test.ts
index bed919b6..fb29bde7 100644
--- a/tests/trace-processing/parse.test.ts
+++ b/tests/trace-processing/parse.test.ts
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
+import '../setupDevTools.js';
+
import assert from 'node:assert';
import {afterEach, describe, it} from 'node:test';
diff --git a/tests/utils.ts b/tests/utils.ts
index 393f2d6d..d251881d 100644
--- a/tests/utils.ts
+++ b/tests/utils.ts
@@ -4,6 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
+import './setupDevTools.js';
+
import assert from 'node:assert';
import {spawn, type ChildProcess} from 'node:child_process';
import crypto from 'node:crypto';