Skip to content

Commit 6aac5e8

Browse files
committed
test: migrate page.textContent to locator.textContent
1 parent 817975d commit 6aac5e8

File tree

9 files changed

+63
-51
lines changed

9 files changed

+63
-51
lines changed

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

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

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

101101
const h1 = await page.locator('h1').textContent()
102102
expect(h1).toBe(expectedH1Content)
@@ -127,7 +127,7 @@ test.describe('app router on-demand revalidation', () => {
127127
)
128128

129129
// the page is cached
130-
const date2 = await page.locator('[data-testid="date-now"]').textContent()
130+
const date2 = await page.getByTestId("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.locator('[data-testid="date-now"]').textContent()
162+
const date3 = await page.getByTestId("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.locator('[data-testid="date-now"]').textContent()
191+
const date4 = await page.getByTestId("date-now").textContent()
192192
expect(date4).toBe(date3)
193193
})
194194
}

tests/e2e/page-router.test.ts

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

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

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

@@ -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.locator('[data-testid="date-now"]').textContent()
241+
const date2 = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()
302+
const date3 = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()) ?? ''
369+
const date1 = (await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()) ?? ''
394+
const date2 = (await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()) ?? ''
419+
const date1 = (await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()) ?? ''
434+
const date2 = (await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()) ?? ''
453+
const date3 = (await page.getByTestId('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-
await expect(page.locator('p')).toHaveText('Custom 404 page')
472+
await expect(page.getByTestId('custom-404')).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-
await expect(page.locator('p')).toHaveText('Custom 404 page')
496+
await expect(page.getByTestId('custom-404')).toHaveText('Custom 404 page')
497497

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

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

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

@@ -790,11 +790,11 @@ test.describe('Page Router with basePath and i18n', () => {
790790
)
791791

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

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

@@ -861,7 +861,7 @@ test.describe('Page Router with basePath and i18n', () => {
861861
)
862862

863863
// the page is cached
864-
const date2ImplicitLocale = await page.locator('[data-testid="date-now"]').textContent()
864+
const date2ImplicitLocale = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()
896+
const date2ExplicitLocale = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()
964+
const date3ImplicitLocale = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()
987+
const date3ExplicitLocale = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()
1060+
const date4ImplicitLocale = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()
1083+
const date4ExplicitLocale = await page.getByTestId('date-now').textContent()
10841084
expect(date4ExplicitLocale).not.toBe(date3ExplicitLocale)
10851085

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

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

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

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

12431243
// the page is cached
1244-
const date2 = await page.locator('[data-testid="date-now"]').textContent()
1244+
const date2 = await page.getByTestId('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.locator('[data-testid="date-now"]').textContent()
1312+
const date3 = await page.getByTestId('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-
await expect(page.locator('p')).toHaveText('Custom 404 page for locale: en')
1363+
await expect(page.getByTestId('custom-404')).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-
await expect(page.locator('p')).toHaveText('Custom 404 page for locale: en')
1381+
await expect(page.getByTestId('custom-404')).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/turborepo.test.ts

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

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

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

7676
// the page is cached
77-
const date2 = await page.locator('[data-testid="date-now"]').textContent()
77+
const date2 = await page.getByTestId("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.locator('[data-testid="date-now"]').textContent()
107+
const date3 = await page.getByTestId("date-now").textContent()
108108
expect(date3).not.toBe(date2)
109109
})
110110
})
@@ -149,7 +149,7 @@ test.describe('[NPM] Package manager', () => {
149149
: 's-maxage=31536000, stale-while-revalidate=31536000, durable',
150150
)
151151

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

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

184184
// the page is cached
185-
const date2 = await page.locator('[data-testid="date-now"]').textContent()
185+
const date2 = await page.getByTestId("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.locator('[data-testid="date-now"]').textContent()
215+
const date3 = await page.getByTestId("date-now").textContent()
216216
expect(date3).not.toBe(date2)
217217
})
218218

tests/fixtures/page-router-404-get-static-props-with-revalidate/pages/404.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function NotFound({ timestamp }) {
22
return (
3-
<p>
3+
<p data-testid="custom-404">
44
Custom 404 page with revalidate: <pre data-testid="timestamp">{timestamp}</pre>
55
</p>
66
)

tests/fixtures/page-router-base-path-i18n/pages/404.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default function NotFound({ locale }) {
22
return (
3-
<p>
3+
<p data-testid="custom-404">
44
Custom 404 page for locale: <pre data-testid="locale">{locale}</pre>
55
</p>
66
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default function NotFound() {
2-
return <p>Custom 404 page</p>
2+
return <p data-testid="custom-404">Custom 404 page</p>
33
}

tests/integration/simple-app.test.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
startMockBlobStore,
3636
} from '../utils/helpers.js'
3737
import {
38+
hasDefaultTurbopackBuilds,
3839
nextVersionSatisfies,
3940
shouldHaveAppRouterGlobalErrorInPrerenderManifest,
4041
shouldHaveAppRouterNotFoundInPrerenderManifest,
@@ -421,19 +422,23 @@ test.skipIf(process.env.NEXT_VERSION !== 'canary')<FixtureTestContext>(
421422
},
422423
)
423424

424-
test<FixtureTestContext>('can require CJS module that is not bundled', async (ctx) => {
425-
await createFixture('simple', ctx)
426-
await runPlugin(ctx)
425+
// setup for this test only works with webpack builds due to usage of ` __non_webpack_require__` to avoid bundling a file
426+
test.skipIf(hasDefaultTurbopackBuilds())<FixtureTestContext>(
427+
'can require CJS module that is not bundled',
428+
async (ctx) => {
429+
await createFixture('simple', ctx)
430+
await runPlugin(ctx)
427431

428-
const response = await invokeFunction(ctx, { url: '/api/cjs-file-with-js-extension' })
432+
const response = await invokeFunction(ctx, { url: '/api/cjs-file-with-js-extension' })
429433

430-
expect(response.statusCode).toBe(200)
434+
expect(response.statusCode).toBe(200)
431435

432-
const parsedBody = JSON.parse(response.body)
436+
const parsedBody = JSON.parse(response.body)
433437

434-
expect(parsedBody.notBundledCJSModule.isBundled).toEqual(false)
435-
expect(parsedBody.bundledCJSModule.isBundled).toEqual(true)
436-
})
438+
expect(parsedBody.notBundledCJSModule.isBundled).toEqual(false)
439+
expect(parsedBody.bundledCJSModule.isBundled).toEqual(true)
440+
},
441+
)
437442

438443
describe('next patching', async () => {
439444
const { cp: originalCp, appendFile } = (await vi.importActual(

tests/utils/fixture.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
} from '../../src/build/plugin-context.js'
3232
import { BLOB_TOKEN } from './constants.mjs'
3333
import { type FixtureTestContext } from './contexts.js'
34-
import { setNextVersionInFixture } from './next-version-helpers.mjs'
34+
import { hasDefaultTurbopackBuilds, setNextVersionInFixture } from './next-version-helpers.mjs'
3535

3636
const bootstrapURL = await getBootstrapURL()
3737
const actualCwd = await vi.importActual<typeof import('process')>('process').then((p) => p.cwd())
@@ -569,5 +569,8 @@ export async function invokeSandboxedFunction(
569569
}
570570

571571
export const EDGE_MIDDLEWARE_FUNCTION_NAME = '___netlify-edge-handler-middleware'
572-
export const EDGE_MIDDLEWARE_SRC_FUNCTION_NAME = '___netlify-edge-handler-src-middleware'
572+
// Turbopack has different output than webpack
573+
export const EDGE_MIDDLEWARE_SRC_FUNCTION_NAME = hasDefaultTurbopackBuilds()
574+
? '___netlify-edge-handler-src-middleware'
575+
: EDGE_MIDDLEWARE_FUNCTION_NAME
573576
export const NODE_MIDDLEWARE_FUNCTION_NAME = '___netlify-edge-handler-node-middleware'

0 commit comments

Comments
 (0)