Unverified Commit b68caaf8 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

dx: add pagePath info to missing component error (#66916)

### What 

Show the full `pagePath` information for missing default component app
router convention files.

Move runtime error tests from `rsc-build-errors.test.ts` to the new test
`test/development/acceptance-app/undefined-default-export.test.ts`

### Why

Previously we only log `segment` which could be `[slug]` that is not
enough useful for users to locate the bad defined component
parent 515836da
...@@ -51,6 +51,12 @@ export function createComponentTree(props: { ...@@ -51,6 +51,12 @@ export function createComponentTree(props: {
) )
} }
function errorMissingDefaultExport(pagePath: string, convention: string) {
throw new Error(
`The default export is not a React Component in "${pagePath}/${convention}"`
)
}
async function createComponentTreeInternal({ async function createComponentTreeInternal({
createSegmentPath, createSegmentPath,
loaderTree: tree, loaderTree: tree,
...@@ -311,30 +317,22 @@ async function createComponentTreeInternal({ ...@@ -311,30 +317,22 @@ async function createComponentTreeInternal({
(isPage || typeof Component !== 'undefined') && (isPage || typeof Component !== 'undefined') &&
!isValidElementType(Component) !isValidElementType(Component)
) { ) {
throw new Error( errorMissingDefaultExport(pagePath, 'page')
`The default export is not a React Component in page: "${pagePath}"`
)
} }
if ( if (
typeof ErrorComponent !== 'undefined' && typeof ErrorComponent !== 'undefined' &&
!isValidElementType(ErrorComponent) !isValidElementType(ErrorComponent)
) { ) {
throw new Error( errorMissingDefaultExport(pagePath, 'error')
`The default export of error is not a React Component in page: ${segment}`
)
} }
if (typeof Loading !== 'undefined' && !isValidElementType(Loading)) { if (typeof Loading !== 'undefined' && !isValidElementType(Loading)) {
throw new Error( errorMissingDefaultExport(pagePath, 'loading')
`The default export of loading is not a React Component in ${segment}`
)
} }
if (typeof NotFound !== 'undefined' && !isValidElementType(NotFound)) { if (typeof NotFound !== 'undefined' && !isValidElementType(NotFound)) {
throw new Error( errorMissingDefaultExport(pagePath, 'not-found')
`The default export of notFound is not a React Component in ${segment}`
)
} }
} }
......
...@@ -124,41 +124,6 @@ describe('Error overlay - RSC build errors', () => { ...@@ -124,41 +124,6 @@ describe('Error overlay - RSC build errors', () => {
await cleanup() await cleanup()
}) })
it('should error when page component export is not valid', async () => {
const { session, cleanup } = await sandbox(
next,
undefined,
'/server-with-errors/page-export'
)
await next.patchFile(
'app/server-with-errors/page-export/page.js',
'export const a = 123'
)
await session.assertHasRedbox()
expect(await session.getRedboxDescription()).toInclude(
'The default export is not a React Component in page: "/server-with-errors/page-export"'
)
await cleanup()
})
it('should error when page component export is not valid on initial load', async () => {
const { session, cleanup } = await sandbox(
next,
undefined,
'/server-with-errors/page-export-initial-error'
)
await session.assertHasRedbox()
expect(await session.getRedboxDescription()).toInclude(
'The default export is not a React Component in page: "/server-with-errors/page-export-initial-error"'
)
await cleanup()
})
it('should throw an error when "use client" is on the top level but after other expressions', async () => { it('should throw an error when "use client" is on the top level but after other expressions', async () => {
const { session, cleanup } = await sandbox( const { session, cleanup } = await sandbox(
next, next,
......
import path from 'path'
import { FileRef, nextTestSetup } from 'e2e-utils'
import { sandbox } from 'development-sandbox'
describe('Undefined default export', () => {
const { next } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
})
it('should error if page component does not have default export', async () => {
const { session, cleanup } = await sandbox(
next,
new Map([
['app/(group)/specific-path/server/page.js', 'export const a = 123'],
]),
'/specific-path/server'
)
await session.assertHasRedbox()
expect(await session.getRedboxDescription()).toInclude(
'The default export is not a React Component in "/specific-path/server/page"'
)
await cleanup()
})
it('should error if not-found component does not have default export when trigger not-found boundary', async () => {
const { session, cleanup } = await sandbox(
next,
new Map([
[
'app/will-not-found/page.js',
`
import { notFound } from 'next/navigation'
export default function Page() { notFound() }
`,
],
['app/will-not-found/not-found.js', 'export const a = 123'],
]),
'/will-not-found'
)
await session.assertHasRedbox()
expect(await session.getRedboxDescription()).toInclude(
'The default export is not a React Component in "/will-not-found/not-found"'
)
await cleanup()
})
it('should error when page component export is not valid', async () => {
const { session, cleanup } = await sandbox(
next,
undefined,
'/server-with-errors/page-export'
)
await next.patchFile(
'app/server-with-errors/page-export/page.js',
'export const a = 123'
)
await session.assertHasRedbox()
expect(await session.getRedboxDescription()).toInclude(
'The default export is not a React Component in "/server-with-errors/page-export/page"'
)
await cleanup()
})
it('should error when page component export is not valid on initial load', async () => {
const { session, cleanup } = await sandbox(
next,
new Map([
[
'app/server-with-errors/page-export-initial-error/page.js',
'export const a = 123',
],
]),
'/server-with-errors/page-export-initial-error'
)
await session.assertHasRedbox()
expect(await session.getRedboxDescription()).toInclude(
'The default export is not a React Component in "/server-with-errors/page-export-initial-error/page"'
)
await cleanup()
})
})
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