Fix hot-update.json & add tests

This commit is contained in:
Sebastien Chopin 2017-05-16 15:12:30 +02:00
parent 51618bef29
commit 54dec8e4b5
5 changed files with 37 additions and 5 deletions

View File

@ -49,10 +49,9 @@ export async function render (req, res) {
/* istanbul ignore next */ /* istanbul ignore next */
req.url = url req.url = url
} }
/* istanbul ignore next */
if (this.dev && req.url.indexOf(self.options.build.publicPath) === 0 && req.url.includes('.hot-update.json')) { if (this.dev && req.url.indexOf(self.options.build.publicPath) === 0 && req.url.includes('.hot-update.json')) {
res.statusCode = 404 res.statusCode = 404
return {html: ''} return res.end()
} }
const { html, error, redirected } = await this.renderRoute(req.url, context) const { html, error, redirected } = await this.renderRoute(req.url, context)
if (redirected) { if (redirected) {
@ -66,7 +65,6 @@ export async function render (req, res) {
res.end(html, 'utf8') res.end(html, 'utf8')
return html return html
} catch (err) { } catch (err) {
/* istanbul ignore next */
if (context.redirected) { if (context.redirected) {
console.error(err) // eslint-disable-line no-console console.error(err) // eslint-disable-line no-console
return err return err

View File

@ -1,5 +1,6 @@
import test from 'ava' import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import rp from 'request-promise-native'
const port = 4005 const port = 4005
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -25,6 +26,15 @@ test('/stateless', async t => {
t.true(html.includes('<h1>My component!</h1>')) t.true(html.includes('<h1>My component!</h1>'))
}) })
test('/_nuxt/test.hot-update.json should returns empty html', async t => {
try {
await rp(url('/_nuxt/test.hot-update.json'))
} catch (err) {
t.is(err.statusCode, 404)
t.is(err.response.body, '')
}
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after('Closing server and nuxt.js', t => {
server.close() server.close()

View File

@ -1,6 +1,8 @@
import test from 'ava' import test from 'ava'
import { resolve } from 'path' import { resolve } from 'path'
import rp from 'request-promise-native' import rp from 'request-promise-native'
import stdMocks from 'std-mocks'
const port = 4000 const port = 4000
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -128,11 +130,20 @@ test('/error2 status code', async t => {
try { try {
await rp(url('/error2')) await rp(url('/error2'))
} catch (err) { } catch (err) {
t.true(err.statusCode === 500) t.is(err.statusCode, 500)
t.true(err.response.body.includes('Custom error')) t.true(err.response.body.includes('Custom error'))
} }
}) })
test('/redirect2 status code', async t => {
stdMocks.use()
await rp(url('/redirect2')) // Should console.error
stdMocks.restore()
const output = stdMocks.flush()
t.true(output.stderr.length >= 1)
t.true(output.stderr[0].includes('Error: NOPE!'))
})
// Close server and ask nuxt to stop listening to file changes // Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => { test.after('Closing server and nuxt.js', t => {
server.close() server.close()

View File

@ -0,0 +1,3 @@
export default function ({ redirect }) {
redirect('/')
}

10
test/fixtures/basic/pages/redirect2.vue vendored Normal file
View File

@ -0,0 +1,10 @@
<template></template>
<script>
export default {
middleware: 'redirect',
created () {
throw new Error('NOPE!')
}
}
</script>