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' },
function runTests(options: { withMinification: boolean }) { { inDebugMode: false, name: 'Without --prerender-debug' },
const { withMinification } = options ])('Dynamic IO Errors - $name', ({ inDebugMode }) => {
describe(`Dynamic IO Errors - ${withMinification ? 'With Minification' : 'Without Minification'}`, () => {
describe('Sync Dynamic - With Fallback - Math.random()', () => { describe('Sync Dynamic - With Fallback - Math.random()', () => {
const { next, isNextDev, skipped } = nextTestSetup({ const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname + '/fixtures/sync-random-with-fallback', files: __dirname + '/fixtures/sync-random-with-fallback',
skipStart: true, skipStart: true,
skipDeployment: true, skipDeployment: true,
buildOptions: inDebugMode ? ['--debug-prerender'] : undefined,
}) })
if (skipped) { if (skipped) {
...@@ -21,17 +22,6 @@ function runTests(options: { withMinification: boolean }) { ...@@ -21,17 +22,6 @@ function runTests(options: { withMinification: boolean }) {
return return
} }
beforeEach(async () => {
if (!withMinification) {
await next.patchFile('next.config.js', (content) =>
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 () => { it('should not error the build when calling Math.random() if all dynamic access is inside a Suspense boundary', async () => {
try { try {
await next.start() await next.start()
...@@ -46,10 +36,11 @@ function runTests(options: { withMinification: boolean }) { ...@@ -46,10 +36,11 @@ function runTests(options: { withMinification: boolean }) {
}) })
describe('Sync Dynamic - Without Fallback - Math.random()', () => { describe('Sync Dynamic - Without Fallback - Math.random()', () => {
const { next, isNextDev, skipped } = nextTestSetup({ const { next, isNextDev, isTurbopack, skipped } = nextTestSetup({
files: __dirname + '/fixtures/sync-random-without-fallback', files: __dirname + '/fixtures/sync-random-without-fallback',
skipStart: true, skipStart: true,
skipDeployment: true, skipDeployment: true,
buildOptions: inDebugMode ? ['--debug-prerender'] : undefined,
}) })
if (skipped) { if (skipped) {
...@@ -61,34 +52,68 @@ function runTests(options: { withMinification: boolean }) { ...@@ -61,34 +52,68 @@ function runTests(options: { withMinification: boolean }) {
return return
} }
beforeEach(async () => {
if (!withMinification) {
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 () => { it('should error the build if Math.random() happens before some component outside a Suspense boundary is complete', async () => {
try { try {
await next.start() await next.start()
} catch { } catch {
// we expect the build to fail // we expect the build to fail
} }
const expectError = createExpectError(next.cliOutput)
expectError( const output = getPrerenderOutput(next.cliOutput, {
'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' isMinified: !inDebugMode,
)
expectError('Error occurred prerendering page "/"')
expectError('exiting the build.')
}) })
if (isTurbopack) {
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 (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."
`)
}
} 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
> 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."
`)
}
}
}) })
}) })
} })
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(
containing: string,
withStackContaining?: string
): void {
const initialCliIndex = cliIndex
let lines = cliOutput.slice(cliIndex).split('\n')
export function getPrerenderOutput(
cliOutput: string,
{ isMinified }: { isMinified: boolean }
): string {
const lines: string[] = []
let foundPrerenderingLine = false
let i = 0 let i = 0
while (i < lines.length) {
let line = lines[i++] + '\n' const replaceNextDistStackFrame = () =>
cliIndex += line.length `at ${abc[i++ % abc.length]} (<next-dist-dir>)`
if (line.includes(containing)) {
if (typeof withStackContaining !== 'string') { const replaceAnonymousStackFrame = (_m, name) => {
return const deterministicName = hostElementsUsedInFixtures.includes(name)
} else { ? name
while (i < lines.length) { : abc[i++ % abc.length]
let stackLine = lines[i++] + '\n'
if (!stackStart.test(stackLine)) { return `at ${deterministicName} (<anonymous>)`
expect(stackLine).toContain(withStackContaining)
}
if (stackLine.includes(withStackContaining)) {
return
} }
for (const line of cliOutput.split('\n')) {
if (line.includes('Collecting page data')) {
foundPrerenderingLine = true
continue
} }
if (line.includes('Next.js build worker exited')) {
break
} }
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>)`
)
)
} }
} }
expect(cliOutput.slice(initialCliIndex)).toContain(containing) 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