Skip to content

Commit 75b9d18

Browse files
authored
fix(e2e): Ensure the response event listener is registered before navigation (#989)
1 parent e0e7057 commit 75b9d18

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

packages/tests-e2e/tests/appRouter/middleware.rewrite.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ test("Middleware Rewrite External Image", async ({ page }) => {
3434
});
3535

3636
test("Middleware Rewrite Status Code", async ({ page }) => {
37-
page.on("response", async (response) => {
38-
// Need to set up the event before navigating to the page to avoid missing it
39-
// We need to check the URL here also cause there will be multiple responses (i.e the fonts, css, js, etc)
40-
if (response.url() === "/rewrite-status-code") {
41-
expect(response.status()).toBe(403);
42-
}
37+
// Need to set up the event before navigating to the page to avoid missing it
38+
// We need to check the URL here also cause there will be multiple responses (i.e the fonts, css, js, etc)
39+
const statusPromise = new Promise<number>((resolve) => {
40+
page.on("response", async (response) => {
41+
// `response.url()` will be the full URL including the host, so we need to check the pathname
42+
if (new URL(response.url()).pathname === "/rewrite-status-code") {
43+
resolve(response.status());
44+
}
45+
});
4346
});
47+
4448
await page.goto("/rewrite-status-code");
4549
const el = page.getByText("Rewritten Destination", { exact: true });
4650
await expect(el).toBeVisible();
51+
expect(statusPromise).resolves.toEqual(403);
4752
});

0 commit comments

Comments
 (0)