Nuxt/test/error.test.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-12-20 18:25:51 +00:00
import test from 'ava'
import { resolve } from 'path'
2017-12-07 08:12:22 +00:00
import rp from 'request-promise-native'
import { Nuxt, Builder } from '..'
import { interceptLog, interceptError, release } from './helpers/console'
2017-06-14 18:51:14 +00:00
2017-05-21 19:00:01 +00:00
const port = 4005
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-01-27 15:29:42 +00:00
let logSpy
2016-12-20 18:25:51 +00:00
// Init nuxt.js and create server listening on localhost:4000
test.serial('Init Nuxt.js', async t => {
2018-01-27 15:29:42 +00:00
const rootDir = resolve(__dirname, 'fixtures/error')
const config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
2017-06-20 11:44:47 +00:00
2018-01-27 15:29:42 +00:00
logSpy = await interceptLog(async () => {
nuxt = new Nuxt(config)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
t.true(logSpy.calledWithMatch('DONE'))
t.true(logSpy.calledWithMatch('OPEN'))
2016-12-20 18:25:51 +00:00
})
test.serial('/ should display an error', async t => {
const error = await t.throws(nuxt.renderRoute('/'))
t.true(error.message.includes('not_defined is not defined'))
2016-12-20 18:25:51 +00:00
})
test.serial('/404 should display an error too', async t => {
2017-04-13 09:32:29 +00:00
let { error } = await nuxt.renderRoute('/404')
2017-09-02 20:35:41 +00:00
t.true(error.message.includes('This page could not be found'))
2016-12-20 18:25:51 +00:00
})
test.serial('/ with renderAndGetWindow()', async t => {
const errorSpy = await interceptError()
2017-05-30 16:18:01 +00:00
const err = await t.throws(nuxt.renderAndGetWindow(url('/')))
2017-08-05 07:47:30 +00:00
t.is(err.response.statusCode, 500)
2017-08-05 19:20:26 +00:00
t.is(err.response.statusMessage, 'NuxtServerError')
release()
t.true(errorSpy.calledOnce)
2018-01-13 05:22:11 +00:00
t.true(
errorSpy
.getCall(0)
.args[0].message.includes(
'render function or template not defined in component: anonymous'
)
)
2016-12-20 18:25:51 +00:00
})
test.serial('/ with text/json content', async t => {
2017-12-07 08:12:22 +00:00
const opts = {
headers: {
2018-01-13 05:22:11 +00:00
accept: 'application/json'
2017-12-07 08:12:22 +00:00
},
resolveWithFullResponse: true
}
const errorSpy = await interceptError()
2017-12-07 08:12:22 +00:00
const { response: { headers } } = await t.throws(rp(url('/'), opts))
t.is(headers['content-type'], 'text/json; charset=utf-8')
release()
t.true(errorSpy.calledOnce)
2018-01-13 05:22:11 +00:00
t.true(
errorSpy
.getCall(0)
.args[0].message.includes(
'render function or template not defined in component: anonymous'
)
)
2017-12-07 08:12:22 +00:00
})
2018-01-27 15:29:42 +00:00
test.serial('Deprecated: dev in build.extend()', async t => {
t.true(logSpy.calledWith('[build:done]: hook error'))
})
2018-01-29 03:41:56 +00:00
test.serial('Error: resolvePath()', async t => {
let error = t.throws(() => nuxt.resolvePath())
t.true(error instanceof TypeError)
error = t.throws(() => nuxt.resolvePath('@/pages/about.vue'))
t.true(error.message.includes('Cannot resolve "@/pages/about.vue"'))
})
2016-12-20 18:25:51 +00:00
// Close server and ask nuxt to stop listening to file changes
test.after.always('Closing server and nuxt.js', async t => {
await nuxt.close()
2016-12-20 18:25:51 +00:00
})