Skip to content

Commit d759c04

Browse files
committed
refactor: clear issue aggregator on page navigation
1 parent bb4ffab commit d759c04

File tree

2 files changed

+65
-14
lines changed

2 files changed

+65
-14
lines changed

src/PageCollector.ts

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -220,23 +220,37 @@ export class ConsoleCollector extends PageCollector<
220220

221221
async subscribeForIssues(page: Page) {
222222
const seenKeys = new Set<string>();
223-
const mockManager = new FakeIssuesManager();
224-
const aggregator = new IssueAggregator(mockManager);
225-
aggregator.addEventListener(
226-
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
227-
event => {
228-
const withId = event.data as WithSymbolId<AggregatedIssue>;
229-
// Emit aggregated issue only if it's a new one
230-
if (withId[stableIdSymbol]) {
231-
return;
232-
}
233-
page.emit('issue', event.data);
234-
},
235-
);
223+
let mockManager = new FakeIssuesManager();
224+
setupIssueAggregator();
225+
226+
function setupIssueAggregator() {
227+
mockManager = new FakeIssuesManager();
228+
const aggregator = new IssueAggregator(mockManager);
229+
aggregator.addEventListener(
230+
IssueAggregatorEvents.AGGREGATED_ISSUE_UPDATED,
231+
event => {
232+
const withId = event.data as WithSymbolId<AggregatedIssue>;
233+
// Emit aggregated issue only if it's a new one
234+
if (withId[stableIdSymbol]) {
235+
return;
236+
}
237+
page.emit('issue', event.data);
238+
},
239+
);
240+
}
236241

237242
try {
238243
// @ts-expect-error use existing CDP client (internal Puppeteer API).
239244
const session = page._client() as CDPSession;
245+
page.on('framenavigated', (frame: Frame) => {
246+
// Only split the storage on main frame navigation
247+
if (frame !== page.mainFrame()) {
248+
return;
249+
}
250+
seenKeys.clear();
251+
setupIssueAggregator();
252+
});
253+
240254
session.on('Audits.issueAdded', data => {
241255
try {
242256
const inspectorIssue = data.issue;

tests/tools/console.test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('console', () => {
4141
});
4242
});
4343

44-
it('lists issues messages', async () => {
44+
it('lists issues', async () => {
4545
await withBrowser(async (response, context) => {
4646
const page = await context.newPage();
4747
const issuePromise = new Promise<void>(resolve => {
@@ -63,6 +63,43 @@ describe('console', () => {
6363
});
6464
});
6565

66+
it('lists issues after a page reload', async () => {
67+
await withBrowser(async (response, context) => {
68+
const page = await context.newPage();
69+
const issuePromise = new Promise<void>(resolve => {
70+
page.on('issue', () => {
71+
resolve();
72+
});
73+
});
74+
75+
await page.setContent('<input type="text" name="username" />');
76+
await issuePromise;
77+
await listConsoleMessages.handler({params: {}}, response, context);
78+
{
79+
const formattedResponse = await response.handle('test', context);
80+
const textContent = formattedResponse[0] as {text: string};
81+
assert.ok(
82+
textContent.text.includes(
83+
`msgid=1 [issue] An element doesn't have an autocomplete attribute (count: 1)`,
84+
),
85+
);
86+
}
87+
88+
await page.reload();
89+
await page.setContent('<input type="text" name="username" />');
90+
91+
{
92+
const formattedResponse = await response.handle('test', context);
93+
const textContent = formattedResponse[0] as {text: string};
94+
assert.ok(
95+
textContent.text.includes(
96+
`msgid=2 [issue] An element doesn't have an autocomplete attribute (count: 1)`,
97+
),
98+
);
99+
}
100+
});
101+
});
102+
66103
it('work with primitive unhandled errors', async () => {
67104
await withBrowser(async (response, context) => {
68105
const page = await context.newPage();

0 commit comments

Comments
 (0)