Skip to content

Commit

Permalink
Fix flakiness in stream tests
Browse files Browse the repository at this point in the history
These tests were a bit flaky because they were asserting DOM changes
that can happen asynchronously.

Changing the assertions so we wait until the change happens fixes the
flakiness.

Also, introduce `waitUntilText` to wait until some text appears in the
page.
  • Loading branch information
afcapel committed Jan 31, 2023
1 parent 360f474 commit 77bd68c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/tests/functional/stream_tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from "@playwright/test"
import { assert } from "chai"
import { nextBeat, nextEventNamed, readEventLogs } from "../helpers/page"
import { nextBeat, nextEventNamed, readEventLogs, waitUntilNoSelector, waitUntilText } from "../helpers/page"

test.beforeEach(async ({ page }) => {
await page.goto("/src/tests/fixtures/stream.html")
Expand Down Expand Up @@ -50,12 +50,10 @@ test("test receiving a message without a template", async ({ page }) => {
`)
)

assert.equal(await page.locator("#messages").count(), 0, "removes target element")
assert.notOk(await waitUntilNoSelector(page, "#messages"), "removes target element")
})

test("test receiving a message with a <script> element", async ({ page }) => {
const messages = await page.locator("#messages .message")

await page.evaluate(() =>
window.Turbo.renderStreamMessage(`
<turbo-stream action="append" target="messages">
Expand All @@ -69,7 +67,7 @@ test("test receiving a message with a <script> element", async ({ page }) => {
`)
)

assert.deepEqual(await messages.allTextContents(), ["Hello from script"])
assert.ok(await waitUntilText(page, "Hello from script"))
})

test("test overriding with custom StreamActions", async ({ page }) => {
Expand All @@ -96,10 +94,10 @@ test("test overriding with custom StreamActions", async ({ page }) => {
`)
}, html)

assert.equal(await page.textContent("#messages"), html, "evaluates custom StreamAction")
assert.ok(await waitUntilText(page, "Rendered with Custom Action"), "evaluates custom StreamAction")
})

test("test receiving a stream message asynchronously", async ({ page }) => {
test("test receiving a stream message over SSE", async ({ page }) => {
await page.evaluate(() => {
document.body.insertAdjacentHTML(
"afterbegin",
Expand All @@ -111,8 +109,8 @@ test("test receiving a stream message asynchronously", async ({ page }) => {
assert.deepEqual(await messages.allTextContents(), ["First"])

await page.click("#async button")
await nextBeat()

await waitUntilText(page, "Hello world!")
assert.deepEqual(await messages.allTextContents(), ["First", "Hello world!"])

await page.evaluate(() => document.getElementById("stream-source")?.remove())
Expand Down
4 changes: 4 additions & 0 deletions src/tests/helpers/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ export function waitForPathname(page: Page, pathname: string): Promise<void> {
return page.waitForURL((url) => url.pathname == pathname)
}

export function waitUntilText(page: Page, text: string, state: "visible" | "attached" = "visible") {
return page.waitForSelector(`text='${text}'`, { state })
}

export function waitUntilSelector(page: Page, selector: string, state: "visible" | "attached" = "visible") {
return page.waitForSelector(selector, { state })
}
Expand Down

0 comments on commit 77bd68c

Please sign in to comment.