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

Current behavior for `reportError` (#76115)

parent a7511f53
......@@ -9,7 +9,7 @@ import {
waitFor,
check,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import webdriver, { BrowserInterface } from 'next-webdriver'
import path from 'path'
import { nextTestSetup } from 'e2e-utils'
......@@ -56,12 +56,25 @@ describe('Client Navigation', () => {
await browser.close()
})
it('should have proper error when no children are provided', async () => {
const browser = await webdriver(next.appPort, '/link-no-child')
it('should have a redbox when no children are provided', async () => {
const pageErrors: unknown[] = []
const browser = await webdriver(next.appPort, '/link-no-child', {
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
})
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toContain(
'No children were passed to <Link> with `href` of `/about` but one child is required'
)
expect(pageErrors).toEqual([
expect.objectContaining({
message:
'No children were passed to <Link> with `href` of `/about` but one child is required https://nextjs.org/docs/messages/link-no-children',
}),
])
})
it('should not throw error when one number type child is provided', async () => {
......@@ -277,15 +290,28 @@ describe('Client Navigation', () => {
})
describe('with empty getInitialProps()', () => {
it('should render an error', async () => {
let browser
it('should render a redbox', async () => {
let browser: BrowserInterface
try {
browser = await webdriver(next.appPort, '/nav')
const pageErrors: unknown[] = []
browser = await webdriver(next.appPort, '/nav', {
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
})
await browser.elementByCss('#empty-props').click()
await assertHasRedbox(browser)
expect(await getRedboxHeader(browser)).toMatch(
/should resolve to an object\. But found "null" instead\./
)
expect(pageErrors).toEqual([
expect.objectContaining({
message:
'"EmptyInitialPropsPage.getInitialProps()" should resolve to an object. But found "null" instead.',
}),
])
} finally {
if (browser) {
await browser.close()
......@@ -1377,13 +1403,39 @@ describe('Client Navigation', () => {
describe('runtime errors', () => {
it('should show redbox when a client side error is thrown inside a component', async () => {
let browser
const isReact18 = process.env.NEXT_TEST_REACT_VERSION?.startsWith('18')
let browser: BrowserInterface
try {
browser = await webdriver(next.appPort, '/error-inside-browser-page')
const pageErrors: unknown[] = []
browser = await webdriver(next.appPort, '/error-inside-browser-page', {
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
})
await assertHasRedbox(browser)
const text = await getRedboxSource(browser)
expect(text).toMatch(/An Expected error occurred/)
expect(text).toMatch(/pages[\\/]error-inside-browser-page\.js \(5:13\)/)
expect(pageErrors).toEqual(
isReact18
? [
expect.objectContaining({
message: 'An Expected error occurred',
}),
expect.objectContaining({
message: 'An Expected error occurred',
}),
expect.objectContaining({
message:
'There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.',
}),
]
: // TODO(veil): Should contain thrown error
[]
)
} finally {
if (browser) {
await browser.close()
......@@ -1392,16 +1444,27 @@ describe('Client Navigation', () => {
})
it('should show redbox when a client side error is thrown outside a component', async () => {
let browser
let browser: BrowserInterface
try {
const pageErrors: unknown[] = []
browser = await webdriver(
next.appPort,
'/error-in-the-browser-global-scope'
'/error-in-the-browser-global-scope',
{
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
}
)
await assertHasRedbox(browser)
const text = await getRedboxSource(browser)
expect(text).toMatch(/An Expected error occurred/)
expect(text).toMatch(/error-in-the-browser-global-scope\.js \(2:9\)/)
expect(pageErrors).toEqual([
expect.objectContaining({ message: 'An Expected error occurred' }),
])
} finally {
if (browser) {
await browser.close()
......
......@@ -14,7 +14,14 @@ describe('app-dir - errors', () => {
describe('error component', () => {
it('should trigger error component when an error happens during rendering', async () => {
const browser = await next.browser('/client-component')
const pageErrors: unknown[] = []
const browser = await next.browser('/client-component', {
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
})
await browser.elementByCss('#error-trigger-button').click()
if (isNextDev) {
......@@ -27,10 +34,20 @@ describe('app-dir - errors', () => {
await browser.waitForElementByCss('#error-boundary-message').text()
).toBe('An error occurred: this is a test')
}
// Handled by custom error boundary.
expect(pageErrors).toEqual([])
})
it('should trigger error component when an error happens during server components rendering', async () => {
const browser = await next.browser('/server-component')
const pageErrors: unknown[] = []
const browser = await next.browser('/server-component', {
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
})
expect(
await browser.waitForElementByCss('#error-boundary-message').text()
......@@ -49,6 +66,9 @@ describe('app-dir - errors', () => {
// await assertHasRedbox(browser)
// expect(await getRedboxHeader(browser)).toMatch(/this is a test/)
}
// Handled by custom error boundary.
expect(pageErrors).toEqual([])
})
it('should preserve custom digests', async () => {
......@@ -143,7 +163,14 @@ describe('app-dir - errors', () => {
})
it('should use default error boundary for prod and overlay for dev when no error component specified', async () => {
const browser = await next.browser('/global-error-boundary/client')
const pageErrors: unknown[] = []
const browser = await next.browser('/global-error-boundary/client', {
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
})
await browser.elementByCss('#error-trigger-button').click()
if (isNextDev) {
......@@ -156,10 +183,20 @@ describe('app-dir - errors', () => {
'Application error: a client-side exception has occurred while loading localhost (see the browser console for more information).'
)
}
// FIXME(veil): Should contain thrown error.
expect(pageErrors).toEqual([])
})
it('should display error digest for error in server component with default error boundary', async () => {
const browser = await next.browser('/global-error-boundary/server')
const pageErrors: unknown[] = []
const browser = await next.browser('/global-error-boundary/server', {
beforePageLoad: (page) => {
page.on('pageerror', (error: unknown) => {
pageErrors.push(error)
})
},
})
if (isNextDev) {
await assertHasRedbox(browser)
......@@ -174,6 +211,9 @@ describe('app-dir - errors', () => {
await browser.waitForElementByCss('body').elementByCss('p').text()
).toMatch(/Digest: \w+/)
}
// FIXME(veil): Should contain thrown error.
expect(pageErrors).toEqual([])
})
// production tests
......
......@@ -3231,7 +3231,7 @@
"Client Navigation with 404 pages should should not contain a page script in a 404 page",
"Client Navigation with <Link/> should call mouse handlers with an absolute url",
"Client Navigation with <Link/> should error when calling onClick without event",
"Client Navigation with <Link/> should have proper error when no children are provided",
"Client Navigation with <Link/> should have a redbox when no children are provided",
"Client Navigation with <Link/> should navigate an absolute local url",
"Client Navigation with <Link/> should navigate an absolute local url with as",
"Client Navigation with <Link/> should navigate an absolute url",
......@@ -3261,7 +3261,7 @@
"Client Navigation with different types of urls should work with / page",
"Client Navigation with different types of urls should work with dir/ page",
"Client Navigation with different types of urls should work with normal page",
"Client Navigation with empty getInitialProps() should render an error",
"Client Navigation with empty getInitialProps() should render a redbox",
"Client Navigation with getInitialProp redirect should redirect the page via client side",
"Client Navigation with getInitialProp redirect should redirect the page when loading",
"Client Navigation with hash changes check hydration mis-match should not have hydration mis-match for hash link",
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