Unverified Commit bc335b88 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

test: unflake use-cache-search-params (#99691)

Reworks `use-cache-search-params` to fix flakiness. The
"/search-params-caught" test [occasionally fails like
this](https://github.com/vercel/next.js/actions/runs/37332777301/job/111840698762#step:35:9635).
i'm not sure what this is exactly (the prerender for the page fails
without logging the specific error?), but using separate prerenders for
each page seems to work around that, and that's what we should be doing
for build failures anyway.
parent a2b3c15e
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup, isNextDev, isNextStart } from 'e2e-utils'
import { assertNoConsoleErrors, waitForNoRedbox } from 'next-test-utils' import { assertNoConsoleErrors, waitForNoRedbox } from 'next-test-utils'
import stripAnsi from 'strip-ansi' import stripAnsi from 'strip-ansi'
const getExpectedErrorMessage = (route: string) => const getExpectedErrorMessage = (route: string) =>
`Route "${route}": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument.\nLearn more: https://nextjs.org/docs/messages/next-request-in-use-cache` `Route "${route}": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument.\nLearn more: https://nextjs.org/docs/messages/next-request-in-use-cache`
// TODO(deploy-test-completion): Re-enable this suite in deploy mode. // We use separate per-page prerenders for CLI output, which we cannot do in deploy.
// It likely asserts local CLI or runtime output that deploy tests do not expose.
// @force-gate !deploy // @force-gate !deploy
describe('use-cache-search-params', () => { describe('use-cache-search-params', () => {
const { next, isNextDev } = nextTestSetup({ const { next } = nextTestSetup({
files: __dirname, files: __dirname,
skipStart: process.env.NEXT_TEST_MODE !== 'dev', skipStart: isNextStart,
}) })
if (isNextDev) { beforeAll(async () => {
let route: string if (isNextStart) {
const args = ['--experimental-build-mode', 'compile']
describe('when searchParams are used inside of "use cache"', () => { const result = await next.build({ args })
beforeAll(() => { if (result.exitCode !== 0) {
route = '/search-params-used' throw new Error('Failed to build. CLI Output:\n\n' + result.cliOutput)
}
}
}) })
const prerenderPattern = async (pattern: string) => {
const args = [
'--experimental-build-mode',
'generate',
'--debug-build-paths',
pattern,
]
const result = await next.build({ args })
if (
result.cliOutput.includes(`Pattern "${pattern}" did not match any files`)
) {
throw new Error(`Pattern "${pattern}" did not match any files`)
}
return result
}
const prerenderPage = (route: string) => {
return prerenderPattern(`app${route}/page.tsx`)
}
describe('when searchParams are used inside of "use cache"', () => {
it('should show an error', async () => { it('should show an error', async () => {
const outputIndex = next.cliOutput.length const route = '/search-params-used'
if (isNextDev) {
const browser = await next.browser(`${route}?foo=1`) const browser = await next.browser(`${route}?foo=1`)
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Route "/search-params-used": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument. "description": "Route "/search-params-used": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument.
...@@ -40,23 +62,19 @@ describe('use-cache-search-params', () => { ...@@ -40,23 +62,19 @@ describe('use-cache-search-params', () => {
], ],
} }
`) `)
} else {
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex)) const result = await prerenderPage(route)
expect(result.cliOutput).toContain(getExpectedErrorMessage(route))
expect(cliOutput).toContain(`Error: ${getExpectedErrorMessage(route)} expect(result.exitCode).toBe(1)
at Page (app/search-params-used/page.tsx:8:17)`) }
}) })
}) })
describe('when searchParams are caught inside of "use cache"', () => { describe('when searchParams are caught inside of "use cache"', () => {
beforeAll(() => { const route = '/search-params-caught'
route = '/search-params-caught'
})
it('should show an error', async () => { it('should show an error', async () => {
const outputIndex = next.cliOutput.length if (isNextDev) {
const browser = await next.browser(`${route}?foo=1`) const browser = await next.browser(`${route}?foo=1`)
await expect(browser).toDisplayCollapsedRedbox(` await expect(browser).toDisplayCollapsedRedbox(`
{ {
"description": "Route "/search-params-caught": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument. "description": "Route "/search-params-caught": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument.
...@@ -71,13 +89,14 @@ describe('use-cache-search-params', () => { ...@@ -71,13 +89,14 @@ describe('use-cache-search-params', () => {
], ],
} }
`) `)
} else {
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex)) const result = await prerenderPage(route)
expect(result.cliOutput).toContain(getExpectedErrorMessage(route))
expect(cliOutput).toContain(`Error: ${getExpectedErrorMessage(route)} expect(result.exitCode).toBe(1)
at Page (app/search-params-caught/page.tsx:11:5)`) }
}) })
// @force-gate dev
it('should also show an error after the second reload', async () => { it('should also show an error after the second reload', async () => {
// There was an obscure bug that lead to the error not being triggered // There was an obscure bug that lead to the error not being triggered
// anymore starting with the third request. We test this scenario // anymore starting with the third request. We test this scenario
...@@ -104,27 +123,28 @@ describe('use-cache-search-params', () => { ...@@ -104,27 +123,28 @@ describe('use-cache-search-params', () => {
}) })
describe('when searchParams are unused inside of "use cache"', () => { describe('when searchParams are unused inside of "use cache"', () => {
beforeAll(() => { const route = '/search-params-unused'
route = '/search-params-unused'
})
it('should not show an error', async () => { it('should not show an error', async () => {
const outputIndex = next.cliOutput.length if (isNextDev) {
const getCliOutput = next.getCliOutputFromHere()
const browser = await next.browser(`${route}?foo=1`) const browser = await next.browser(`${route}?foo=1`)
await waitForNoRedbox(browser) await waitForNoRedbox(browser)
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex)) const cliOutput = stripAnsi(getCliOutput())
expect(cliOutput).not.toContain(getExpectedErrorMessage(route)) expect(cliOutput).not.toContain(getExpectedErrorMessage(route))
} else {
const result = await prerenderPage(route)
expect(result.cliOutput).not.toContain(getExpectedErrorMessage(route))
expect(result.exitCode).toBe(0)
}
}) })
}) })
it('should show an error when searchParams are used inside of a cached generateMetadata', async () => { it('should show an error when searchParams are used inside of a cached generateMetadata', async () => {
const browser = await next.browser( const route = '/search-params-used-generate-metadata'
'/search-params-used-generate-metadata?title=foo' if (isNextDev) {
) const browser = await next.browser(`${route}?title=foo`)
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Route "/search-params-used-generate-metadata": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument. "description": "Route "/search-params-used-generate-metadata": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument.
...@@ -139,13 +159,17 @@ describe('use-cache-search-params', () => { ...@@ -139,13 +159,17 @@ describe('use-cache-search-params', () => {
], ],
} }
`) `)
} else {
const result = await prerenderPage(route)
expect(result.cliOutput).toContain(getExpectedErrorMessage(route))
expect(result.exitCode).toBe(1)
}
}) })
it('should show an error when searchParams are used inside of a cached generateViewport', async () => { it('should show an error when searchParams are used inside of a cached generateViewport', async () => {
const browser = await next.browser( const route = '/search-params-used-generate-viewport'
'/search-params-used-generate-viewport?color=red' if (isNextDev) {
) const browser = await next.browser(`${route}?color=red`)
await expect(browser).toDisplayRedbox(` await expect(browser).toDisplayRedbox(`
{ {
"description": "Route "/search-params-used-generate-viewport": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument. "description": "Route "/search-params-used-generate-viewport": \`searchParams\` can't be read inside \`"use cache"\`. Await it outside the cached function and pass what you need as an argument.
...@@ -160,44 +184,23 @@ describe('use-cache-search-params', () => { ...@@ -160,44 +184,23 @@ describe('use-cache-search-params', () => {
], ],
} }
`) `)
})
} else { } else {
afterEach(async () => { const result = await prerenderPage(route)
await next.stop() expect(result.cliOutput).toContain(getExpectedErrorMessage(route))
}) expect(result.exitCode).toBe(1)
}
it('should fail the build with errors', async () => {
const { cliOutput } = await next.build()
expect(cliOutput).toInclude(
getExpectedErrorMessage('/search-params-used')
)
expect(cliOutput).toInclude(
getExpectedErrorMessage('/search-params-caught')
)
expect(cliOutput).not.toInclude(
getExpectedErrorMessage('/search-params-unused')
)
expect(cliOutput).toInclude(
'Error occurred prerendering page "/search-params-used"'
)
expect(cliOutput).toInclude(
'Error occurred prerendering page "/search-params-caught"'
)
expect(cliOutput).not.toInclude(
'Error occurred prerendering page "/search-params-unused"'
)
}) })
// @force-gate start
describe('runtime behavior', () => {
afterEach(() => next.stop())
it('should resume a cached page that does not access search params without hydration errors', async () => { it('should resume a cached page that does not access search params without hydration errors', async () => {
await next.build({ const result = await next.build({
args: ['--debug-build-paths', 'app/search-params-unused/page.tsx'], args: ['--debug-build-paths', 'app/search-params-unused/page.tsx'],
}) })
if (result.exitCode !== 0) {
throw new Error('Failed to build. CLI Output:\n\n' + result.cliOutput)
}
await next.start({ skipBuild: true }) await next.start({ skipBuild: true })
...@@ -225,5 +228,5 @@ describe('use-cache-search-params', () => { ...@@ -225,5 +228,5 @@ describe('use-cache-search-params', () => {
// replaced by a new runtime date. // replaced by a new runtime date.
await assertNoConsoleErrors(browser) await assertNoConsoleErrors(browser)
}) })
} })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment