Skip to content

Commit

Permalink
test: add case for handling invalid element
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Oct 30, 2024
1 parent 0169324 commit faefe3e
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client'

import Foo from '../foo'

export default function BrowserOnly() {
return (
<div>
<Foo />
</div>
)
}
11 changes: 11 additions & 0 deletions test/development/app-dir/invalid-element-type/app/browser/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client'

import dynamic from 'next/dynamic'

const BrowserOnly = dynamic(() => import('./browser-only'), {
ssr: false,
})

export default function Page() {
return <BrowserOnly />
}
Empty file.
8 changes: 8 additions & 0 deletions test/development/app-dir/invalid-element-type/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
9 changes: 9 additions & 0 deletions test/development/app-dir/invalid-element-type/app/rsc/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Foo from '../foo'

export default function Page() {
return (
<div>
<Foo />
</div>
)
}
11 changes: 11 additions & 0 deletions test/development/app-dir/invalid-element-type/app/ssr/page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use client'

import Foo from '../foo'

export default function Page() {
return (
<div>
<Foo />
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { nextTestSetup } from 'e2e-utils'
import { assertHasRedbox, getRedboxSource } from 'next-test-utils'

async function getStackFramesContent(browser) {
const stackFrameElements = await browser.elementsByCss(
'[data-nextjs-call-stack-frame]'
)
const stackFramesContent = (
await Promise.all(
stackFrameElements.map(async (frame) => {
const functionNameEl = await frame.$('[data-nextjs-frame-expanded]')
const sourceEl = await frame.$('[data-has-source]')
const functionName = functionNameEl
? await functionNameEl.innerText()
: ''
const source = sourceEl ? await sourceEl.innerText() : ''

if (!functionName) {
return ''
}
return `at ${functionName} (${source})`
})
)
)
.filter(Boolean)
.join('\n')

return stackFramesContent
}

describe('app-dir - invalid-element-type', () => {
const { next } = nextTestSetup({
files: __dirname,
})

it('should catch invalid element from on client-only component', async () => {
const browser = await next.browser('/browser')

await assertHasRedbox(browser)
const source = await getRedboxSource(browser)

const stackFramesContent = await getStackFramesContent(browser)
if (process.env.TURBOPACK) {
expect(stackFramesContent).toMatchInlineSnapshot(
`"at Page (app/browser/page.js (10:10))"`
)
expect(source).toMatchInlineSnapshot(`
"app/browser/browser-only.js (8:7) @ BrowserOnly
6 | return (
7 | <div>
> 8 | <Foo />
| ^
9 | </div>
10 | )
11 | }"
`)
} else {
expect(stackFramesContent).toMatchInlineSnapshot(
`"at BrowserOnly (app/browser/page.js (10:11))"`
)
expect(source).toMatchInlineSnapshot(`
"app/browser/browser-only.js (8:8) @ Foo
6 | return (
7 | <div>
> 8 | <Foo />
| ^
9 | </div>
10 | )
11 | }"
`)
}
})

it('should catch invalid element from on rsc component', async () => {
const browser = await next.browser('/rsc')

await assertHasRedbox(browser)
const stackFramesContent = await getStackFramesContent(browser)
const source = await getRedboxSource(browser)

if (process.env.TURBOPACK) {
expect(stackFramesContent).toMatchInlineSnapshot(`""`)
expect(source).toMatchInlineSnapshot(`
"app/rsc/page.js (6:7) @ Page
4 | return (
5 | <div>
> 6 | <Foo />
| ^
7 | </div>
8 | )
9 | }"
`)
} else {
expect(stackFramesContent).toMatchInlineSnapshot(`""`)
expect(source).toMatchInlineSnapshot(`
"app/rsc/page.js (6:8) @ Foo
4 | return (
5 | <div>
> 6 | <Foo />
| ^
7 | </div>
8 | )
9 | }"
`)
}
})

it('should catch invalid element from on ssr client component', async () => {
const browser = await next.browser('/ssr')

await assertHasRedbox(browser)

const stackFramesContent = await getStackFramesContent(browser)
const source = await getRedboxSource(browser)
if (process.env.TURBOPACK) {
expect(stackFramesContent).toMatchInlineSnapshot(`""`)
expect(source).toMatchInlineSnapshot(`
"app/ssr/page.js (8:7) @ Page
6 | return (
7 | <div>
> 8 | <Foo />
| ^
9 | </div>
10 | )
11 | }"
`)
} else {
expect(stackFramesContent).toMatchInlineSnapshot(`""`)
expect(source).toMatchInlineSnapshot(`
"app/ssr/page.js (8:8) @ Foo
6 | return (
7 | <div>
> 8 | <Foo />
| ^
9 | </div>
10 | )
11 | }"
`)
}
})
})
10 changes: 10 additions & 0 deletions test/development/app-dir/invalid-element-type/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
reactOwnerStack: true,
},
}

module.exports = nextConfig

0 comments on commit faefe3e

Please sign in to comment.