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

Use snapshots to verify dynamic validation errors in dev mode (#80992)

Follow-up to #80946, asserting on the collapsed redbox errors in dev mode.

> [!NOTE]  
> This PR is best reviewed with hidden whitespace changes.
parent 186913a2
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { assertNoErrorToast } from 'next-test-utils'
import { getPrerenderOutput } from './utils' import { getPrerenderOutput } from './utils'
describe.each([ describe.each([
{ inDebugMode: true, name: 'With --prerender-debug' }, { inPrerenderDebugMode: true, name: 'With --prerender-debug' },
{ inDebugMode: false, name: 'Without --prerender-debug' }, // { inPrerenderDebugMode: false, name: 'Without --prerender-debug' },
])('Dynamic IO Errors - $name', ({ inDebugMode }) => { ])('Dynamic IO Errors - $name', ({ inPrerenderDebugMode }) => {
// We want to skip building and starting in start mode, and in dev mode when
// prerender debug mode is enabled, which doesn't exist for `next dev`.
const skipStart =
process.env.NEXT_TEST_MODE === 'start' || inPrerenderDebugMode
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,
skipDeployment: true, skipDeployment: true,
buildOptions: inDebugMode ? ['--debug-prerender'] : undefined, buildOptions: inPrerenderDebugMode ? ['--debug-prerender'] : undefined,
}) })
if (skipped) { if (skipped) {
...@@ -18,10 +24,16 @@ describe.each([ ...@@ -18,10 +24,16 @@ describe.each([
} }
if (isNextDev) { if (isNextDev) {
it('does not run in dev', () => {}) if (inPrerenderDebugMode) {
it('prerender debug mode does not exist for `next dev`', () => {})
return return
} }
it('should not show a collapsed redbox error', async () => {
const browser = await next.browser('/')
await assertNoErrorToast(browser)
})
} else {
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()
...@@ -33,14 +45,15 @@ describe.each([ ...@@ -33,14 +45,15 @@ describe.each([
const $ = await next.render$('/') const $ = await next.render$('/')
expect($('[data-fallback]').length).toBe(2) expect($('[data-fallback]').length).toBe(2)
}) })
}
}) })
describe('Sync Dynamic - Without Fallback - Math.random()', () => { describe('Sync Dynamic - Without Fallback - Math.random()', () => {
const { next, isNextDev, isTurbopack, 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,
skipDeployment: true, skipDeployment: true,
buildOptions: inDebugMode ? ['--debug-prerender'] : undefined, buildOptions: inPrerenderDebugMode ? ['--debug-prerender'] : undefined,
}) })
if (skipped) { if (skipped) {
...@@ -48,10 +61,31 @@ describe.each([ ...@@ -48,10 +61,31 @@ describe.each([
} }
if (isNextDev) { if (isNextDev) {
it('does not run in dev', () => {}) if (inPrerenderDebugMode) {
it('prerender debug mode does not exist for `next dev`', () => {})
return return
} }
it('should show a collapsed redbox error', async () => {
const browser = await next.browser('/')
await expect(browser).toDisplayCollapsedRedbox(`
{
"description": "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",
"environmentLabel": "Server",
"label": "Console Error",
"source": "app/page.tsx (35:23) @ RandomReadingComponent
> 35 | const random = Math.random()
| ^",
"stack": [
"RandomReadingComponent app/page.tsx (35:23)",
"JSON.parse <anonymous> (0:0)",
"LogSafely <anonymous> (0:0)",
],
}
`)
})
} else {
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()
...@@ -60,11 +94,11 @@ describe.each([ ...@@ -60,11 +94,11 @@ describe.each([
} }
const output = getPrerenderOutput(next.cliOutput, { const output = getPrerenderOutput(next.cliOutput, {
isMinified: !inDebugMode, isMinified: !inPrerenderDebugMode,
}) })
if (isTurbopack) { if (isTurbopack) {
if (inDebugMode) { if (inPrerenderDebugMode) {
expect(output).toMatchInlineSnapshot(` 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 "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) at RandomReadingComponent (turbopack:///[project]/app/page.tsx:35:22)
...@@ -89,7 +123,7 @@ describe.each([ ...@@ -89,7 +123,7 @@ describe.each([
`) `)
} }
} else { } else {
if (inDebugMode) { if (inPrerenderDebugMode) {
expect(output).toMatchInlineSnapshot(` 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 "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) at RandomReadingComponent (webpack:///app/page.tsx:35:22)
...@@ -115,5 +149,6 @@ describe.each([ ...@@ -115,5 +149,6 @@ describe.each([
} }
} }
}) })
}
}) })
}) })
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