Skip to content

Commit 18fe891

Browse files
authored
Playwright: replace assert with expect (#1465)
Replace calls to `assert`-prefixed assertions with Playwright's `expect`-based assertions in the following files: * `src/tests/functional/async_script_tests.js` * `src/tests/functional/cache_observer_tests.js` * `src/tests/functional/drive_disabled_tests.js` * `src/tests/functional/drive_tests.js` * `src/tests/functional/drive_view_transition_legacy_tests.js` * `src/tests/functional/import_tests.js` * `src/tests/functional/link_prefetch_observer_tests.js` * `src/tests/functional/pausable_rendering_tests.js` * `src/tests/functional/preloader_tests.js` Some `nextBody` calls are made unnecessary by invoking Playwright's wait-and-retry expectations, so this commit removes them when possible.
1 parent b65bf46 commit 18fe891

9 files changed

+64
-87
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
1+
import { expect, test } from "@playwright/test"
32
import { readEventLogs, visitAction } from "../helpers/page"
43

54
test.beforeEach(async ({ page }) => {
@@ -10,11 +9,11 @@ test.beforeEach(async ({ page }) => {
109
test("does not emit turbo:load when loaded asynchronously after DOMContentLoaded", async ({ page }) => {
1110
const events = await readEventLogs(page)
1211

13-
assert.deepEqual(events, [])
12+
expect(events).toEqual([])
1413
})
1514

1615
test("following a link when loaded asynchronously after DOMContentLoaded", async ({ page }) => {
1716
await page.click("#async-link")
1817

19-
assert.equal(await visitAction(page), "advance")
18+
expect(await visitAction(page)).toEqual("advance")
2019
})
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
3-
import { hasSelector, nextBody } from "../helpers/page"
1+
import { expect, test } from "@playwright/test"
2+
import { nextEventNamed } from "../helpers/page"
43

54
test("removes temporary elements", async ({ page }) => {
65
await page.goto("/src/tests/fixtures/cache_observer.html")
76

8-
assert.equal(await page.textContent("#temporary"), "data-turbo-temporary")
7+
await expect(page.locator("#temporary")).toHaveText("data-turbo-temporary")
98

109
await page.click("#link")
11-
await nextBody(page)
10+
await nextEventNamed(page, "turbo:load")
1211
await page.goBack()
13-
await nextBody(page)
1412

15-
assert.notOk(await hasSelector(page, "#temporary"))
13+
await expect(page.locator("#temporary")).not.toBeAttached()
1614
})
1715

1816
test("following a redirect renders [data-turbo-temporary] elements before the cache removes", async ({ page }) => {
1917
await page.goto("/src/tests/fixtures/navigation.html")
2018
await page.click("#redirect-to-cache-observer")
21-
await nextBody(page)
2219

23-
assert.equal(await page.textContent("#temporary"), "data-turbo-temporary")
20+
await expect(page.locator("#temporary")).toHaveText("data-turbo-temporary")
2421
})

src/tests/functional/drive_disabled_tests.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
1+
import { expect, test } from "@playwright/test"
32
import {
43
getFromLocalStorage,
5-
nextBody,
64
nextEventOnTarget,
7-
pathname,
8-
searchParams,
95
setLocalStorageFromEvent,
10-
visitAction
6+
visitAction,
7+
withPathname,
8+
withSearchParam
119
} from "../helpers/page"
1210

1311
const path = "/src/tests/fixtures/drive_disabled.html"
@@ -18,30 +16,27 @@ test.beforeEach(async ({ page }) => {
1816

1917
test("drive disabled by default; click normal link", async ({ page }) => {
2018
await page.click("#drive_disabled")
21-
await nextBody(page)
2219

23-
assert.equal(pathname(page.url()), path)
24-
assert.equal(await visitAction(page), "load")
20+
await expect(page).toHaveURL(withPathname(path))
21+
expect(await visitAction(page)).toEqual("load")
2522
})
2623

2724
test("drive disabled by default; click link inside data-turbo='true'", async ({ page }) => {
2825
await page.click("#drive_enabled")
29-
await nextBody(page)
3026

31-
assert.equal(pathname(page.url()), path)
32-
assert.equal(await visitAction(page), "advance")
27+
await expect(page).toHaveURL(withPathname(path))
28+
expect(await visitAction(page)).toEqual("advance")
3329
})
3430

3531
test("drive disabled by default; submit form inside data-turbo='true'", async ({ page }) => {
3632
await setLocalStorageFromEvent(page, "turbo:submit-start", "formSubmitted", "true")
3733

3834
await page.click("#no_submitter_drive_enabled a#requestSubmit")
39-
await nextBody(page)
4035

41-
assert.ok(await getFromLocalStorage(page, "formSubmitted"))
42-
assert.equal(pathname(page.url()), "/src/tests/fixtures/form.html")
43-
assert.equal(await visitAction(page), "advance")
44-
assert.equal(await searchParams(page.url()).get("greeting"), "Hello from a redirect")
36+
await expect(page).toHaveURL(withPathname("/src/tests/fixtures/form.html"))
37+
await expect(page).toHaveURL(withSearchParam("greeting", "Hello from a redirect"))
38+
expect(await getFromLocalStorage(page, "formSubmitted")).toBeTruthy()
39+
expect(await visitAction(page)).toEqual("advance")
4540
})
4641

4742
test("drive disabled by default; links within <turbo-frame> navigate with Turbo", async ({ page }) => {
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
3-
import { nextBody, pathname, visitAction } from "../helpers/page"
1+
import { expect, test } from "@playwright/test"
2+
import { visitAction, withPathname } from "../helpers/page"
43

54
const path = "/src/tests/fixtures/drive.html"
65

@@ -10,8 +9,8 @@ test.beforeEach(async ({ page }) => {
109

1110
test("drive enabled by default; click normal link", async ({ page }) => {
1211
await page.click("#drive_enabled")
13-
await nextBody(page)
14-
assert.equal(pathname(page.url()), path)
12+
13+
await expect(page).toHaveURL(withPathname(path))
1514
})
1615

1716
test("drive to external link", async ({ page }) => {
@@ -20,16 +19,14 @@ test("drive to external link", async ({ page }) => {
2019
})
2120

2221
await page.click("#drive_enabled_external")
23-
await nextBody(page)
2422

25-
assert.equal(await page.evaluate(() => window.location.href), "https://example.com/")
26-
assert.equal(await page.textContent("body"), "Hello from the outside world")
23+
await expect(page).toHaveURL("https://example.com/")
24+
await expect(page.locator("body")).toHaveText("Hello from the outside world")
2725
})
2826

2927
test("drive enabled by default; click link inside data-turbo='false'", async ({ page }) => {
3028
await page.click("#drive_disabled")
31-
await nextBody(page)
3229

33-
assert.equal(pathname(page.url()), path)
34-
assert.equal(await visitAction(page), "load")
30+
await expect(page).toHaveURL(withPathname(path))
31+
expect(await visitAction(page)).toEqual("load")
3532
})

src/tests/functional/drive_view_transition_legacy_test.js renamed to src/tests/functional/drive_view_transition_legacy_tests.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
1+
import { expect, test } from "@playwright/test"
32
import { nextBody } from "../helpers/page"
43

54
test.beforeEach(async ({ page }) => {
@@ -17,14 +16,12 @@ test("navigating triggers the view transition", async ({ page }) => {
1716
await page.locator("#go-right").click()
1817
await nextBody(page)
1918

20-
const called = await page.evaluate(`window.startViewTransitionCalled`)
21-
assert.isTrue(called)
19+
expect(await page.evaluate(`window.startViewTransitionCalled`)).toEqual(true)
2220
})
2321

2422
test("navigating does not trigger a view transition when meta tag not present", async ({ page }) => {
2523
await page.locator("#go-other").click()
2624
await nextBody(page)
2725

28-
const called = await page.evaluate(`window.startViewTransitionCalled`)
29-
assert.isUndefined(called)
26+
expect(await page.evaluate(`window.startViewTransitionCalled`)).toEqual(undefined)
3027
})

src/tests/functional/import_tests.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
1+
import { expect, test } from "@playwright/test"
32

43
test("window variable with ESM", async ({ page }) => {
54
await page.goto("/src/tests/fixtures/esm.html")
@@ -39,5 +38,5 @@ async function assertTypeOf(page, propertyName, propertyType) {
3938
return typeof object
4039
}, propertyName)
4140

42-
assert.equal(type, propertyType, `Expected ${propertyName} to be ${propertyType}`)
41+
expect(type, `Expected ${propertyName} to be ${propertyType}`).toEqual(propertyType)
4342
}

src/tests/functional/link_prefetch_observer_tests.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { expect, test } from "@playwright/test"
2-
import { assert } from "chai"
32
import { nextBeat, nextEventOnTarget, noNextEventNamed, noNextEventOnTarget, sleep } from "../helpers/page"
43
import fs from "fs"
54
import path from "path"
@@ -24,7 +23,7 @@ test("it prefetches the page", async ({ page }) => {
2423
await nextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-prefetch")
2524
const { url, fetchOptions } = await nextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-fetch-request")
2625

27-
expect(url).toEqual(await link.evaluate(a => a.href))
26+
await expect(link).toHaveJSProperty("href", url)
2827
expect(fetchOptions.headers["X-Sec-Purpose"]).toEqual("prefetch")
2928
expect(fetchOptions.priority).toEqual("low")
3029

@@ -40,7 +39,7 @@ test("it doesn't follow the link", async ({ page }) => {
4039
await goTo({ page, path: "/hover_to_prefetch.html" })
4140
await hoverSelector({ page, selector: "#anchor_for_prefetch" })
4241

43-
assert.equal(await page.title(), "Hover to Prefetch")
42+
await expect(page).toHaveTitle("Hover to Prefetch")
4443
})
4544

4645
test("prefetches the page when link has a whole valid url as a href", async ({ page }) => {
@@ -84,7 +83,6 @@ test("allows to cancel prefetch requests with custom logic", async ({ page }) =>
8483
const link = page.locator("#anchor_for_prefetch")
8584
await link.evaluate(a => a.addEventListener("turbo:before-prefetch", event => event.preventDefault()))
8685

87-
await page.pause()
8886
await link.hover()
8987
await nextEventOnTarget(page, "anchor_for_prefetch", "turbo:before-prefetch")
9088
await noNextEventNamed(page, "turbo:before-fetch-request")
@@ -193,7 +191,7 @@ test("it prefetches links inside a turbo frame", async ({ page }) => {
193191

194192
await assertPrefetchedOnHover({ page, selector: "#anchor_for_prefetch_in_frame", callback: (request) => {
195193
const turboFrameHeader = request.headers()["turbo-frame"]
196-
assert.equal(turboFrameHeader, "frame_for_prefetch")
194+
expect(turboFrameHeader).toEqual("frame_for_prefetch")
197195
}})
198196
})
199197

@@ -203,7 +201,7 @@ test("doesn't include a turbo-frame header when the link is inside a turbo frame
203201

204202
await assertPrefetchedOnHover({ page, selector: "#anchor_for_prefetch_in_frame_target_top", callback: (request) => {
205203
const turboFrameHeader = request.headers()["turbo-frame"]
206-
assert.equal(undefined, turboFrameHeader)
204+
expect(turboFrameHeader).toEqual(undefined)
207205
}})
208206
})
209207

@@ -243,13 +241,13 @@ test("it resets the cache when a link is hovered", async ({ page }) => {
243241
await page.hover("#anchor_for_prefetch")
244242
await sleep(200)
245243

246-
assert.equal(requestCount, 1)
244+
expect(requestCount).toEqual(1)
247245
await page.mouse.move(0, 0)
248246

249247
await page.hover("#anchor_for_prefetch")
250248
await sleep(200)
251249

252-
assert.equal(requestCount, 2)
250+
expect(requestCount).toEqual(2)
253251
})
254252

255253
test("it does not make a network request when clicking on a link that has been prefetched", async ({ page }) => {
@@ -265,7 +263,7 @@ test("it follows the link using the cached response when clicking on a link that
265263
await hoverSelector({ page, selector: "#anchor_for_prefetch" })
266264

267265
await clickSelector({ page, selector: "#anchor_for_prefetch" })
268-
assert.equal(await page.title(), "Prefetched Page")
266+
await expect(page).toHaveTitle("Prefetched Page")
269267
})
270268

271269
const assertPrefetchedOnHover = async ({ page, selector, callback }) => {
@@ -296,7 +294,7 @@ const assertRequestMade = async (page, action, callback) => {
296294

297295
await action()
298296

299-
assert.equal(!!requestMade, true, "Network request wasn't made when it should have been.")
297+
expect(!!requestMade, "Network request wasn't made when it should have been.").toEqual(true)
300298

301299
if (callback) {
302300
await callback(requestMade)
@@ -309,7 +307,7 @@ const assertRequestNotMade = async (page, action, callback) => {
309307

310308
await action()
311309

312-
assert.equal(!!requestMade, false, "Network request was made when it should not have been.")
310+
expect(!!requestMade, "Network request was made when it should not have been.").toEqual(false)
313311

314312
if (callback) {
315313
await callback(requestMade)
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,46 @@
1-
import { test } from "@playwright/test"
2-
import { assert } from "chai"
3-
import { nextBeat } from "../helpers/page"
1+
import { expect, test } from "@playwright/test"
42

53
test.beforeEach(async ({ page }) => {
64
await page.goto("/src/tests/fixtures/pausable_rendering.html")
75
})
86

97
test("pauses and resumes rendering", async ({ page }) => {
108
page.on("dialog", (dialog) => {
11-
assert.strictEqual(dialog.message(), "Continue rendering?")
9+
expect(dialog.message()).toEqual("Continue rendering?")
1210
dialog.accept()
1311
})
1412

1513
await page.click("#link")
16-
await nextBeat()
1714

18-
assert.equal(await page.textContent("h1"), "One")
15+
await expect(page.locator("h1")).toHaveText("One")
1916
})
2017

2118
test("aborts rendering", async ({ page }) => {
2219
const [firstDialog] = await Promise.all([page.waitForEvent("dialog"), page.click("#link")])
2320

24-
assert.strictEqual(firstDialog.message(), "Continue rendering?")
21+
expect(firstDialog.message()).toEqual("Continue rendering?")
2522

2623
firstDialog.dismiss()
2724

28-
assert.equal(await page.textContent("h1"), "Pausable Rendering")
25+
await expect(page.locator("h1")).toHaveText("Pausable Rendering")
2926
})
3027

3128
test("pauses and resumes rendering a Frame", async ({ page }) => {
3229
page.on("dialog", (dialog) => {
33-
assert.strictEqual(dialog.message(), "Continue rendering?")
30+
expect(dialog.message()).toEqual("Continue rendering?")
3431
dialog.accept()
3532
})
3633

3734
await page.click("#frame-link")
38-
await nextBeat()
3935

40-
assert.equal(await page.textContent("#hello h2"), "Hello from a frame")
36+
await expect(page.locator("#hello h2")).toHaveText("Hello from a frame")
4137
})
4238

4339
test("aborts rendering a Frame", async ({ page }) => {
4440
page.on("dialog", (dialog) => {
45-
assert.strictEqual(dialog.message(), "Continue rendering?")
41+
expect(dialog.message()).toEqual("Continue rendering?")
4642
dialog.dismiss()
4743
})
4844

49-
assert.equal(await page.textContent("#hello h2"), "Pausable Frame Rendering")
45+
await expect(page.locator("#hello h2")).toHaveText("Pausable Frame Rendering")
5046
})

0 commit comments

Comments
 (0)