Migrating unit tests to foundation unit tests using TestUniverse and devtools_foundation_module. Use when moving tests away from DOM-heavy helpers like describeWithEnvironment or describeWithMockConnection.
93%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
This skill provides guidance on migrating DevTools unit tests to the "foundation" pattern, which is lighter, avoids global singletons, and is compatible with both Node and Browser runtimes (Isomorphic).
Use this template for modules that should be platform-agnostic.
FileReader or layout metrics) or heavy DevTools dependencies. Use Universe to access services.TestUniverse is the preferred way to setup a DevTools-like environment for tests without global singletons.
Common.Settings.Settings.instance()).DevToolsContext to manage dependencies.Do not use describeWithEnvironment or describeWithMockConnection; both are banned in new and migrated unit tests.
Instead, use standard describe and initialize environment hooks at the top-level describe block. Crucial: Without setupRuntimeHooks, tests creating SDK models will crash due to uninitialized experiments (e.g. capture-node-creation-stacks).
import {setupLocaleHooks} from '../../testing/LocaleHelpers.js';
import {setupSettingsHooks} from '../../testing/SettingsHelpers.js';
import {setupRuntimeHooks} from '../../testing/RuntimeHelpers.js';
import {TestUniverse} from '../../testing/TestUniverse.js';
describe('MyComponent', () => {
setupLocaleHooks();
setupSettingsHooks();
setupRuntimeHooks();
let universe: TestUniverse;
beforeEach(() => {
universe = new TestUniverse();
});
});Instead of using SDK.TargetManager.TargetManager.instance(), use universe.targetManager. Use universe.createTarget() instead of the global createTarget.
// OLD
const target = createTarget();
const targetManager = SDK.TargetManager.TargetManager.instance();
// NEW
const target = universe.createTarget({url: urlString`http://example.com/`});
const targetManager = universe.targetManager;If the test used describeWithMockConnection and global setMockConnectionResponseHandler to stub CDP traffic, you can migrate this by passing a MockCDPConnection to universe.createTarget(). This allows stubbing out CDP traffic scoped to a specific target tree rather than globally.
import {MockCDPConnection} from '../../testing/MockCDPConnection.js';
const cdpConnection = new MockCDPConnection([
{
method: 'Network.getResponseBody',
response: () => ({body: 'mocked body', base64Encoded: false}),
}
]);
const target = universe.createTarget({connection: cdpConnection});[!TIP] Legacy Target URLs:
EnvironmentHelpers.createTarget()defaults tohttp://example.com/.TestUniverse.createTarget()defaults toabout:blank. If your test asserts against specific URLs, remember to pass the URL explicitly.
For large integration tests, you may encounter code that strictly calls SomeModule.instance() or uses complex legacy helpers (like createWorkspaceProject).
Do not use setUpEnvironment() as it will create disconnected singletons. Instead, wire the singletons to your TestUniverse and stub the globals to bridge legacy helpers:
beforeEach(async () => {
universe = new TestUniverse();
const {targetManager, workspace, settings} = universe;
// 1. Stub globals so legacy helpers use TestUniverse components
sinon.stub(Workspace.Workspace.WorkspaceImpl, 'instance').returns(workspace);
sinon.stub(SDK.TargetManager.TargetManager, 'instance').returns(targetManager);
sinon.stub(Common.Settings.Settings, 'instance').returns(settings);
// 2. Initialize interdependent singletons in the correct order
SDK.NetworkManager.MultitargetNetworkManager.instance({forceNew: true, targetManager});
// 3. Now safe to use legacy helpers that rely on the above instances
await createWorkspaceProject(urlString`file:///path/to/overrides`, [...]);
});assert.deepEqual)Protocol requests/responses dynamically generated by Chrome (like Network conditions) can vary slightly (e.g., adding connectionType, urlPattern).
assert.deepEqual(rules, [{...}]) will flake if unrequested fields are present.sinon.spy() for handlers and assert with sinon.assert.calledOnceWithMatch:const emulateSpy = sinon.spy();
connection.setSuccessHandler('Network.emulateNetworkConditionsByRule', request => {
emulateSpy(request);
return {ruleIds: []};
});
// Matches only the fields you care about, ignoring extra protocol fields
sinon.assert.calledOnceWithMatch(emulateSpy, {
offline: false,
matchedNetworkConditions: [sinon.match({ downloadThroughput: 1000 })],
});ReferenceError: FileReader is not defined)Foundation tests run in Node.js where window, FileReader, and certain DOM string encodings don't exist.
btoa(), Uint8Array).front_end/core/platform/api/HostRuntime.ts.If a test times out (5000ms exceeded), it is usually an unhandled promise caused by a missing singleton. Double-check the constructor of the failing manager to see which instance() it listens to, and ensure that dependent singleton was created first.
When a module and its tests are ready, update BUILD.gn:
devtools_module to devtools_foundation_module for the module and
define its tests with devtools_foundation_module("foundation_unittests")
referencing :bundle and ../../testing in ts_deps (TypeScript targets
belong in ts_deps so GN generates tsconfig project references, whereas
deps is reserved for non-TypeScript generator targets).":foundation_unittests" target into
group("foundation_unittests") in front_end/BUILD.gn (and remove any
replaced ":unittests" entry from group("unittests")) so GN emits the
tests into foundation_tests.txt. Unwired foundation_unittests targets
compile when built directly and silently never run in ./dt test or on CQ.# front_end/models/my_module/BUILD.gn
devtools_foundation_module("my_module") {
sources = [ "MyModule.ts" ]
ts_deps = [ "../../core/common:bundle" ]
}
devtools_entrypoint("bundle") {
entrypoint = "my_module.ts"
ts_deps = [ ":my_module" ]
visibility = [ ":*" ]
}
devtools_foundation_module("foundation_unittests") {
testonly = true
sources = [ "MyModule.test.ts" ]
ts_deps = [
":bundle",
"../../testing",
]
}Foundation tests must pass in both Node.js and Browser runtimes.
autoninja -C out/Default front_end/models/my_module:foundation_unittests./dt test front_end/core/sdk/NetworkManager.test.ts --node-unit-tests./dt test front_end/core/sdk/NetworkManager.test.tsChromeDevTools/devtools-frontend
9d90722
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.