Unverified Commit b0837e0f authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[test] Exclude likely Next.js internal Components from component stacks in…

[test] Exclude likely Next.js internal Components from component stacks in Redbox assertions (#85420)
parent 456d8e7b
import { ReactNode } from 'react' import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) { export default function RootLayout({ children }: { children: ReactNode }) {
return ( return (
<html> <html>
<body>{children}</body> <body>{children}</body>
......
...@@ -4,7 +4,7 @@ import { ReactNode } from 'react' ...@@ -4,7 +4,7 @@ import { ReactNode } from 'react'
const isServer = typeof window === 'undefined' const isServer = typeof window === 'undefined'
export default function Root({ children }: { children: ReactNode }) { export default function RootLayout({ children }: { children: ReactNode }) {
return ( return (
<html {...(isServer ? { className: 'server-html' } : undefined)}> <html {...(isServer ? { className: 'server-html' } : undefined)}>
<body>{children}</body> <body>{children}</body>
......
import Script from 'next/script' import Script from 'next/script'
import { ReactNode } from 'react' import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) { export default function RootLayout({ children }: { children: ReactNode }) {
return ( return (
<html> <html>
<body>{children}</body> <body>{children}</body>
......
...@@ -1446,24 +1446,75 @@ export function getSnapshotTestDescribe(variant: TestVariants) { ...@@ -1446,24 +1446,75 @@ export function getSnapshotTestDescribe(variant: TestVariants) {
return shouldSkip ? describe.skip : describe return shouldSkip ? describe.skip : describe
} }
const nextjsClientComponentNames = [
// Pages Router
'App',
'AppContainer',
'Container',
'Head',
'PagesDevOverlayBridge',
'PagesDevOverlayErrorBoundary',
'PathnameContextProviderAdapter',
// App Router
'ClientPageRoot',
'ClientSegmentRoot',
'HTTPAccessFallbackBoundary',
'HTTPAccessFallbackErrorBoundary',
'InnerLayoutRouter',
'InnerScrollAndFocusHandler',
'RedirectBoundary',
'RedirectErrorBoundary',
'RenderFromTemplateContext',
'Root',
'ScrollAndFocusHandler',
'SegmentViewNode',
'SegmentTrieNode',
// These are added due to user actions e.g. loading.js -> LoadingBoundary
// They may be relevant in some context in the future.
// Consider including them in different assertions.
'ErrorBoundary',
'LoadingBoundary',
]
const nextjsClientComponentStackFrame = new RegExp(
`^(\\s*)<(${nextjsClientComponentNames.join('|')})(>| )`
)
/** /**
* @returns `null` if there are no frames * @returns `null` if there are no frames
*/ */
export async function getRedboxComponentStack( export async function getRedboxComponentStack(
browser: Playwright browser: Playwright,
includeNextjsInternalComponents = false
): Promise<string | null> { ): Promise<string | null> {
const componentStackFrameElements = await browser.elementsByCss( const componentStackTraceElements = await browser.elementsByCss(
'[data-nextjs-container-errors-pseudo-html] code' '[data-nextjs-container-errors-pseudo-html] code'
) )
if (componentStackFrameElements.length === 0) { if (componentStackTraceElements.length === 0) {
return null return null
} }
const componentStackFrameTexts = await Promise.all( const componentStackTrace = await componentStackTraceElements[0].innerText()
componentStackFrameElements.map((f) => f.innerText()) const componentStackFrames = componentStackTrace.split('\n')
)
return componentStackFrames
.map((componentStackFrame) => {
if (!includeNextjsInternalComponents) {
const componentStackFrameMatch = componentStackFrame.match(
nextjsClientComponentStackFrame
)
// React component stack frames aren't subject to ignore-listing.
// They're not relevant for our tests though.
// If you need to assert on Next.js internal component frames,
// use `getRedboxComponentStack(browser, true)` instead.
if (componentStackFrameMatch) {
return componentStackFrameMatch[1] + '<Next.js Internal Component>'
}
}
return componentStackFrameTexts.join('\n').trim() return componentStackFrame
})
.join('\n')
.trim()
} }
export async function hasRedboxCallStack(browser: Playwright) { export async function hasRedboxCallStack(browser: Playwright) {
......
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