| import type { MatcherContext } from 'expect' |
| import { toMatchInlineSnapshot } from 'jest-snapshot' |
| import { |
| assertHasRedbox, |
| getRedboxCallStack, |
| getRedboxComponentStack, |
| getRedboxDescription, |
| getRedboxEnvironmentLabel, |
| getRedboxSource, |
| getRedboxLabel, |
| getRedboxTotalErrorCount, |
| openRedbox, |
| } from './next-test-utils' |
| import type { Playwright } from 'next-webdriver' |
| import { NextInstance } from 'e2e-utils' |
|
|
| declare global { |
| namespace jest { |
| |
| interface Matchers<R> { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| toDisplayRedbox( |
| inlineSnapshot?: string, |
| opts?: ErrorSnapshotOptions |
| ): Promise<void> |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| toDisplayCollapsedRedbox( |
| inlineSnapshot?: string, |
| opts?: ErrorSnapshotOptions |
| ): Promise<void> |
| } |
| } |
| } |
|
|
| interface ErrorSnapshotOptions { |
| label?: boolean |
| } |
|
|
| interface ErrorSnapshot { |
| environmentLabel: string | null |
| label: string | null |
| description: string | null |
| componentStack?: string |
| source: string | null |
| stack: string[] | null |
| } |
|
|
| async function createErrorSnapshot( |
| browser: Playwright, |
| next: NextInstance | null, |
| { label: includeLabel = true }: ErrorSnapshotOptions = {} |
| ): Promise<ErrorSnapshot> { |
| const [label, environmentLabel, description, source, stack, componentStack] = |
| await Promise.all([ |
| includeLabel ? getRedboxLabel(browser) : null, |
| getRedboxEnvironmentLabel(browser), |
| getRedboxDescription(browser), |
| getRedboxSource(browser), |
| getRedboxCallStack(browser), |
| getRedboxComponentStack(browser), |
| ]) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| let focusedSource = source |
| if (source !== null) { |
| focusedSource = '' |
| const sourceFrameLines = source.split('\n') |
| for (let i = 0; i < sourceFrameLines.length; i++) { |
| const sourceFrameLine = sourceFrameLines[i].trimEnd() |
| if (sourceFrameLine === '') { |
| continue |
| } |
|
|
| if (sourceFrameLine.startsWith('>')) { |
| |
| |
| focusedSource += '\n' + sourceFrameLine |
| focusedSource += '\n' + sourceFrameLines[i + 1] |
| break |
| } |
| const isCodeFrameLine = /^ {2}\s*\d+ \|/.test(sourceFrameLine) |
| if (!isCodeFrameLine) { |
| focusedSource += '\n' + sourceFrameLine |
| } |
| } |
|
|
| focusedSource = focusedSource.trim() |
|
|
| if (next !== null) { |
| focusedSource = focusedSource.replaceAll( |
| next.testDir, |
| '<FIXME-project-root>' |
| ) |
| } |
|
|
| |
| |
| |
| |
| const sourceLines = focusedSource.split('\n') |
| if ( |
| sourceLines[0].startsWith('./node_modules/.pnpm/next@file+') || |
| sourceLines[0].startsWith('./node_modules/.pnpm/file+') || |
| |
| /^next-[a-zA-Z0-9\-_]+?-loader\?/.test(sourceLines[0]) |
| ) { |
| focusedSource = |
| `<FIXME-nextjs-internal-source>` + |
| '\n' + |
| sourceLines.slice(1).join('\n') |
| } |
| } |
|
|
| let sanitizedDescription = description |
|
|
| if (sanitizedDescription) { |
| sanitizedDescription = sanitizedDescription |
| .replace(/{imported module [^}]+}/, '<turbopack-module-id>') |
| .replace(/\w+_WEBPACK_IMPORTED_MODULE_\w+/, '<webpack-module-id>') |
|
|
| if (next !== null) { |
| sanitizedDescription = sanitizedDescription.replace( |
| next.testDir, |
| '<FIXME-project-root>' |
| ) |
| } |
| } |
|
|
| const snapshot: ErrorSnapshot = { |
| environmentLabel, |
| label: label ?? '<FIXME-excluded-label>', |
| description: sanitizedDescription, |
| source: focusedSource, |
| stack: |
| next !== null && stack !== null |
| ? stack.map((stackframe) => { |
| return stackframe.replace(next.testDir, '<FIXME-project-root>') |
| }) |
| : stack, |
| } |
|
|
| |
| |
| if (componentStack !== null) { |
| snapshot.componentStack = componentStack |
| } |
|
|
| return snapshot |
| } |
|
|
| type RedboxSnapshot = ErrorSnapshot | ErrorSnapshot[] |
|
|
| async function createRedboxSnapshot( |
| browser: Playwright, |
| next: NextInstance | null, |
| opts?: ErrorSnapshotOptions |
| ): Promise<RedboxSnapshot> { |
| const errorTally = await getRedboxTotalErrorCount(browser) |
| const errorSnapshots: ErrorSnapshot[] = [] |
|
|
| for (let errorIndex = 0; errorIndex < errorTally; errorIndex++) { |
| const errorSnapshot = await createErrorSnapshot(browser, next, opts) |
| errorSnapshots.push(errorSnapshot) |
|
|
| if (errorIndex < errorTally - 1) { |
| |
| await browser |
| .waitForElementByCss('[data-nextjs-dialog-error-next]') |
| .click() |
| |
| await browser.waitForElementByCss( |
| `[data-nextjs-dialog-error-index="${errorIndex + 1}"]` |
| ) |
| } |
| } |
|
|
| return errorSnapshots.length === 1 |
| ? |
| |
| errorSnapshots[0] |
| : errorSnapshots |
| } |
|
|
| expect.extend({ |
| async toDisplayRedbox( |
| this: MatcherContext, |
| browserOrContext: Playwright | { browser: Playwright; next: NextInstance }, |
| expectedRedboxSnapshot?: string, |
| opts?: ErrorSnapshotOptions |
| ) { |
| let browser: Playwright |
| let next: NextInstance | null |
| if ('browser' in browserOrContext && 'next' in browserOrContext) { |
| browser = browserOrContext.browser |
| next = browserOrContext.next |
| } else { |
| browser = browserOrContext |
| next = null |
| } |
|
|
| |
| |
| this.error = new Error() |
| |
| |
| |
| this.dontThrow = () => {} |
|
|
| try { |
| await assertHasRedbox(browser) |
| } catch (cause) { |
| |
| |
| if (expectedRedboxSnapshot === undefined) { |
| return toMatchInlineSnapshot.call(this, String(cause.message)) |
| } else { |
| return toMatchInlineSnapshot.call( |
| this, |
| String(cause.message), |
| expectedRedboxSnapshot |
| ) |
| } |
| } |
|
|
| const redbox = await createRedboxSnapshot(browser, next, opts) |
|
|
| |
| |
| if (expectedRedboxSnapshot === undefined) { |
| return toMatchInlineSnapshot.call(this, redbox) |
| } else { |
| return toMatchInlineSnapshot.call(this, redbox, expectedRedboxSnapshot) |
| } |
| }, |
| async toDisplayCollapsedRedbox( |
| this: MatcherContext, |
| browserOrContext: Playwright | { browser: Playwright; next: NextInstance }, |
| expectedRedboxSnapshot?: string, |
| opts?: ErrorSnapshotOptions |
| ) { |
| let browser: Playwright |
| let next: NextInstance | null |
| if ('browser' in browserOrContext && 'next' in browserOrContext) { |
| browser = browserOrContext.browser |
| next = browserOrContext.next |
| } else { |
| browser = browserOrContext |
| next = null |
| } |
|
|
| |
| |
| this.error = new Error() |
| |
| |
| |
| this.dontThrow = () => {} |
|
|
| try { |
| await openRedbox(browser) |
| } catch (cause) { |
| |
| |
| if (expectedRedboxSnapshot === undefined) { |
| return toMatchInlineSnapshot.call( |
| this, |
| String(cause.message) |
| |
| .replace('assertHasRedbox', 'toDisplayRedbox') |
| ) |
| } else { |
| return toMatchInlineSnapshot.call( |
| this, |
| String(cause.message) |
| |
| .replace('assertHasRedbox', 'toDisplayRedbox'), |
| expectedRedboxSnapshot |
| ) |
| } |
| } |
|
|
| const redbox = await createRedboxSnapshot(browser, next, opts) |
|
|
| |
| |
| if (expectedRedboxSnapshot === undefined) { |
| return toMatchInlineSnapshot.call(this, redbox) |
| } else { |
| return toMatchInlineSnapshot.call(this, redbox, expectedRedboxSnapshot) |
| } |
| }, |
| }) |
|
|