Skip to content

Commit 8971690

Browse files
committed
test: Add test for handling client error in _error
1 parent 37a1ff6 commit 8971690

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
3+
class Error extends React.Component {
4+
static async getInitialProps({ res, err }) {
5+
const statusCode = res ? res.statusCode : err ? err.statusCode : null
6+
return { statusCode }
7+
}
8+
9+
render() {
10+
if (typeof window !== 'undefined') {
11+
throw new Error('Error from Error page')
12+
}
13+
return (
14+
<p>
15+
{this.props.statusCode
16+
? `An error ${this.props.statusCode} occurred on server`
17+
: 'An error occurred on client'}
18+
</p>
19+
)
20+
}
21+
}
22+
23+
export default Error
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Link from 'next/link'
2+
3+
function Index() {
4+
return (
5+
<>
6+
<h3>Hi 👋</h3>
7+
<Link href="/a-non-existing-page">
8+
<a>a link to no-where</a>
9+
</Link>
10+
</>
11+
)
12+
}
13+
14+
Index.getInitialProps = () => {
15+
if (typeof window !== 'undefined') {
16+
throw new Error('Error from Index page')
17+
}
18+
return {}
19+
}
20+
21+
export default Index
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* eslint-env jest */
2+
3+
import path from 'path'
4+
import webdriver from 'next-webdriver'
5+
import {
6+
nextBuild,
7+
nextStart,
8+
findPort,
9+
killApp,
10+
waitFor,
11+
} from 'next-test-utils'
12+
13+
jest.setTimeout(1000 * 60 * 1)
14+
const appDir = path.join(__dirname, '..')
15+
let app
16+
let port
17+
18+
describe('Handles an Error in _error', () => {
19+
beforeAll(async () => {
20+
await nextBuild(appDir)
21+
port = await findPort()
22+
app = await nextStart(appDir, port)
23+
})
24+
afterAll(() => killApp(app))
25+
26+
it('Handles error during client transition', async () => {
27+
const browser = await webdriver(port, '/')
28+
await browser.waitForElementByCss('a').click()
29+
await waitFor(1000)
30+
const html = await browser.eval('document.body.innerHTML')
31+
expect(html).toMatch(/internal server error/i)
32+
})
33+
})

0 commit comments

Comments
 (0)