Unverified Commit c910be03 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Use snapshots to verify error stack traces for dynamic validation errors (#80946)

For now, this is just documenting the current state of affairs with regards to how we print dynamic validation errors during prerendering.

Previously, the tests were run with and without `experimental.serverMinification`. Now, we're running them with and without the `--debug-prerender` CLI flag instead.

In follow-ups, we will add assertions for `next dev`, and also improve the errors for `next build`.

> [!NOTE]  
> This PR is best reviewed with hidden whitespace changes.
parent 345d4d4f
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { getPrerenderOutput } from './utils'
import { createExpectError } from './utils' describe.each([
{ inDebugMode: true, name: 'With --prerender-debug' },
{ inDebugMode: false, name: 'Without --prerender-debug' },
])('Dynamic IO Errors - $name', ({ inDebugMode }) => {
describe('Sync Dynamic - With Fallback - Math.random()', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname + '/fixtures/sync-random-with-fallback',
skipStart: true,
skipDeployment: true,
buildOptions: inDebugMode ? ['--debug-prerender'] : undefined,
})
function runTests(options: { withMinification: boolean }) { if (skipped) {
const { withMinification } = options return
describe(`Dynamic IO Errors - ${withMinification ? 'With Minification' : 'Without Minification'}`, () => { }
describe('Sync Dynamic - With Fallback - Math.random()', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname + '/fixtures/sync-random-with-fallback',
skipStart: true,
skipDeployment: true,
})
if (skipped) { if (isNextDev) {
return it('does not run in dev', () => {})
} return
}
if (isNextDev) { it('should not error the build when calling Math.random() if all dynamic access is inside a Suspense boundary', async () => {
it('does not run in dev', () => {}) try {
return await next.start()
} catch {
throw new Error('expected build not to fail for fully static project')
} }
beforeEach(async () => { expect(next.cliOutput).toContain('◐ / ')
if (!withMinification) { const $ = await next.render$('/')
await next.patchFile('next.config.js', (content) => expect($('[data-fallback]').length).toBe(2)
content.replace( })
'serverMinification: true,', })
'serverMinification: false,'
)
)
}
})
it('should not error the build when calling Math.random() if all dynamic access is inside a Suspense boundary', async () => {
try {
await next.start()
} catch {
throw new Error('expected build not to fail for fully static project')
}
expect(next.cliOutput).toContain('◐ / ') describe('Sync Dynamic - Without Fallback - Math.random()', () => {
const $ = await next.render$('/') const { next, isNextDev, isTurbopack, skipped } = nextTestSetup({
expect($('[data-fallback]').length).toBe(2) files: __dirname + '/fixtures/sync-random-without-fallback',
}) skipStart: true,
skipDeployment: true,
buildOptions: inDebugMode ? ['--debug-prerender'] : undefined,
}) })
describe('Sync Dynamic - Without Fallback - Math.random()', () => { if (skipped) {
const { next, isNextDev, skipped } = nextTestSetup({ return
files: __dirname + '/fixtures/sync-random-without-fallback', }
skipStart: true,
skipDeployment: true,
})
if (skipped) { if (isNextDev) {
return it('does not run in dev', () => {})
} return
}
if (isNextDev) { it('should error the build if Math.random() happens before some component outside a Suspense boundary is complete', async () => {
it('does not run in dev', () => {}) try {
return await next.start()
} catch {
// we expect the build to fail
} }
beforeEach(async () => { const output = getPrerenderOutput(next.cliOutput, {
if (!withMinification) { isMinified: !inDebugMode,
await next.patchFile('next.config.js', (content) =>
content.replace(
'serverMinification: true,',
'serverMinification: false,'
)
)
}
}) })
it('should error the build if Math.random() happens before some component outside a Suspense boundary is complete', async () => { if (isTurbopack) {
try { if (inDebugMode) {
await next.start() expect(output).toMatchInlineSnapshot(`
} catch { "Error: Route "/" used \`Math.random()\` outside of \`"use cache"\` and without explicitly calling \`await connection()\` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random
// we expect the build to fail at RandomReadingComponent (turbopack:///[project]/app/page.tsx:35:22)
33 | use(new Promise((r) => process.nextTick(r)))
34 | }
> 35 | const random = Math.random()
| ^
36 | return (
37 | <div>
38 | <span id="rand">{random}</span>
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
> Export encountered errors on following paths:
/page: /"
`)
} else {
expect(output).toMatchInlineSnapshot(`
"Error: Route "/" used \`Math.random()\` outside of \`"use cache"\` and without explicitly calling \`await connection()\` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random
at a (<next-dist-dir>)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /page: /, exiting the build."
`)
} }
const expectError = createExpectError(next.cliOutput) } else {
if (inDebugMode) {
expect(output).toMatchInlineSnapshot(`
"Error: Route "/" used \`Math.random()\` outside of \`"use cache"\` and without explicitly calling \`await connection()\` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random
at RandomReadingComponent (webpack:///app/page.tsx:35:22)
33 | use(new Promise((r) => process.nextTick(r)))
34 | }
> 35 | const random = Math.random()
| ^
36 | return (
37 | <div>
38 | <span id="rand">{random}</span>
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
expectError( > Export encountered errors on following paths:
'Error: Route "/" used `Math.random()` outside of `"use cache"` and without explicitly calling `await connection()` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random' /page: /"
) `)
expectError('Error occurred prerendering page "/"') } else {
expectError('exiting the build.') expect(output).toMatchInlineSnapshot(`
}) "Error: Route "/" used \`Math.random()\` outside of \`"use cache"\` and without explicitly calling \`await connection()\` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random
at a (<next-dist-dir>)
Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
Export encountered an error on /page: /, exiting the build."
`)
}
}
}) })
}) })
} })
runTests({ withMinification: true })
runTests({ withMinification: false })
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -7,7 +7,3 @@ export function IndirectionOne({ children }) { ...@@ -7,7 +7,3 @@ export function IndirectionOne({ children }) {
export function IndirectionTwo({ children }) { export function IndirectionTwo({ children }) {
return children return children
} }
export function IndirectionThree({ children }) {
return children
}
import { Suspense } from 'react' import { Suspense } from 'react'
import { IndirectionOne, IndirectionTwo, IndirectionThree } from './indirection' import { IndirectionOne, IndirectionTwo } from './indirection'
export default async function Page() { export default async function Page() {
return ( return (
...@@ -24,12 +24,10 @@ export default async function Page() { ...@@ -24,12 +24,10 @@ export default async function Page() {
<FetchingComponent nonce="d" /> <FetchingComponent nonce="d" />
</Suspense> </Suspense>
</IndirectionTwo> </IndirectionTwo>
<IndirectionThree> <FetchingComponent nonce="e" />
<FetchingComponent nonce="e" /> <Suspense fallback="loading...">
<Suspense fallback="loading..."> <FetchingComponent nonce="f" />
<FetchingComponent nonce="f" /> </Suspense>
</Suspense>
</IndirectionThree>
</> </>
) )
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: false,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: false,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
dynamicIO: true, dynamicIO: true,
serverMinification: true,
}, },
} }
......
const stackStart = /\s+at / // Used to deterministically stub out minified local names in stack traces.
const abc = 'abcdefghijklmnopqrstuvwxyz'
export function createExpectError(cliOutput: string) { const hostElementsUsedInFixtures = ['html', 'body', 'main', 'div']
let cliIndex = 0
return function expectError( export function getPrerenderOutput(
containing: string, cliOutput: string,
withStackContaining?: string { isMinified }: { isMinified: boolean }
): void { ): string {
const initialCliIndex = cliIndex const lines: string[] = []
let lines = cliOutput.slice(cliIndex).split('\n') let foundPrerenderingLine = false
let i = 0
let i = 0
while (i < lines.length) { const replaceNextDistStackFrame = () =>
let line = lines[i++] + '\n' `at ${abc[i++ % abc.length]} (<next-dist-dir>)`
cliIndex += line.length
if (line.includes(containing)) { const replaceAnonymousStackFrame = (_m, name) => {
if (typeof withStackContaining !== 'string') { const deterministicName = hostElementsUsedInFixtures.includes(name)
return ? name
} else { : abc[i++ % abc.length]
while (i < lines.length) {
let stackLine = lines[i++] + '\n' return `at ${deterministicName} (<anonymous>)`
if (!stackStart.test(stackLine)) { }
expect(stackLine).toContain(withStackContaining)
} for (const line of cliOutput.split('\n')) {
if (stackLine.includes(withStackContaining)) { if (line.includes('Collecting page data')) {
return foundPrerenderingLine = true
} continue
} }
}
} if (line.includes('Next.js build worker exited')) {
break
} }
expect(cliOutput.slice(initialCliIndex)).toContain(containing) if (foundPrerenderingLine && !line.includes('Generating static pages')) {
lines.push(
isMinified
? line
.replace(/at [\w.]+ \(.next[^)]+\)/, replaceNextDistStackFrame)
.replace(
/at ([\w.]+) \(<anonymous>\)/,
replaceAnonymousStackFrame
)
: line.replace(
/at ([\w.]+) \((webpack:\/\/)\/src[^)]+\)/,
`at $1 ($2<next-src>)`
)
)
}
} }
return lines.join('\n').trim()
} }
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