2018-03-18 19:31:32 +00:00
|
|
|
// import rp from 'request-promise-native'
|
2018-04-18 14:36:23 +00:00
|
|
|
import consola from 'consola'
|
2018-03-27 22:28:17 +00:00
|
|
|
import { loadFixture, getPort, Nuxt } from '../utils'
|
2017-06-14 18:51:14 +00:00
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
let port
|
2018-01-13 05:22:11 +00:00
|
|
|
const url = route => 'http://localhost:' + port + route
|
2016-12-20 18:25:51 +00:00
|
|
|
|
|
|
|
let nuxt = null
|
2018-03-18 19:31:32 +00:00
|
|
|
// let logSpy
|
2016-12-20 18:25:51 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
describe('error', () => {
|
|
|
|
beforeAll(async () => {
|
2018-08-17 20:25:23 +00:00
|
|
|
const config = await loadFixture('error')
|
2018-01-27 15:29:42 +00:00
|
|
|
nuxt = new Nuxt(config)
|
2019-03-08 20:43:23 +00:00
|
|
|
await nuxt.ready()
|
|
|
|
|
2018-03-18 23:41:14 +00:00
|
|
|
port = await getPort()
|
2018-10-30 20:42:53 +00:00
|
|
|
await nuxt.server.listen(port, 'localhost')
|
2020-01-17 09:47:40 +00:00
|
|
|
|
|
|
|
consola.wrapConsole()
|
2018-03-18 23:41:14 +00:00
|
|
|
})
|
2017-12-17 19:30:26 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/ should display an error', async () => {
|
2019-09-01 14:58:56 +00:00
|
|
|
await expect(nuxt.server.renderRoute('/error')).rejects.toMatchObject({
|
2018-03-18 19:31:32 +00:00
|
|
|
message: expect.stringContaining('not_defined is not defined')
|
|
|
|
})
|
|
|
|
})
|
2016-12-20 18:25:51 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/404 should display an error too', async () => {
|
2018-10-30 20:42:53 +00:00
|
|
|
const { error } = await nuxt.server.renderRoute('/404')
|
2018-11-08 09:41:24 +00:00
|
|
|
expect(error.message).toContain('This page could not be found')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2016-12-20 18:25:51 +00:00
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
test('/ with renderAndGetWindow()', async () => {
|
2019-09-01 14:58:56 +00:00
|
|
|
await expect(nuxt.server.renderAndGetWindow(url('/error'))).rejects.toMatchObject({
|
2018-03-18 19:31:32 +00:00
|
|
|
statusCode: 500
|
|
|
|
})
|
2018-03-19 02:14:26 +00:00
|
|
|
})
|
2016-12-20 18:25:51 +00:00
|
|
|
|
2018-08-10 07:41:23 +00:00
|
|
|
test('Error: resolvePath()', () => {
|
2018-10-17 21:28:25 +00:00
|
|
|
expect(() => nuxt.resolver.resolvePath()).toThrowError()
|
2019-09-01 14:58:56 +00:00
|
|
|
expect(() => nuxt.resolver.resolvePath('@/pages/not-found.vue')).toThrowError('Cannot resolve "@/pages/not-found.vue"')
|
2018-03-18 19:31:32 +00:00
|
|
|
})
|
2018-01-29 03:41:56 +00:00
|
|
|
|
2018-04-18 14:36:23 +00:00
|
|
|
test('Error: callHook()', async () => {
|
2018-11-02 13:54:58 +00:00
|
|
|
consola.fatal.mockClear()
|
2018-08-16 15:46:42 +00:00
|
|
|
|
2018-04-18 14:36:23 +00:00
|
|
|
const errorHook = jest.fn()
|
|
|
|
const error = new Error('test hook error')
|
|
|
|
|
|
|
|
nuxt.hook('error', errorHook)
|
|
|
|
nuxt.hook('test:error', () => { throw error })
|
|
|
|
await nuxt.callHook('test:error')
|
|
|
|
|
|
|
|
expect(errorHook).toHaveBeenCalledTimes(1)
|
|
|
|
expect(errorHook).toHaveBeenCalledWith(error)
|
2018-11-02 13:54:58 +00:00
|
|
|
expect(consola.fatal).toHaveBeenCalledTimes(1)
|
|
|
|
expect(consola.fatal).toHaveBeenCalledWith(error)
|
2018-04-18 14:36:23 +00:00
|
|
|
})
|
|
|
|
|
2019-09-01 14:58:56 +00:00
|
|
|
test('/info should display an error', async () => {
|
|
|
|
await expect(nuxt.server.renderRoute('/info')).rejects.toMatchObject({
|
2019-11-26 22:42:39 +00:00
|
|
|
message: expect.stringContaining('Cannot read property \'title\' of undefined')
|
2019-09-01 14:58:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/about should work', async () => {
|
|
|
|
await expect(nuxt.server.renderRoute('/about')).resolves.toMatchObject({
|
|
|
|
html: expect.stringContaining('About')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
test('/error-square should display an error', async () => {
|
|
|
|
await expect(nuxt.server.renderRoute('/squared')).rejects.toMatchObject({
|
2019-11-26 22:42:39 +00:00
|
|
|
message: expect.stringContaining('Cannot read property \'data\' of undefined')
|
2019-09-01 14:58:56 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-03-18 19:31:32 +00:00
|
|
|
// Close server and ask nuxt to stop listening to file changes
|
2018-03-30 09:20:16 +00:00
|
|
|
afterAll(async () => {
|
2018-03-18 19:31:32 +00:00
|
|
|
await nuxt.close()
|
|
|
|
})
|
2016-12-20 18:25:51 +00:00
|
|
|
})
|