Add error tests

This commit is contained in:
Sébastien Chopin 2016-12-20 19:25:51 +01:00
parent 0ea0bf8d87
commit 095c8efe56
7 changed files with 107 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import test from 'ava'
import { resolve } from 'path'
const port = 4002
const port = 4000
const url = (route) => 'http://localhost:' + port + route
let nuxt = null
@ -67,6 +67,23 @@ test('/validate?valid=true', async t => {
t.true(html.includes('<h1>I am valid</h1>'))
})
test('/redirect', async t => {
const { html } = await nuxt.renderRoute('/redirect')
t.true(html.includes('<div id="__nuxt"></div>'))
})
test('/redirect -> check redirected source', async t => {
const window = await nuxt.renderAndGetWindow(url('/redirect'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>Index page</h1>'))
})
test('/error', async t => {
const { html, error } = await nuxt.renderRoute('/error')
t.true(html.includes('Error mouahahah'))
t.true(error.message.includes('Error mouahahah'))
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
server.close()

View File

@ -0,0 +1,11 @@
<template>
<h1>Error page</h1>
</template>
<script>
export default {
data () {
throw new Error('Error mouahahah')
}
}
</script>

View File

@ -0,0 +1,3 @@
<template>
<h1>Index page</h1>
</template>

View File

@ -0,0 +1,11 @@
<template>
<div></div>
</template>
<script>
export default {
fetch ({ redirect }) {
return redirect('/')
}
}
</script>

View File

@ -1,6 +1,6 @@
import test from 'ava'
import { resolve } from 'path'
const port = 4003
const port = 4001
// const url = (route) => 'http://localhost:' + port + route
let nuxt = null

53
test/error.test.js Normal file
View File

@ -0,0 +1,53 @@
import test from 'ava'
import { resolve } from 'path'
const port = 4002
const url = (route) => 'http://localhost:' + port + route
let nuxt = null
let server = null
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', t => {
const Nuxt = require('../')
const options = {
rootDir: resolve(__dirname, 'error'),
dev: false
}
nuxt = new Nuxt(options)
return nuxt.build()
.then(function () {
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')
})
})
test('/ should display an error', async t => {
try {
await nuxt.renderRoute('/')
} catch (e) {
t.true(e.message.includes('not_defined is not defined'))
}
})
test('/404 should display an error too', async t => {
try {
await nuxt.renderRoute('/404')
} catch (e) {
t.true(e.message.includes('not_defined is not defined'))
}
})
test('/ with renderAndGetWindow()', async t => {
try {
await nuxt.renderAndGetWindow(url('/'))
} catch (e) {
t.true(e.message.includes('Could not load the nuxt app'))
t.true(e.body.includes('not_defined is not defined'))
}
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
server.close()
nuxt.close()
})

View File

@ -0,0 +1,10 @@
<template>
<h1>Error page</h1>
</template>
<script>
/* eslint no-undef: 0 */
export default {
not_defined
}
</script>