Skip to content

Commit a989f3f

Browse files
committed
Current behavior for reportError
1 parent fef9c5c commit a989f3f

File tree

3 files changed

+119
-16
lines changed

3 files changed

+119
-16
lines changed

test/development/pages-dir/client-navigation/index.test.ts

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
waitFor,
1010
check,
1111
} from 'next-test-utils'
12-
import webdriver from 'next-webdriver'
12+
import webdriver, { BrowserInterface } from 'next-webdriver'
1313
import path from 'path'
1414
import { nextTestSetup } from 'e2e-utils'
1515

@@ -56,12 +56,25 @@ describe('Client Navigation', () => {
5656
await browser.close()
5757
})
5858

59-
it('should have proper error when no children are provided', async () => {
60-
const browser = await webdriver(next.appPort, '/link-no-child')
59+
it('should have a redbox when no children are provided', async () => {
60+
const pageErrors: unknown[] = []
61+
const browser = await webdriver(next.appPort, '/link-no-child', {
62+
beforePageLoad: (page) => {
63+
page.on('pageerror', (error: unknown) => {
64+
pageErrors.push(error)
65+
})
66+
},
67+
})
6168
await assertHasRedbox(browser)
6269
expect(await getRedboxHeader(browser)).toContain(
6370
'No children were passed to <Link> with `href` of `/about` but one child is required'
6471
)
72+
expect(pageErrors).toEqual([
73+
expect.objectContaining({
74+
message:
75+
'No children were passed to <Link> with `href` of `/about` but one child is required https://nextjs.org/docs/messages/link-no-children',
76+
}),
77+
])
6578
})
6679

6780
it('should not throw error when one number type child is provided', async () => {
@@ -277,15 +290,28 @@ describe('Client Navigation', () => {
277290
})
278291

279292
describe('with empty getInitialProps()', () => {
280-
it('should render an error', async () => {
281-
let browser
293+
it('should render a redbox', async () => {
294+
let browser: BrowserInterface
282295
try {
283-
browser = await webdriver(next.appPort, '/nav')
296+
const pageErrors: unknown[] = []
297+
browser = await webdriver(next.appPort, '/nav', {
298+
beforePageLoad: (page) => {
299+
page.on('pageerror', (error: unknown) => {
300+
pageErrors.push(error)
301+
})
302+
},
303+
})
284304
await browser.elementByCss('#empty-props').click()
285305
await assertHasRedbox(browser)
286306
expect(await getRedboxHeader(browser)).toMatch(
287307
/should resolve to an object\. But found "null" instead\./
288308
)
309+
expect(pageErrors).toEqual([
310+
expect.objectContaining({
311+
message:
312+
'"EmptyInitialPropsPage.getInitialProps()" should resolve to an object. But found "null" instead.',
313+
}),
314+
])
289315
} finally {
290316
if (browser) {
291317
await browser.close()
@@ -1377,13 +1403,39 @@ describe('Client Navigation', () => {
13771403

13781404
describe('runtime errors', () => {
13791405
it('should show redbox when a client side error is thrown inside a component', async () => {
1380-
let browser
1406+
const isReact18 = process.env.NEXT_TEST_REACT_VERSION?.startsWith('18')
1407+
let browser: BrowserInterface
13811408
try {
1382-
browser = await webdriver(next.appPort, '/error-inside-browser-page')
1409+
const pageErrors: unknown[] = []
1410+
browser = await webdriver(next.appPort, '/error-inside-browser-page', {
1411+
beforePageLoad: (page) => {
1412+
page.on('pageerror', (error: unknown) => {
1413+
pageErrors.push(error)
1414+
})
1415+
},
1416+
})
13831417
await assertHasRedbox(browser)
13841418
const text = await getRedboxSource(browser)
13851419
expect(text).toMatch(/An Expected error occurred/)
13861420
expect(text).toMatch(/pages[\\/]error-inside-browser-page\.js \(5:13\)/)
1421+
1422+
expect(pageErrors).toEqual(
1423+
isReact18
1424+
? [
1425+
expect.objectContaining({
1426+
message: 'An Expected error occurred',
1427+
}),
1428+
expect.objectContaining({
1429+
message: 'An Expected error occurred',
1430+
}),
1431+
expect.objectContaining({
1432+
message:
1433+
'There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.',
1434+
}),
1435+
]
1436+
: // TODO(veil): Should contain thrown error
1437+
[]
1438+
)
13871439
} finally {
13881440
if (browser) {
13891441
await browser.close()
@@ -1392,16 +1444,27 @@ describe('Client Navigation', () => {
13921444
})
13931445

13941446
it('should show redbox when a client side error is thrown outside a component', async () => {
1395-
let browser
1447+
let browser: BrowserInterface
13961448
try {
1449+
const pageErrors: unknown[] = []
13971450
browser = await webdriver(
13981451
next.appPort,
1399-
'/error-in-the-browser-global-scope'
1452+
'/error-in-the-browser-global-scope',
1453+
{
1454+
beforePageLoad: (page) => {
1455+
page.on('pageerror', (error: unknown) => {
1456+
pageErrors.push(error)
1457+
})
1458+
},
1459+
}
14001460
)
14011461
await assertHasRedbox(browser)
14021462
const text = await getRedboxSource(browser)
14031463
expect(text).toMatch(/An Expected error occurred/)
14041464
expect(text).toMatch(/error-in-the-browser-global-scope\.js \(2:9\)/)
1465+
expect(pageErrors).toEqual([
1466+
expect.objectContaining({ message: 'An Expected error occurred' }),
1467+
])
14051468
} finally {
14061469
if (browser) {
14071470
await browser.close()

test/e2e/app-dir/errors/index.test.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ describe('app-dir - errors', () => {
1414

1515
describe('error component', () => {
1616
it('should trigger error component when an error happens during rendering', async () => {
17-
const browser = await next.browser('/client-component')
17+
const pageErrors: unknown[] = []
18+
const browser = await next.browser('/client-component', {
19+
beforePageLoad: (page) => {
20+
page.on('pageerror', (error: unknown) => {
21+
pageErrors.push(error)
22+
})
23+
},
24+
})
1825
await browser.elementByCss('#error-trigger-button').click()
1926

2027
if (isNextDev) {
@@ -27,10 +34,20 @@ describe('app-dir - errors', () => {
2734
await browser.waitForElementByCss('#error-boundary-message').text()
2835
).toBe('An error occurred: this is a test')
2936
}
37+
38+
// Handled by custom error boundary.
39+
expect(pageErrors).toEqual([])
3040
})
3141

3242
it('should trigger error component when an error happens during server components rendering', async () => {
33-
const browser = await next.browser('/server-component')
43+
const pageErrors: unknown[] = []
44+
const browser = await next.browser('/server-component', {
45+
beforePageLoad: (page) => {
46+
page.on('pageerror', (error: unknown) => {
47+
pageErrors.push(error)
48+
})
49+
},
50+
})
3451

3552
expect(
3653
await browser.waitForElementByCss('#error-boundary-message').text()
@@ -49,6 +66,9 @@ describe('app-dir - errors', () => {
4966
// await assertHasRedbox(browser)
5067
// expect(await getRedboxHeader(browser)).toMatch(/this is a test/)
5168
}
69+
70+
// Handled by custom error boundary.
71+
expect(pageErrors).toEqual([])
5272
})
5373

5474
it('should preserve custom digests', async () => {
@@ -143,7 +163,14 @@ describe('app-dir - errors', () => {
143163
})
144164

145165
it('should use default error boundary for prod and overlay for dev when no error component specified', async () => {
146-
const browser = await next.browser('/global-error-boundary/client')
166+
const pageErrors: unknown[] = []
167+
const browser = await next.browser('/global-error-boundary/client', {
168+
beforePageLoad: (page) => {
169+
page.on('pageerror', (error: unknown) => {
170+
pageErrors.push(error)
171+
})
172+
},
173+
})
147174
await browser.elementByCss('#error-trigger-button').click()
148175

149176
if (isNextDev) {
@@ -156,10 +183,20 @@ describe('app-dir - errors', () => {
156183
'Application error: a client-side exception has occurred while loading localhost (see the browser console for more information).'
157184
)
158185
}
186+
187+
// FIXME(veil): Should contain thrown error.
188+
expect(pageErrors).toEqual([])
159189
})
160190

161191
it('should display error digest for error in server component with default error boundary', async () => {
162-
const browser = await next.browser('/global-error-boundary/server')
192+
const pageErrors: unknown[] = []
193+
const browser = await next.browser('/global-error-boundary/server', {
194+
beforePageLoad: (page) => {
195+
page.on('pageerror', (error: unknown) => {
196+
pageErrors.push(error)
197+
})
198+
},
199+
})
163200

164201
if (isNextDev) {
165202
await assertHasRedbox(browser)
@@ -174,6 +211,9 @@ describe('app-dir - errors', () => {
174211
await browser.waitForElementByCss('body').elementByCss('p').text()
175212
).toMatch(/Digest: \w+/)
176213
}
214+
215+
// FIXME(veil): Should contain thrown error.
216+
expect(pageErrors).toEqual([])
177217
})
178218

179219
// production tests

test/turbopack-dev-tests-manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,7 +3231,7 @@
32313231
"Client Navigation with 404 pages should should not contain a page script in a 404 page",
32323232
"Client Navigation with <Link/> should call mouse handlers with an absolute url",
32333233
"Client Navigation with <Link/> should error when calling onClick without event",
3234-
"Client Navigation with <Link/> should have proper error when no children are provided",
3234+
"Client Navigation with <Link/> should have a redbox when no children are provided",
32353235
"Client Navigation with <Link/> should navigate an absolute local url",
32363236
"Client Navigation with <Link/> should navigate an absolute local url with as",
32373237
"Client Navigation with <Link/> should navigate an absolute url",
@@ -3261,7 +3261,7 @@
32613261
"Client Navigation with different types of urls should work with / page",
32623262
"Client Navigation with different types of urls should work with dir/ page",
32633263
"Client Navigation with different types of urls should work with normal page",
3264-
"Client Navigation with empty getInitialProps() should render an error",
3264+
"Client Navigation with empty getInitialProps() should render a redbox",
32653265
"Client Navigation with getInitialProp redirect should redirect the page via client side",
32663266
"Client Navigation with getInitialProp redirect should redirect the page when loading",
32673267
"Client Navigation with hash changes check hydration mis-match should not have hydration mis-match for hash link",

0 commit comments

Comments
 (0)