Skip to content

Chrome DevTools for coding agents

test: return JSON for stabilizeStructuredContent (#2560)

Nikolay Vitkov7cebcf0f6dfc
diff --git a/tests/McpContext.test.ts b/tests/McpContext.test.ts
index 2723bfd7..2af2277e 100644
--- a/tests/McpContext.test.ts
+++ b/tests/McpContext.test.ts
@@ -22,7 +22,13 @@ import {TextSnapshot} from '../src/TextSnapshot.js';
import {type HTTPResponse} from '../src/third_party/index.js';
import type {TraceResult} from '../src/trace-processing/parse.js';
-import {getMockRequest, html, withBrowser, withMcpContext} from './utils.js';
+import {
+ getMockRequest,
+ html,
+ withBrowser,
+ withMcpContext,
+ stabilizeStructuredContent,
+} from './utils.js';
describe('McpContext', () => {
afterEach(() => {
@@ -317,7 +323,7 @@ describe('McpContext', () => {
response.setIncludeNetworkRequests(true);
const result = await response.handle(context);
- t.assert.snapshot(JSON.stringify(result.structuredContent, null, 2));
+ t.assert.snapshot(stabilizeStructuredContent(result.structuredContent));
});
});
@@ -335,7 +341,7 @@ describe('McpContext', () => {
response.attachNetworkRequest(456);
const result = await response.handle(context);
- t.assert.snapshot(JSON.stringify(result.structuredContent, null, 2));
+ t.assert.snapshot(stabilizeStructuredContent(result.structuredContent));
});
});
@@ -386,7 +392,7 @@ describe('McpContext', () => {
});
const result = await response.handle(context);
- t.assert.snapshot(JSON.stringify(result.structuredContent, null, 2));
+ t.assert.snapshot(stabilizeStructuredContent(result.structuredContent));
fromStub.restore();
});
diff --git a/tests/McpResponse.test.ts b/tests/McpResponse.test.ts
index e4e097c8..3f80bc68 100644
--- a/tests/McpResponse.test.ts
+++ b/tests/McpResponse.test.ts
@@ -50,9 +50,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.equal(content[0].type, 'text');
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -89,9 +87,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.equal(content[0].type, 'text');
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -101,9 +97,7 @@ describe('McpResponse', () => {
page.accessibility.snapshot = async () => null;
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -121,9 +115,7 @@ describe('McpResponse', () => {
response.includeSnapshot();
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -142,9 +134,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.equal(content[0].type, 'text');
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -158,7 +148,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.equal(content[0].type, 'text');
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(JSON.stringify(structuredContent, null, 2));
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -175,13 +165,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.equal(content[0].type, 'text');
t.assert.snapshot(stabilizeResponseOutput(getTextContent(content[0])));
- t.assert.snapshot(
- JSON.stringify(
- stabilizeStructuredContent(structuredContent),
- null,
- 2,
- ),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
const content = await readFile(filePath, 'utf-8');
t.assert.snapshot(stabilizeResponseOutput(content));
@@ -304,13 +288,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.equal(content[0].type, 'text');
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(
- stabilizeStructuredContent(structuredContent),
- null,
- 2,
- ),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
},
{navigationTimeout: 10000},
);
@@ -321,9 +299,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
await context.getSelectedMcpPage().emulate({});
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
it('adds image when image is attached', async t => {
@@ -334,9 +310,7 @@ describe('McpResponse', () => {
assert.equal(content[1].type, 'image');
assert.strictEqual(getImageContent(content[1]).data, 'imageBase64');
assert.strictEqual(getImageContent(content[1]).mimeType, 'image/png');
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -345,9 +319,7 @@ describe('McpResponse', () => {
await context.getSelectedMcpPage().emulate({cpuThrottlingRate: 4});
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -356,9 +328,7 @@ describe('McpResponse', () => {
await context.getSelectedMcpPage().emulate({cpuThrottlingRate: 1});
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -369,9 +339,7 @@ describe('McpResponse', () => {
});
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -380,9 +348,7 @@ describe('McpResponse', () => {
await context.getSelectedMcpPage().emulate({userAgent: 'MyUA'});
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -391,9 +357,7 @@ describe('McpResponse', () => {
await context.getSelectedMcpPage().emulate({colorScheme: 'dark'});
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -412,9 +376,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
await page.getDialog()?.dismiss();
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -433,9 +395,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
await page.getDialog()?.dismiss();
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -447,9 +407,7 @@ describe('McpResponse', () => {
};
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -461,9 +419,7 @@ describe('McpResponse', () => {
};
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -496,9 +452,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -515,9 +469,7 @@ describe('McpResponse', () => {
response.attachNetworkRequest(1);
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -537,9 +489,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.ok(getTextContent(content[0]));
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -549,9 +499,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
assert.ok(getTextContent(content[0]));
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -571,9 +519,7 @@ describe('McpResponse', () => {
const {content, structuredContent} = await response.handle(context);
const text = getTextContent(content[0]);
assert.ok(text.includes('<no console messages found>'));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -615,9 +561,7 @@ describe('McpResponse network request filtering', () => {
};
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -635,9 +579,7 @@ describe('McpResponse network request filtering', () => {
};
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -655,9 +597,7 @@ describe('McpResponse network request filtering', () => {
};
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -676,9 +616,7 @@ describe('McpResponse network request filtering', () => {
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -698,9 +636,7 @@ describe('McpResponse network request filtering', () => {
};
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
});
@@ -716,9 +652,7 @@ describe('McpResponse network pagination', () => {
assert.ok(text.includes('Showing 1-5 of 5 (Page 1 of 1).'));
assert.ok(!text.includes('Next page:'));
assert.ok(!text.includes('Previous page:'));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -736,9 +670,7 @@ describe('McpResponse network pagination', () => {
assert.ok(text.includes('Showing 1-10 of 30 (Page 1 of 3).'));
assert.ok(text.includes('Next page: 1'));
assert.ok(!text.includes('Previous page:'));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -757,9 +689,7 @@ describe('McpResponse network pagination', () => {
assert.ok(text.includes('Showing 11-20 of 25 (Page 2 of 3).'));
assert.ok(text.includes('Next page: 2'));
assert.ok(text.includes('Previous page: 0'));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -794,9 +724,7 @@ describe('McpResponse network pagination', () => {
text.includes('Invalid page number provided. Showing first page.'),
);
assert.ok(text.includes('Showing 1-2 of 5 (Page 1 of 3).'));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -818,10 +746,10 @@ describe('McpResponse network pagination', () => {
traceInsights?: unknown[];
};
t.assert.snapshot(
- JSON.stringify(typedStructuredContent.traceSummary, null, 2),
+ stabilizeStructuredContent(typedStructuredContent.traceSummary),
);
t.assert.snapshot(
- JSON.stringify(typedStructuredContent.traceInsights, null, 2),
+ stabilizeStructuredContent(typedStructuredContent.traceInsights),
);
});
});
@@ -844,13 +772,7 @@ describe('McpResponse network pagination', () => {
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(
- stabilizeStructuredContent(structuredContent),
- null,
- 2,
- ),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
@@ -870,13 +792,7 @@ describe('McpResponse network pagination', () => {
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(
- stabilizeStructuredContent(structuredContent),
- null,
- 2,
- ),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
});
@@ -925,7 +841,7 @@ describe('extensions', () => {
const {content, structuredContent} = await response.handle(context);
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(JSON.stringify(structuredContent, null, 2));
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
});
@@ -965,9 +881,7 @@ describe('lighthouse', () => {
assert.ok(text.includes('- /tmp/report.html'));
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(stabilizeStructuredContent(structuredContent), null, 2),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
});
});
});
@@ -1030,7 +944,7 @@ describe('third-party developer tools', () => {
responseText.includes('inputSchema={"type":"object"'),
'Response should include inputSchema',
);
- t.assert.snapshot(JSON.stringify(structuredContent, null, 2));
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
},
undefined,
{categoryExperimentalThirdParty: true},
@@ -1171,13 +1085,7 @@ describe('webmcp', () => {
const {content, structuredContent} = await response.handle(context);
assert.ok(getTextContent(content[0]));
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(
- stabilizeStructuredContent(structuredContent),
- null,
- 2,
- ),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
},
{args: ['--enable-features=WebMCP,DevToolsWebMCPSupport']},
parseArguments,
@@ -1229,13 +1137,7 @@ describe('webmcp', () => {
const {content, structuredContent} = await response.handle(context);
assert.ok(getTextContent(content[0]));
t.assert.snapshot(getTextContent(content[0]));
- t.assert.snapshot(
- JSON.stringify(
- stabilizeStructuredContent(structuredContent),
- null,
- 2,
- ),
- );
+ t.assert.snapshot(stabilizeStructuredContent(structuredContent));
},
{args: ['--enable-features=WebMCP,DevToolsWebMCPSupport']},
{categoryExperimentalWebmcp: true},
diff --git a/tests/formatters/NetworkFormatter.test.ts b/tests/formatters/NetworkFormatter.test.ts
index ba883308..7a75fd35 100644
--- a/tests/formatters/NetworkFormatter.test.ts
+++ b/tests/formatters/NetworkFormatter.test.ts
@@ -335,8 +335,7 @@ describe('NetworkFormatter', () => {
assert.deepStrictEqual(
textOrder,
jsonOrder,
- `redirect chain order differs between text (${JSON.stringify(textOrder)}) ` +
- `and JSON (${JSON.stringify(jsonOrder)})`,
+ `redirect chain order differs between text and JSON`,
);
});
it('shows saved to file message in toStringDetailed', async () => {
diff --git a/tests/tools/console.test.ts b/tests/tools/console.test.ts
index 65ce959f..21220ebf 100644
--- a/tests/tools/console.test.ts
+++ b/tests/tools/console.test.ts
@@ -706,13 +706,7 @@ describe('console', () => {
);
const result = await response.handle(context);
- t.assert.snapshot(
- JSON.stringify(
- stabilizeStructuredContent(result.structuredContent),
- null,
- 2,
- ),
- );
+ t.assert.snapshot(stabilizeStructuredContent(result.structuredContent));
await dialog.dismiss();
});
});
diff --git a/tests/utils.ts b/tests/utils.ts
index 9ce2fdc3..2a887bfe 100644
--- a/tests/utils.ts
+++ b/tests/utils.ts
@@ -299,25 +299,29 @@ export function html(
</html>`;
}
-export function stabilizeStructuredContent(content: unknown): unknown {
- if (typeof content === 'string') {
- return stabilizeResponseOutput(content);
- }
- if (Array.isArray(content)) {
- return content.map(item => stabilizeStructuredContent(item));
- }
- if (typeof content === 'object' && content !== null) {
- const result: Record<string, unknown> = {};
- for (const [key, value] of Object.entries(content)) {
- if (key === 'snapshotFilePath' && typeof value === 'string') {
- result[key] = '<file>';
- } else {
- result[key] = stabilizeStructuredContent(value);
+export function stabilizeStructuredContent(content: unknown): string {
+ const stabilize = (c: unknown): unknown => {
+ if (typeof c === 'string') {
+ return stabilizeResponseOutput(c);
+ }
+ if (Array.isArray(c)) {
+ return c.map(item => stabilize(item));
+ }
+ if (typeof c === 'object' && c !== null) {
+ const result: Record<string, unknown> = {};
+ for (const [key, value] of Object.entries(c)) {
+ if (key === 'snapshotFilePath' && typeof value === 'string') {
+ result[key] = '<file>';
+ } else {
+ result[key] = stabilize(value);
+ }
}
+ return result;
}
- return result;
- }
- return content;
+ return c;
+ };
+
+ return JSON.stringify(stabilize(content), null, 2);
}
export function stabilizeResponseOutput(text: unknown) {