Skip to content

Chrome DevTools for coding agents

test: migrate performance_start_trace handler tests to mocks (#3209)

Twotdf14fcc1c79f
diff --git a/tests/tools/performance.test.ts b/tests/tools/performance.test.ts
index b1bb2be2..665c6c11 100644
--- a/tests/tools/performance.test.ts
+++ b/tests/tools/performance.test.ts
@@ -41,72 +41,70 @@ describe('performance', () => {
});
describe('performance_start_trace', () => {
- it('starts a trace recording', async () => {
- await withMcpContext(async (response, context, args) => {
- context.getSelectedMcpPage().setIsRunningPerformanceTrace(false);
- const selectedPage = context.getSelectedMcpPage().pptrPage;
- sinon
- .stub(selectedPage, 'url')
- .callsFake(() => 'https://trace.example/');
- sinon.stub(selectedPage, 'goto').resolves(null);
- const startTracingStub = sinon.stub(selectedPage.tracing, 'start');
- await startTrace(args).handler(
- {
- params: {reload: true, autoStop: false},
- page: context.getSelectedMcpPage(),
- },
- response,
- context,
- );
- sinon.assert.calledOnce(startTracingStub);
- sinon.assert.calledWithExactly(startTracingStub, {
- categories: [
- '-*',
- ...DevTools.TracingDefaultCategories,
- ...DevTools.TracingOptionalCategories.JsSampling,
- ...DevTools.TracingOptionalCategories.Screenshot,
- ],
- bufferSize: 1200 * 1000,
- });
- assert.ok(context.isRunningPerformanceTrace());
- assert.ok(
- response.responseLines
- .join('\n')
- .match(/The performance trace is being recorded/),
- );
+ it('starts a trace recording and reloads the page by default', async () => {
+ const {page, context, response, args} = createHandlerMocks();
+ context.isRunningPerformanceTrace.returns(false);
+ page.pptrPage.url.returns('https://trace.example/');
+
+ await startTrace(args).handler(
+ {
+ params: {reload: true, autoStop: false},
+ page,
+ },
+ response,
+ context,
+ );
+
+ sinon.assert.calledTwice(page.pptrPage.goto);
+ sinon.assert.calledWithExactly(
+ page.pptrPage.goto.firstCall,
+ 'about:blank',
+ {waitUntil: 'load'},
+ );
+ sinon.assert.calledWithExactly(
+ page.pptrPage.goto.secondCall,
+ 'https://trace.example/',
+ {waitUntil: ['load']},
+ );
+ sinon.assert.calledOnceWithExactly(page.pptrPage.tracing.start, {
+ categories: [
+ '-*',
+ ...DevTools.TracingDefaultCategories,
+ ...DevTools.TracingOptionalCategories.JsSampling,
+ ...DevTools.TracingOptionalCategories.Screenshot,
+ ],
+ bufferSize: 1200 * 1000,
});
+ sinon.assert.calledOnceWithExactly(
+ page.setIsRunningPerformanceTrace,
+ true,
+ );
+ sinon.assert.calledWithMatch(
+ response.appendResponseLine,
+ sinon.match(/The performance trace is being recorded/),
+ );
});
- it('can navigate to about:blank and record a page reload', async () => {
- await withMcpContext(async (response, context, args) => {
- const selectedPage = context.getSelectedMcpPage().pptrPage;
- sinon
- .stub(selectedPage, 'url')
- .callsFake(() => 'https://trace.example/');
- const gotoStub = sinon.stub(selectedPage, 'goto');
- const startTracingStub = sinon.stub(selectedPage.tracing, 'start');
- await startTrace(args).handler(
- {
- params: {reload: true, autoStop: false},
- page: context.getSelectedMcpPage(),
- },
- response,
- context,
- );
- sinon.assert.calledOnce(startTracingStub);
- sinon.assert.calledWithExactly(gotoStub, 'about:blank', {
- waitUntil: 'load',
- });
- sinon.assert.calledWithExactly(gotoStub, 'https://trace.example/', {
- waitUntil: ['load'],
- });
- assert.ok(context.isRunningPerformanceTrace());
- assert.ok(
- response.responseLines
- .join('\n')
- .match(/The performance trace is being recorded/),
- );
- });
+ it('does not reload when reload is false', async () => {
+ const {page, context, response, args} = createHandlerMocks();
+ context.isRunningPerformanceTrace.returns(false);
+ page.pptrPage.url.returns('https://trace.example/');
+
+ await startTrace(args).handler(
+ {
+ params: {reload: false, autoStop: false},
+ page,
+ },
+ response,
+ context,
+ );
+
+ sinon.assert.calledOnce(page.pptrPage.tracing.start);
+ sinon.assert.notCalled(page.pptrPage.goto);
+ sinon.assert.calledOnceWithExactly(
+ page.setIsRunningPerformanceTrace,
+ true,
+ );
});
it('disables the cache during the reload when ignoreCache is set', async () => {
@@ -241,46 +239,38 @@ describe('performance', () => {
});
it('resets the running flag if a setup step throws', async () => {
- await withMcpContext(async (response, context, args) => {
- const selectedPage = context.getSelectedMcpPage().pptrPage;
- sinon
- .stub(selectedPage, 'url')
- .callsFake(() => 'https://trace.example/');
- const gotoStub = sinon
- .stub(selectedPage, 'goto')
- .rejects(new Error('Navigation failed'));
- const startTracingStub = sinon.stub(selectedPage.tracing, 'start');
- sinon
- .stub(selectedPage.tracing, 'stop')
- .rejects(new Error('Cannot stop recording: tracing was not started'));
- await assert.rejects(
- startTrace(args).handler(
- {
- params: {reload: true, autoStop: true},
- page: context.getSelectedMcpPage(),
- },
- response,
- context,
- ),
- /Navigation failed/,
- );
- sinon.assert.notCalled(startTracingStub);
- assert.strictEqual(context.isRunningPerformanceTrace(), false);
+ const {page, context, response, args} = createHandlerMocks();
+ context.isRunningPerformanceTrace
+ .onFirstCall()
+ .returns(false)
+ .onSecondCall()
+ .returns(true);
+ page.pptrPage.url.returns('https://trace.example/');
+ page.pptrPage.goto.rejects(new Error('Navigation failed'));
- // A follow-up start_trace must proceed instead of reporting that a
- // trace is already running.
- gotoStub.resolves(null);
- await startTrace(args).handler(
+ await assert.rejects(
+ startTrace(args).handler(
{
- params: {reload: true, autoStop: false},
- page: context.getSelectedMcpPage(),
+ params: {reload: true, autoStop: true},
+ page,
},
response,
context,
- );
- sinon.assert.calledOnce(startTracingStub);
- assert.ok(context.isRunningPerformanceTrace());
- });
+ ),
+ /Navigation failed/,
+ );
+
+ sinon.assert.notCalled(page.pptrPage.tracing.start);
+ sinon.assert.calledOnce(page.pptrPage.tracing.stop);
+ sinon.assert.calledTwice(page.setIsRunningPerformanceTrace);
+ sinon.assert.calledWithExactly(
+ page.setIsRunningPerformanceTrace.firstCall,
+ true,
+ );
+ sinon.assert.calledWithExactly(
+ page.setIsRunningPerformanceTrace.secondCall,
+ false,
+ );
});
it('supports filePath', async () => {