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

[test] Use new Redbox matchers in pages/ error-recovery test (#76348)

parent 49c26c7b
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ReactRefreshLogBox default syntax > runtime error 1`] = `
"index.js (5:9) @ eval
3 | setInterval(() => {
4 | i++
> 5 | throw Error('no ' + i)
| ^
6 | }, 1000)
7 | export default function FunctionNamed() {
8 | return <div />"
`;
exports[`ReactRefreshLogBox turbo syntax > runtime error 1`] = `
"index.js (5:9) @ eval
3 | setInterval(() => {
4 | i++
> 5 | throw Error('no ' + i)
| ^
6 | }, 1000)
7 | export default function FunctionNamed() {
8 | return <div />"
`;
/* eslint-env jest */
import { createSandbox } from 'development-sandbox'
import { FileRef, nextTestSetup } from 'e2e-utils'
import { check, describeVariants as describe, retry } from 'next-test-utils'
import { check, retry } from 'next-test-utils'
import { outdent } from 'outdent'
import path from 'path'
describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
const isReact18 = parseInt(process.env.NEXT_TEST_REACT_VERSION) === 18
describe('pages/ error recovery', () => {
const { next, isTurbopack } = nextTestSetup({
files: new FileRef(path.join(__dirname, 'fixtures', 'default-template')),
skipStart: true,
......@@ -13,7 +15,7 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
test('logbox: can recover from a syntax error without losing state', async () => {
await using sandbox = await createSandbox(next)
const { session } = sandbox
const { browser, session } = sandbox
await session.patch(
'index.js',
......@@ -40,10 +42,41 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
await session.patch('index.js', `export default () => <div/`)
await session.assertHasRedbox()
expect(await session.getRedboxSource()).toInclude(
'export default () => <div/'
)
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Parsing ecmascript source code failed",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js (1:27)
Parsing ecmascript source code failed
> 1 | export default () => <div/
| ^",
"stack": [],
}
`)
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: x Unexpected eof",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js
Error: x Unexpected eof
,----
1 | export default () => <div/
\`----
Caused by:
Syntax Error
Import trace for requested module:
./index.js
./pages/index.js",
"stack": [],
}
`)
}
await session.patch(
'index.js',
......@@ -73,7 +106,7 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
test('logbox: can recover from a event handler error', async () => {
await using sandbox = await createSandbox(next)
const { session } = sandbox
const { browser, session } = sandbox
await session.patch(
'index.js',
......@@ -104,32 +137,22 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
await session.evaluate(() => document.querySelector('p').textContent)
).toBe('1')
await session.assertHasRedbox()
if (isTurbopack) {
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"index.js (7:11) @ Index.useCallback[increment]
5 | const increment = useCallback(() => {
6 | setCount(c => c + 1)
> 7 | throw new Error('oops')
| ^
8 | }, [setCount])
9 | return (
10 | <main>"
`)
} else {
expect(await session.getRedboxSource()).toMatchInlineSnapshot(`
"index.js (7:11) @ Index.useCallback[increment]
5 | const increment = useCallback(() => {
6 | setCount(c => c + 1)
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: oops",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "index.js (7:11) @ Index.useCallback[increment]
> 7 | throw new Error('oops')
| ^
8 | }, [setCount])
9 | return (
10 | <main>"
`)
| ^",
"stack": [
"Index.useCallback[increment] index.js (7:11)",
"UtilityScript.evaluate <anonymous> (236:17)",
"UtilityScript.<anonymous> <anonymous> (1:44)",
],
}
`)
await session.patch(
'index.js',
outdent`
......@@ -163,7 +186,7 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
test('logbox: can recover from a component error', async () => {
await using sandbox = await createSandbox(next)
const { session } = sandbox
const { browser, session } = sandbox
await session.write(
'child.js',
......@@ -203,10 +226,44 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
`
)
await session.assertHasRedbox()
expect(await session.getRedboxSource()).toInclude(
'export default function Child()'
)
// Somehow we end up with two in React 18 due to React's attempt to recover from this error.
if (isReact18) {
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"description": "Error: oops",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "child.js (3:9) @ Child
> 3 | throw new Error('oops')
| ^",
"stack": [
"Child child.js (3:9)",
"Set.forEach <anonymous> (0:0)",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
],
}
`)
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: oops",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "child.js (3:9) @ Child
> 3 | throw new Error('oops')
| ^",
"stack": [
"Child child.js (3:9)",
"Set.forEach <anonymous> (0:0)",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
],
}
`)
}
const didNotReload = await session.patch(
'child.js',
......@@ -225,9 +282,9 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
})
// https://github.com/pmmmwh/react-refresh-webpack-plugin/pull/3#issuecomment-554137262
test('render error not shown right after syntax error', async () => {
it('render error not shown right after syntax error', async () => {
await using sandbox = await createSandbox(next)
const { session } = sandbox
const { browser, session } = sandbox
// Starting here:
await session.patch(
......@@ -263,7 +320,48 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
export default ClassDefault;
`
)
await session.assertHasRedbox()
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Parsing ecmascript source code failed",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js (5:5)
Parsing ecmascript source code failed
> 5 | return <h1>Default Export</h1>;
| ^^^^^^",
"stack": [],
}
`)
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: x Expected '{', got 'return'",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js
Error: x Expected '{', got 'return'
,-[5:1]
2 |
3 | class ClassDefault extends React.Component {
4 | render()
5 | return <h1>Default Export</h1>;
: ^^^^^^
6 | }
7 | }
\`----
Caused by:
Syntax Error
Import trace for requested module:
./index.js
./pages/index.js",
"stack": [],
}
`)
}
// Now change the code to introduce a runtime error without fixing the syntax error:
await session.patch(
......@@ -281,7 +379,49 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
export default ClassDefault;
`
)
await session.assertHasRedbox()
if (isTurbopack) {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Parsing ecmascript source code failed",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js (5:5)
Parsing ecmascript source code failed
> 5 | throw new Error('nooo');
| ^^^^^",
"stack": [],
}
`)
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: x Expected '{', got 'throw'",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js
Error: x Expected '{', got 'throw'
,-[5:1]
2 |
3 | class ClassDefault extends React.Component {
4 | render()
5 | throw new Error('nooo');
: ^^^^^
6 | return <h1>Default Export</h1>;
7 | }
8 | }
\`----
Caused by:
Syntax Error
Import trace for requested module:
./index.js
./pages/index.js",
"stack": [],
}
`)
}
// Now fix the syntax error:
await session.patch(
......@@ -299,22 +439,56 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
export default ClassDefault;
`
)
await session.assertHasRedbox()
// wait for patch to get applied
await retry(async () => {
await expect(session.getRedboxSource()).resolves.toInclude('render() {')
})
expect(await session.getRedboxSource()).toInclude(
"throw new Error('nooo');"
)
// Somehow we end up with two in React 18 due to React's attempt to recover from this error.
if (isReact18) {
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"description": "Error: nooo",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "index.js (5:11) @ ClassDefault.render
> 5 | throw new Error('nooo');
| ^",
"stack": [
"ClassDefault.render index.js (5:11)",
"Set.forEach <anonymous> (0:0)",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
],
}
`)
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: nooo",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "index.js (5:11) @ ClassDefault.render
> 5 | throw new Error('nooo');
| ^",
"stack": [
"ClassDefault.render index.js (5:11)",
"Set.forEach <anonymous> (0:0)",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
],
}
`)
}
})
// https://github.com/pmmmwh/react-refresh-webpack-plugin/pull/3#issuecomment-554144016
test('stuck error', async () => {
it('stuck error', async () => {
await using sandbox = await createSandbox(next)
const { session } = sandbox
const { browser, session } = sandbox
// We start here.
await session.patch(
......@@ -355,10 +529,44 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
)
// We get an error because Foo didn't import React. Fair.
await session.assertHasRedbox()
expect(await session.getRedboxSource()).toInclude(
"return React.createElement('h1', null, 'Foo');"
)
// Somehow we end up with two in React 18 due to React's attempt to recover from this error.
if (isReact18) {
await expect(browser).toDisplayRedbox(`
{
"count": 2,
"description": "ReferenceError: React is not defined",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "Foo.js (3:3) @ Foo
> 3 | return React.createElement('h1', null, 'Foo');
| ^",
"stack": [
"Foo Foo.js (3:3)",
"Set.forEach <anonymous> (0:0)",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
],
}
`)
} else {
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "ReferenceError: React is not defined",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "Foo.js (3:3) @ Foo
> 3 | return React.createElement('h1', null, 'Foo');
| ^",
"stack": [
"Foo Foo.js (3:3)",
"Set.forEach <anonymous> (0:0)",
"<FIXME-file-protocol>",
"<FIXME-file-protocol>",
],
}
`)
}
// Let's add that to Foo.
await session.patch(
......@@ -378,7 +586,7 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
// https://github.com/pmmmwh/react-refresh-webpack-plugin/pull/3#issuecomment-554150098
test('syntax > runtime error', async () => {
await using sandbox = await createSandbox(next)
const { session } = sandbox
const { browser, session } = sandbox
// Start here.
await session.patch(
......@@ -406,14 +614,22 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
}
`
)
await new Promise((resolve) => setTimeout(resolve, 1000))
await session.assertHasRedbox()
if (process.platform === 'win32') {
expect(await session.getRedboxSource()).toMatchSnapshot()
} else {
expect(await session.getRedboxSource()).toMatchSnapshot()
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: no 1",
"environmentLabel": null,
"label": "Unhandled Runtime Error",
"source": "index.js (5:9) @ eval
> 5 | throw Error('no ' + i)
| ^",
"stack": [
"eval index.js (5:9)",
],
}
`)
// Make a syntax error.
await session.patch(
......@@ -429,26 +645,31 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
)
await new Promise((resolve) => setTimeout(resolve, 1000))
await session.assertHasRedbox()
let redboxSource = next.normalizeTestDirContent(
await session.getRedboxSource()
)
if (isTurbopack) {
// TODO: Remove this branching once import traces are implemented in Turbopack
expect(redboxSource).toMatchInlineSnapshot(`
"./index.js (7:41)
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Parsing ecmascript source code failed",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js (7:41)
Parsing ecmascript source code failed
5 | throw Error('no ' + i)
6 | }, 1000)
> 7 | export default function FunctionNamed() {
| ^
Expected '}', got '<eof>'"
| ^",
"stack": [],
}
`)
} else {
expect(redboxSource).toMatchInlineSnapshot(`
"./index.js
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: x Expected '}', got '<eof>'",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js
Error: x Expected '}', got '<eof>'
,-[7:1]
4 | i++
......@@ -457,35 +678,42 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
7 | export default function FunctionNamed() {
: ^
\`----
Caused by:
Syntax Error
Import trace for requested module:
./index.js
./pages/index.js"
./pages/index.js",
"stack": [],
}
`)
}
// Test that runtime error does not take over:
await new Promise((resolve) => setTimeout(resolve, 2000))
await session.assertHasRedbox()
redboxSource = next.normalizeTestDirContent(await session.getRedboxSource())
if (isTurbopack) {
// TODO: Remove this branching once import traces are implemented in Turbopack
expect(redboxSource).toMatchInlineSnapshot(`
"./index.js (7:41)
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Parsing ecmascript source code failed",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js (7:41)
Parsing ecmascript source code failed
5 | throw Error('no ' + i)
6 | }, 1000)
> 7 | export default function FunctionNamed() {
| ^
Expected '}', got '<eof>'"
| ^",
"stack": [],
}
`)
} else {
expect(redboxSource).toMatchInlineSnapshot(`
"./index.js
await expect(browser).toDisplayRedbox(`
{
"count": 1,
"description": "Error: x Expected '}', got '<eof>'",
"environmentLabel": null,
"label": "Build Error",
"source": "./index.js
Error: x Expected '}', got '<eof>'
,-[7:1]
4 | i++
......@@ -494,13 +722,13 @@ describe.each(['default', 'turbo'])('ReactRefreshLogBox %s', () => {
7 | export default function FunctionNamed() {
: ^
\`----
Caused by:
Syntax Error
Import trace for requested module:
./index.js
./pages/index.js"
./pages/index.js",
"stack": [],
}
`)
}
})
......
......@@ -86,13 +86,7 @@ describe('non-root-project-monorepo', () => {
if (stack === null) {
return null
}
const isolatedPath = /file:\/\/.*\/next-install-[^/]+\//g
const nonIsolatedPath =
/file:\/\/.*\/test\/e2e\/app-dir\/non-root-project-monorepo\//g
return stack
.join('\n')
.replaceAll(nonIsolatedPath, 'file://<full-path>/')
.replaceAll(isolatedPath, 'file://<full-path>/')
return stack.join('\n')
}
it('should work on RSC', async () => {
......@@ -134,8 +128,8 @@ describe('non-root-project-monorepo', () => {
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"eval app/separate-file.ts (1:11)
<unknown> rsc)/./app/separate-file.ts (rsc://React/Server/file://<full-path>/apps/web/.next/server/app/source-maps-rsc/page.js?3 (63:1)
__webpack_require__ rsc:/Server/file://<full-path>/apps/web/.next/server/webpack-runtime.js (33:42)
<FIXME-file-protocol>
<FIXME-file-protocol>
innerArrowFunction app/source-maps-rsc/page.tsx (14:3)
innerFunction app/source-maps-rsc/page.tsx (10:3)
Page app/source-maps-rsc/page.tsx (4:5)"
......@@ -177,10 +171,10 @@ describe('non-root-project-monorepo', () => {
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"eval app/separate-file.ts (1:7)
./app/separate-file.ts file://<full-path>/apps/web/.next/static/chunks/app/source-maps-ssr/page.js (49:1)
options.factory file://<full-path>/apps/web/.next/static/chunks/webpack.js (700:31)
__webpack_require__ file://<full-path>/apps/web/.next/static/chunks/webpack.js (37:33)
fn file://<full-path>/apps/web/.next/static/chunks/webpack.js (357:21)
<FIXME-file-protocol>
<FIXME-file-protocol>
<FIXME-file-protocol>
<FIXME-file-protocol>
innerArrowFunction app/source-maps-ssr/page.tsx (16:3)
innerFunction app/source-maps-ssr/page.tsx (12:3)
Page app/source-maps-ssr/page.tsx (6:5)"
......@@ -222,10 +216,10 @@ describe('non-root-project-monorepo', () => {
expect(normalizeStackTrace(await getRedboxCallStack(browser)))
.toMatchInlineSnapshot(`
"eval app/separate-file.ts (1:7)
./app/separate-file.ts file://<full-path>/apps/web/.next/static/chunks/app/source-maps-client/page.js (49:1)
options.factory file://<full-path>/apps/web/.next/static/chunks/webpack.js (712:31)
__webpack_require__ file://<full-path>/apps/web/.next/static/chunks/webpack.js (37:33)
fn file://<full-path>/apps/web/.next/static/chunks/webpack.js (369:21)
<FIXME-file-protocol>
<FIXME-file-protocol>
<FIXME-file-protocol>
<FIXME-file-protocol>
innerArrowFunction app/source-maps-client/page.tsx (17:3)
innerFunction app/source-maps-client/page.tsx (13:3)
effectCallback app/source-maps-client/page.tsx (7:5)"
......
......@@ -1417,6 +1417,8 @@ export async function getRedboxCallStack(
stack.push('<FIXME-internal-frame>')
}
foundInternalFrame = true
} else if (frame.includes('file://')) {
stack.push('<FIXME-file-protocol>')
} else {
stack.push(frame)
}
......
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