Skip to content

Commit 34b8cd5

Browse files
Use context.route() for API mocking in extension tests
API requests from the popup are made by the background service worker, not the popup page itself. page.route() only intercepts requests from that page's context, so service worker requests bypass it entirely. context.route() intercepts at the browser level, catching all requests including those from service workers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4490ab9 commit 34b8cd5

2 files changed

Lines changed: 12 additions & 12 deletions

File tree

tests/extension.spec.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,8 @@ test.describe('', () => {
262262
const issue = TEST_ISSUES[0];
263263
await addBookmarks(context, makeBookmark(issue));
264264

265-
const popupPage = await context.newPage();
266-
267-
// Mock API to avoid rate limits and ensure test stability
268-
await popupPage.route('**/api.github.com/repos/**', async (route) => {
265+
// Mock API at context level - requests come from service worker, not page
266+
await context.route('**/api.github.com/repos/**', async (route) => {
269267
await route.fulfill({
270268
status: 200,
271269
contentType: 'application/json',
@@ -280,11 +278,13 @@ test.describe('', () => {
280278
});
281279
});
282280

281+
const popupPage = await context.newPage();
283282
await popupPage.goto(`chrome-extension://${extensionId}/assets/popup.html`);
284283

285284
const issueItem = popupPage.locator('.issue-item');
286285
await expect(issueItem).toBeVisible({ timeout: 10000 });
287286

287+
await context.unroute('**/api.github.com/repos/**');
288288
await popupPage.close();
289289
});
290290

@@ -293,10 +293,8 @@ test.describe('', () => {
293293
const bookmarkKey = `${issue.owner}/${issue.repo}/issues/${issue.number}`;
294294
await addBookmarks(context, makeBookmark(issue));
295295

296-
const popupPage = await context.newPage();
297-
298-
// Mock API to avoid rate limits and ensure test stability
299-
await popupPage.route('**/api.github.com/repos/**', async (route) => {
296+
// Mock API at context level - requests come from service worker, not page
297+
await context.route('**/api.github.com/repos/**', async (route) => {
300298
await route.fulfill({
301299
status: 200,
302300
contentType: 'application/json',
@@ -311,6 +309,7 @@ test.describe('', () => {
311309
});
312310
});
313311

312+
const popupPage = await context.newPage();
314313
await popupPage.goto(`chrome-extension://${extensionId}/assets/popup.html`);
315314

316315
const issueItem = popupPage.locator('.issue-item');
@@ -325,6 +324,7 @@ test.describe('', () => {
325324
const bookmarks = await getBookmarks(context);
326325
expect(Object.keys(bookmarks)).not.toContain(bookmarkKey);
327326

327+
await context.unroute('**/api.github.com/repos/**');
328328
await popupPage.close();
329329
});
330330

@@ -778,10 +778,8 @@ test.describe('', () => {
778778
...makeBookmark(issue2, Date.now() - 86400000)
779779
});
780780

781-
const popupPage = await context.newPage();
782-
783-
// Mock API for visual stability - consistent titles/states for screenshot comparison
784-
await popupPage.route('**/api.github.com/repos/**', async (route) => {
781+
// Mock API at context level - requests come from service worker, not page
782+
await context.route('**/api.github.com/repos/**', async (route) => {
785783
const url = route.request().url();
786784
let data = {
787785
title: 'Sample Issue',
@@ -809,11 +807,13 @@ test.describe('', () => {
809807
});
810808
});
811809

810+
const popupPage = await context.newPage();
812811
await popupPage.goto(`chrome-extension://${extensionId}/assets/popup.html`);
813812
await popupPage.waitForSelector('.issue-item');
814813
await popupPage.waitForTimeout(500);
815814

816815
await expect(popupPage).toHaveScreenshot('popup-with-issues.png');
816+
await context.unroute('**/api.github.com/repos/**');
817817
await popupPage.close();
818818
});
819819

-7.87 KB
Loading

0 commit comments

Comments
 (0)