Skip to content

Commit b6fb280

Browse files
committed
fix(test/e2e/prerender): Remove race condition in test
1 parent b0e246f commit b6fb280

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

test/e2e/prerender.test.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ describe('Prerender', () => {
551551
})
552552
}
553553

554-
const runTests = (isDev = false, isDeploy) => {
554+
const runTests = (isDev: boolean = false, isDeploy: boolean) => {
555555
navigateTest(isDev)
556556

557557
it('should respond with 405 for POST to static page', async () => {
@@ -926,33 +926,45 @@ describe('Prerender', () => {
926926
const text = await browser.elementByCss('p').text()
927927
expect(text).toContain('hi fallback')
928928

929-
// wait for fallback data to load
930-
await check(() => browser.elementByCss('p').text(), /Post/)
929+
if (isDeploy) {
930+
// patchFile does not work with deploy tests
931+
return
932+
}
933+
934+
await next.patchFile('resolve-static-props', '', async () => {
935+
// wait for fallback data to load
936+
await check(() => browser.elementByCss('p').text(), /Post/)
931937

932-
// check fallback data
933-
const post = await browser.elementByCss('p').text()
934-
const query = JSON.parse(await browser.elementByCss('#query').text())
935-
const params = JSON.parse(await browser.elementByCss('#params').text())
938+
// check fallback data
939+
const post = await browser.elementByCss('p').text()
940+
const query = JSON.parse(await browser.elementByCss('#query').text())
941+
const params = JSON.parse(await browser.elementByCss('#params').text())
936942

937-
expect(post).toContain('first/post')
938-
expect(params).toEqual({
939-
slug: 'first/post',
943+
expect(post).toContain('first/post')
944+
expect(params).toEqual({
945+
slug: 'first/post',
946+
})
947+
expect(query).toEqual(params)
940948
})
941-
expect(query).toEqual(params)
942949
})
943950

944-
it('should handle fallback only page correctly data', async () => {
945-
const data = JSON.parse(
946-
await renderViaHTTP(
947-
next.url,
948-
`/_next/data/${next.buildId}/fallback-only/second%2Fpost.json`
949-
)
950-
)
951+
// patchFile does not work with deploy tests
952+
if (!isDeploy) {
953+
it('should handle fallback only page correctly data', async () => {
954+
await next.patchFile('resolve-static-props', '', async () => {
955+
const data = JSON.parse(
956+
await renderViaHTTP(
957+
next.url,
958+
`/_next/data/${next.buildId}/fallback-only/second%2Fpost.json`
959+
)
960+
)
951961

952-
expect(data.pageProps.params).toEqual({
953-
slug: 'second/post',
962+
expect(data.pageProps.params).toEqual({
963+
slug: 'second/post',
964+
})
965+
})
954966
})
955-
})
967+
}
956968

957969
it('should 404 for a missing catchall explicit route', async () => {
958970
const res = await fetchViaHTTP(

test/e2e/prerender/pages/fallback-only/[slug].js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import Link from 'next/link'
33
import { useRouter } from 'next/router'
4+
import fs from 'node:fs/promises'
45

56
export async function getStaticPaths() {
67
return {
@@ -10,7 +11,13 @@ export async function getStaticPaths() {
1011
}
1112

1213
export async function getStaticProps({ params }) {
13-
await new Promise((resolve) => setTimeout(resolve, 1000))
14+
while (true) {
15+
try {
16+
await fs.stat('resolve-static-props')
17+
break
18+
} catch (e) {}
19+
await new Promise((resolve) => setTimeout(resolve, 100))
20+
}
1421

1522
return {
1623
props: {

0 commit comments

Comments
 (0)