Skip to content

Commit b67039e

Browse files
authored
Reduce flakiness in several tests (#1487)
"does not fire turbo:load twice after following a redirect" src/tests/functional/navigation_tests.js:451 The event can sometimes arrive slightly before the check. Count events over the whole scenario rather than checking at a specific moment, which is more robust and more closely matches the purpose of the test. "link method form submission targeting frame submits a single request" src/tests/functional/form_submission_tests.js:1019 Redirect accounting differs between browsers—Firefox sometimes won't count the redirect as a request, returning just 1. Stop counting redirect requests. Instead, assert the redirect happened by checking that the page contains content from the redirect target, then verify a single request was made by counting only the non-redirect requests. "successfully following a link to a page without a matching frame shows an error and throws an exception" src/tests/functional/frame_tests.js:152 "failing to follow a link to a page without a matching frame shows an error and throws an exception" src/tests/functional/frame_tests.js:181 The assertion on the error can run before the error-setting callback runs. Wait for the error to be set and the page click to finish before continuing with assertions. "reloads when tracked elements change" src/tests/functional/rendering_tests.js:48 Since we're triggering a full page reload, it's possible to try to evaluate on the page at the moment of navigation. This leads to a race condition where the check is triggered as the page is being navigated away and the evaluation happens after its context was already destroyed, throwing "Execution context was destroyed, most likely because of a navigation". Use Playwright's native page.waitForURL instead of waiting for the turbo:load event, since it doesn't rely on page evaluation and is resistant to navigation issues.
1 parent 26c5704 commit b67039e

3 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/tests/functional/form_submission_tests.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,15 +1018,18 @@ test("link method form submission inside frame submits a single request", async
10181018

10191019
test("link method form submission targeting frame submits a single request", async ({ page }) => {
10201020
let requestCounter = 0
1021-
page.on("request", () => requestCounter++)
1021+
page.on("request", (request) => {
1022+
if (request.url().includes("/__turbo/redirect")) requestCounter++
1023+
})
10221024

10231025
await page.click("#turbo-method-post-to-targeted-frame")
10241026

10251027
const { fetchOptions } = await nextEventNamed(page, "turbo:before-fetch-request")
10261028

10271029
await noNextEventNamed(page, "turbo:before-fetch-request")
10281030
expect(fetchOptions.method, "[data-turbo-method] overrides the GET method").toEqual("POST")
1029-
expect(requestCounter, "submits a single HTTP request then follows a redirect").toEqual(2)
1031+
await expect(page.locator("#hello h2")).toHaveText("Hello from a frame")
1032+
expect(requestCounter, "submits a single HTTP request").toEqual(1)
10301033
})
10311034

10321035
test("link method form submission inside frame", async ({ page }) => {

src/tests/functional/frame_tests.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ test("successfully following a link to a page without a matching frame dispatche
152152
test("successfully following a link to a page without a matching frame shows an error and throws an exception", async ({
153153
page
154154
}) => {
155-
let error = undefined
156-
page.once("pageerror", (e) => (error = e))
157-
158-
await page.click("#missing-frame-link")
155+
const [error] = await Promise.all([
156+
page.waitForEvent("pageerror"),
157+
page.click("#missing-frame-link")
158+
])
159159

160160
await expect(page.locator("#missing")).toHaveText("Content missing")
161161

@@ -184,10 +184,10 @@ test("failing to follow a link to a page without a matching frame dispatches a t
184184
test("failing to follow a link to a page without a matching frame shows an error and throws an exception", async ({
185185
page
186186
}) => {
187-
let error = undefined
188-
page.once("pageerror", (e) => (error = e))
189-
190-
await page.click("#missing-page-link")
187+
const [error] = await Promise.all([
188+
page.waitForEvent("pageerror"),
189+
page.click("#missing-page-link")
190+
])
191191

192192
await expect(page.locator("#missing")).toHaveText("Content missing")
193193

src/tests/functional/navigation_tests.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,21 @@ test("double-clicking on a link", async ({ page }) => {
449449
})
450450

451451
test("does not fire turbo:load twice after following a redirect", async ({ page }) => {
452+
await page.evaluate(() => {
453+
window.turboLoadCount = 0
454+
addEventListener("turbo:load", () => window.turboLoadCount++)
455+
})
456+
452457
page.click("#redirection-link")
453458

454459
await nextBeat() // 301 redirect response
455460

456-
expect(await noNextEventNamed(page, "turbo:load")).toEqual(true)
457-
458461
await nextBeat() // 200 response
459462
await nextBody(page)
460463
await nextEventNamed(page, "turbo:load")
464+
await nextBeat()
465+
466+
expect(await page.evaluate(() => window.turboLoadCount)).toEqual(1)
461467
})
462468

463469
test("navigating back whilst a visit is in-flight", async ({ page }) => {

0 commit comments

Comments
 (0)