Nuxt/test/dev/error.test.js
2023-10-16 22:23:38 +01:00

87 lines
2.6 KiB
JavaScript

// import rp from 'request-promise-native'
import consola from 'consola'
import { loadFixture, getPort, Nuxt } from '../utils'
let port
const url = route => 'http://127.0.0.1:' + port + route
let nuxt = null
// let logSpy
describe('error', () => {
beforeAll(async () => {
const config = await loadFixture('error')
nuxt = new Nuxt(config)
await nuxt.ready()
port = await getPort()
await nuxt.server.listen(port, '127.0.0.1')
consola.wrapConsole()
})
test('/ should display an error', async () => {
await expect(nuxt.server.renderRoute('/error')).rejects.toMatchObject({
message: expect.stringContaining('notDefined is not defined')
})
})
test('/404 should display an error too', async () => {
const { error } = await nuxt.server.renderRoute('/404')
expect(error.message).toContain('This page could not be found')
})
test('/ with renderAndGetWindow()', async () => {
await expect(nuxt.server.renderAndGetWindow(url('/error')))
.rejects.toThrow('Resource was not loaded. Status: 500')
})
test('Error: resolvePath()', () => {
expect(() => nuxt.resolver.resolvePath()).toThrowError()
expect(() => nuxt.resolver.resolvePath('@/pages/not-found.vue')).toThrowError('Cannot resolve "@/pages/not-found.vue"')
})
test('Error: callHook()', async () => {
consola.fatal.mockClear()
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)
expect(consola.fatal).toHaveBeenCalledTimes(1)
expect(consola.fatal).toHaveBeenCalledWith(error)
})
test('/info should display an error', async () => {
const promise = nuxt.server.renderRoute('/info')
await expect(promise).rejects
const message = await promise.catch(e => e.message)
expect(message).toContain('Cannot read proper')
expect(message).toContain('title')
})
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 () => {
const promise = nuxt.server.renderRoute('/squared')
await expect(promise).rejects
const message = await promise.catch(e => e.message)
expect(message).toContain('Cannot read proper')
expect(message).toContain('data')
})
// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
})
})