Skip to content

Commit 817975d

Browse files
committed
test: migrate page.textContent to locator.textContent or locator.toHaveText
1 parent 68eed5c commit 817975d

File tree

5 files changed

+47
-47
lines changed

5 files changed

+47
-47
lines changed

tests/e2e/cli-before-regional-blobs-support.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('should serve 404 page when requesting non existing page (no matching route
1717
const headers = response?.headers() || {}
1818
expect(response?.status()).toBe(404)
1919

20-
expect(await page.textContent('h1')).toBe('404')
20+
await expect(page.locator('h1')).toHaveText('404')
2121

2222
// https://github.com/vercel/next.js/pull/69802 made changes to returned cache-control header,
2323
// after that (14.2.10 and canary.147) 404 pages would have `private` directive, before that it

tests/e2e/on-demand-app.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ test.describe('app router on-demand revalidation', () => {
9696
: 's-maxage=31536000, stale-while-revalidate=31536000, durable',
9797
)
9898

99-
const date1 = await page.textContent('[data-testid="date-now"]')
99+
const date1 = await page.locator('[data-testid="date-now"]').textContent()
100100

101-
const h1 = await page.textContent('h1')
101+
const h1 = await page.locator('h1').textContent()
102102
expect(h1).toBe(expectedH1Content)
103103

104104
const response2 = await pollUntilHeadersMatch(new URL(pagePath, serverComponents.url).href, {
@@ -127,7 +127,7 @@ test.describe('app router on-demand revalidation', () => {
127127
)
128128

129129
// the page is cached
130-
const date2 = await page.textContent('[data-testid="date-now"]')
130+
const date2 = await page.locator('[data-testid="date-now"]').textContent()
131131
expect(date2).toBe(date1)
132132

133133
const revalidate = await page.goto(new URL(revalidateApiPath, serverComponents.url).href)
@@ -159,7 +159,7 @@ test.describe('app router on-demand revalidation', () => {
159159
)
160160

161161
// the page has now an updated date
162-
const date3 = await page.textContent('[data-testid="date-now"]')
162+
const date3 = await page.locator('[data-testid="date-now"]').textContent()
163163
expect(date3).not.toBe(date2)
164164

165165
const response4 = await pollUntilHeadersMatch(new URL(pagePath, serverComponents.url).href, {
@@ -188,7 +188,7 @@ test.describe('app router on-demand revalidation', () => {
188188
)
189189

190190
// the page is cached
191-
const date4 = await page.textContent('[data-testid="date-now"]')
191+
const date4 = await page.locator('[data-testid="date-now"]').textContent()
192192
expect(date4).toBe(date3)
193193
})
194194
}

tests/e2e/page-router.test.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
174174
)
175175

176176
if (fallbackWasServed) {
177-
const loading = await page.textContent('[data-testid="loading"]')
177+
const loading = await page.locator('[data-testid="loading"]').textContent()
178178
expect(loading, 'Fallback should be shown').toBe('Loading...')
179179
}
180180

181-
const date1 = await page.textContent('[data-testid="date-now"]')
182-
const h1 = await page.textContent('h1')
181+
const date1 = await page.locator('[data-testid="date-now"]').textContent()
182+
const h1 = await page.locator('h1').textContent()
183183
expect(h1).toBe(expectedH1Content)
184184

185185
// check json route
@@ -238,7 +238,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
238238
)
239239

240240
// the page is cached
241-
const date2 = await page.textContent('[data-testid="date-now"]')
241+
const date2 = await page.locator('[data-testid="date-now"]').textContent()
242242
expect(date2).toBe(date1)
243243

244244
// check json route
@@ -299,7 +299,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
299299
expect(headers3?.['x-nextjs-cache']).toBeUndefined()
300300

301301
// the page has now an updated date
302-
const date3 = await page.textContent('[data-testid="date-now"]')
302+
const date3 = await page.locator('[data-testid="date-now"]').textContent()
303303
expect(date3).not.toBe(date2)
304304

305305
// check json route
@@ -366,7 +366,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
366366
},
367367
)
368368
expect(response1?.status()).toBe(200)
369-
const date1 = (await page.textContent('[data-testid="date-now"]')) ?? ''
369+
const date1 = (await page.locator('[data-testid="date-now"]').textContent()) ?? ''
370370

371371
// ensure response was produced before invocation (served from cache)
372372
expect(date1.localeCompare(beforeFetch)).toBeLessThan(0)
@@ -391,7 +391,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
391391
},
392392
)
393393
expect(response2?.status()).toBe(200)
394-
const date2 = (await page.textContent('[data-testid="date-now"]')) ?? ''
394+
const date2 = (await page.locator('[data-testid="date-now"]').textContent()) ?? ''
395395

396396
// ensure response was produced after initial invocation
397397
expect(beforeFetch.localeCompare(date2)).toBeLessThan(0)
@@ -416,7 +416,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
416416
)
417417

418418
// ensure response was NOT produced before invocation
419-
const date1 = (await page.textContent('[data-testid="date-now"]')) ?? ''
419+
const date1 = (await page.locator('[data-testid="date-now"]').textContent()) ?? ''
420420
expect(date1.localeCompare(beforeFirstFetch)).toBeGreaterThan(0)
421421

422422
// allow page to get stale
@@ -431,7 +431,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
431431
/s-maxage=60, stale-while-revalidate=[0-9]+, durable/,
432432
)
433433

434-
const date2 = (await page.textContent('[data-testid="date-now"]')) ?? ''
434+
const date2 = (await page.locator('[data-testid="date-now"]').textContent()) ?? ''
435435
expect(date2).toBe(date1)
436436

437437
// wait a bit to ensure background work has a chance to finish
@@ -450,7 +450,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
450450
/s-maxage=60, stale-while-revalidate=[0-9]+, durable/,
451451
)
452452

453-
const date3 = (await page.textContent('[data-testid="date-now"]')) ?? ''
453+
const date3 = (await page.locator('[data-testid="date-now"]').textContent()) ?? ''
454454
expect(date3.localeCompare(date2)).toBeGreaterThan(0)
455455
})
456456

@@ -469,7 +469,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
469469
const headers = response?.headers() || {}
470470
expect(response?.status()).toBe(404)
471471

472-
expect(await page.textContent('p')).toBe('Custom 404 page')
472+
await expect(page.locator('p')).toHaveText('Custom 404 page')
473473

474474
// https://github.com/vercel/next.js/pull/69802 made changes to returned cache-control header,
475475
// after that (14.2.10 and canary.147) 404 pages would have `private` directive, before that
@@ -493,7 +493,7 @@ test.describe('Simple Page Router (no basePath, no i18n)', () => {
493493
const headers = response?.headers() || {}
494494
expect(response?.status()).toBe(404)
495495

496-
expect(await page.textContent('p')).toBe('Custom 404 page')
496+
await expect(page.locator('p')).toHaveText('Custom 404 page')
497497

498498
expect(headers['debug-netlify-cdn-cache-control']).toBe(
499499
nextVersionSatisfies('>=15.0.0-canary.187')
@@ -748,12 +748,12 @@ test.describe('Page Router with basePath and i18n', () => {
748748
)
749749

750750
if (fallbackWasServedImplicitLocale) {
751-
const loading = await page.textContent('[data-testid="loading"]')
751+
const loading = await page.locator('[data-testid="loading"]').textContent()
752752
expect(loading, 'Fallback should be shown').toBe('Loading...')
753753
}
754754

755-
const date1ImplicitLocale = await page.textContent('[data-testid="date-now"]')
756-
const h1ImplicitLocale = await page.textContent('h1')
755+
const date1ImplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
756+
const h1ImplicitLocale = await page.locator('h1').textContent()
757757
expect(h1ImplicitLocale).toBe(expectedH1Content)
758758

759759
const response1ExplicitLocale = await pollUntilHeadersMatch(
@@ -790,12 +790,12 @@ test.describe('Page Router with basePath and i18n', () => {
790790
)
791791

792792
if (fallbackWasServedExplicitLocale) {
793-
const loading = await page.textContent('[data-testid="loading"]')
793+
const loading = await page.locator('[data-testid="loading"]').textContent()
794794
expect(loading, 'Fallback should be shown').toBe('Loading...')
795795
}
796796

797-
const date1ExplicitLocale = await page.textContent('[data-testid="date-now"]')
798-
const h1ExplicitLocale = await page.textContent('h1')
797+
const date1ExplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
798+
const h1ExplicitLocale = await page.locator('h1').textContent()
799799
expect(h1ExplicitLocale).toBe(expectedH1Content)
800800

801801
// implicit and explicit locale paths should be the same (same cached response)
@@ -861,7 +861,7 @@ test.describe('Page Router with basePath and i18n', () => {
861861
)
862862

863863
// the page is cached
864-
const date2ImplicitLocale = await page.textContent('[data-testid="date-now"]')
864+
const date2ImplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
865865
expect(date2ImplicitLocale).toBe(date1ImplicitLocale)
866866

867867
const response2ExplicitLocale = await pollUntilHeadersMatch(
@@ -893,7 +893,7 @@ test.describe('Page Router with basePath and i18n', () => {
893893
)
894894

895895
// the page is cached
896-
const date2ExplicitLocale = await page.textContent('[data-testid="date-now"]')
896+
const date2ExplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
897897
expect(date2ExplicitLocale).toBe(date1ExplicitLocale)
898898

899899
// check json route
@@ -961,7 +961,7 @@ test.describe('Page Router with basePath and i18n', () => {
961961
expect(headers3ImplicitLocale?.['x-nextjs-cache']).toBeUndefined()
962962

963963
// the page has now an updated date
964-
const date3ImplicitLocale = await page.textContent('[data-testid="date-now"]')
964+
const date3ImplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
965965
expect(date3ImplicitLocale).not.toBe(date2ImplicitLocale)
966966

967967
const response3ExplicitLocale = await pollUntilHeadersMatch(
@@ -984,7 +984,7 @@ test.describe('Page Router with basePath and i18n', () => {
984984
expect(headers3ExplicitLocale?.['x-nextjs-cache']).toBeUndefined()
985985

986986
// the page has now an updated date
987-
const date3ExplicitLocale = await page.textContent('[data-testid="date-now"]')
987+
const date3ExplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
988988
expect(date3ExplicitLocale).not.toBe(date2ExplicitLocale)
989989

990990
// implicit and explicit locale paths should be the same (same cached response)
@@ -1057,7 +1057,7 @@ test.describe('Page Router with basePath and i18n', () => {
10571057
expect(headers4ImplicitLocale?.['x-nextjs-cache']).toBeUndefined()
10581058

10591059
// the page has now an updated date
1060-
const date4ImplicitLocale = await page.textContent('[data-testid="date-now"]')
1060+
const date4ImplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
10611061
expect(date4ImplicitLocale).not.toBe(date3ImplicitLocale)
10621062

10631063
const response4ExplicitLocale = await pollUntilHeadersMatch(
@@ -1080,7 +1080,7 @@ test.describe('Page Router with basePath and i18n', () => {
10801080
expect(headers4ExplicitLocale?.['x-nextjs-cache']).toBeUndefined()
10811081

10821082
// the page has now an updated date
1083-
const date4ExplicitLocale = await page.textContent('[data-testid="date-now"]')
1083+
const date4ExplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
10841084
expect(date4ExplicitLocale).not.toBe(date3ExplicitLocale)
10851085

10861086
// implicit and explicit locale paths should be the same (same cached response)
@@ -1173,12 +1173,12 @@ test.describe('Page Router with basePath and i18n', () => {
11731173
)
11741174

11751175
if (fallbackWasServed) {
1176-
const loading = await page.textContent('[data-testid="loading"]')
1176+
const loading = await page.locator('[data-testid="loading"]').textContent()
11771177
expect(loading, 'Fallback should be shown').toBe('Loading...')
11781178
}
11791179

1180-
const date1 = await page.textContent('[data-testid="date-now"]')
1181-
const h1 = await page.textContent('h1')
1180+
const date1 = await page.locator('[data-testid="date-now"]').textContent()
1181+
const h1 = await page.locator('h1').textContent()
11821182
expect(h1).toBe(expectedH1Content)
11831183

11841184
// check json route
@@ -1241,7 +1241,7 @@ test.describe('Page Router with basePath and i18n', () => {
12411241
)
12421242

12431243
// the page is cached
1244-
const date2 = await page.textContent('[data-testid="date-now"]')
1244+
const date2 = await page.locator('[data-testid="date-now"]').textContent()
12451245
expect(date2).toBe(date1)
12461246

12471247
// check json route
@@ -1309,7 +1309,7 @@ test.describe('Page Router with basePath and i18n', () => {
13091309
expect(headers3?.['x-nextjs-cache']).toBeUndefined()
13101310

13111311
// the page has now an updated date
1312-
const date3 = await page.textContent('[data-testid="date-now"]')
1312+
const date3 = await page.locator('[data-testid="date-now"]').textContent()
13131313
expect(date3).not.toBe(date2)
13141314

13151315
// check json route
@@ -1360,7 +1360,7 @@ test.describe('Page Router with basePath and i18n', () => {
13601360
const headers = response?.headers() || {}
13611361
expect(response?.status()).toBe(404)
13621362

1363-
expect(await page.textContent('p')).toBe('Custom 404 page for locale: en')
1363+
await expect(page.locator('p')).toHaveText('Custom 404 page for locale: en')
13641364

13651365
expect(headers['debug-netlify-cdn-cache-control']).toMatch(
13661366
/no-cache, no-store, max-age=0, must-revalidate, durable/m,
@@ -1378,7 +1378,7 @@ test.describe('Page Router with basePath and i18n', () => {
13781378
const headers = response?.headers() || {}
13791379
expect(response?.status()).toBe(404)
13801380

1381-
expect(await page.textContent('p')).toBe('Custom 404 page for locale: en')
1381+
await expect(page.locator('p')).toHaveText('Custom 404 page for locale: en')
13821382

13831383
// Prior to v14.2.4 notFound pages are not cacheable
13841384
// https://github.com/vercel/next.js/pull/66674

tests/e2e/simple-app.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ test('requesting a non existing page route that needs to be fetched from the blo
227227
const headers = response?.headers() || {}
228228
expect(response?.status()).toBe(404)
229229

230-
expect(await page.textContent('h1')).toBe('404 Not Found')
230+
await expect(page.locator('h1')).toHaveText('404 Not Found')
231231

232232
// https://github.com/vercel/next.js/pull/66674 made changes to returned cache-control header,
233233
// before that 404 page would have `private` directive, after that (14.2.4 and canary.24) it
@@ -254,7 +254,7 @@ test('requesting a non existing page route that needs to be fetched from the blo
254254
const headers = response?.headers() || {}
255255
expect(response?.status()).toBe(404)
256256

257-
expect(await page.textContent('h1')).toBe('404 Not Found')
257+
await expect(page.locator('h1')).toHaveText('404 Not Found')
258258

259259
expect(headers['debug-netlify-cdn-cache-control']).toBe(
260260
nextVersionSatisfies('>=15.0.0-canary.187')

tests/e2e/turborepo.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ test.describe('[PNPM] Package manager', () => {
4141
: 's-maxage=31536000, stale-while-revalidate=31536000, durable',
4242
)
4343

44-
const date1 = await page.textContent('[data-testid="date-now"]')
45-
const h1 = await page.textContent('h1')
44+
const date1 = await page.locator('[data-testid="date-now"]').textContent()
45+
const h1 = await page.locator('h1').textContent()
4646
expect(h1).toBe('Show #71')
4747

4848
const response2 = await pollUntilHeadersMatch(
@@ -74,7 +74,7 @@ test.describe('[PNPM] Package manager', () => {
7474
)
7575

7676
// the page is cached
77-
const date2 = await page.textContent('[data-testid="date-now"]')
77+
const date2 = await page.locator('[data-testid="date-now"]').textContent()
7878
expect(date2).toBe(date1)
7979

8080
const revalidate = await page.goto(new URL('/api/revalidate', turborepo.url).href)
@@ -104,7 +104,7 @@ test.describe('[PNPM] Package manager', () => {
104104
expect(headers3?.['x-nextjs-cache']).toBeUndefined()
105105

106106
// the page has now an updated date
107-
const date3 = await page.textContent('[data-testid="date-now"]')
107+
const date3 = await page.locator('[data-testid="date-now"]').textContent()
108108
expect(date3).not.toBe(date2)
109109
})
110110
})
@@ -149,8 +149,8 @@ test.describe('[NPM] Package manager', () => {
149149
: 's-maxage=31536000, stale-while-revalidate=31536000, durable',
150150
)
151151

152-
const date1 = await page.textContent('[data-testid="date-now"]')
153-
const h1 = await page.textContent('h1')
152+
const date1 = await page.locator('[data-testid="date-now"]').textContent()
153+
const h1 = await page.locator('h1').textContent()
154154
expect(h1).toBe('Show #71')
155155

156156
const response2 = await pollUntilHeadersMatch(
@@ -182,7 +182,7 @@ test.describe('[NPM] Package manager', () => {
182182
)
183183

184184
// the page is cached
185-
const date2 = await page.textContent('[data-testid="date-now"]')
185+
const date2 = await page.locator('[data-testid="date-now"]').textContent()
186186
expect(date2).toBe(date1)
187187

188188
const revalidate = await page.goto(new URL('/api/revalidate', turborepoNPM.url).href)
@@ -212,7 +212,7 @@ test.describe('[NPM] Package manager', () => {
212212
expect(headers3?.['x-nextjs-cache']).toBeUndefined()
213213

214214
// the page has now an updated date
215-
const date3 = await page.textContent('[data-testid="date-now"]')
215+
const date3 = await page.locator('[data-testid="date-now"]').textContent()
216216
expect(date3).not.toBe(date2)
217217
})
218218

0 commit comments

Comments
 (0)