From 54dec8e4b579d7f789e2b5a2939036b851f27ffd Mon Sep 17 00:00:00 2001 From: Sebastien Chopin Date: Tue, 16 May 2017 15:12:30 +0200 Subject: [PATCH] Fix hot-update.json & add tests --- lib/render.js | 6 ++---- test/basic.dev.test.js | 10 ++++++++++ test/basic.test.js | 13 ++++++++++++- test/fixtures/basic/middleware/redirect.js | 3 +++ test/fixtures/basic/pages/redirect2.vue | 10 ++++++++++ 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/basic/middleware/redirect.js create mode 100644 test/fixtures/basic/pages/redirect2.vue diff --git a/lib/render.js b/lib/render.js index 9c79d15c37..020f1cba45 100644 --- a/lib/render.js +++ b/lib/render.js @@ -49,12 +49,11 @@ export async function render (req, res) { /* istanbul ignore next */ req.url = url } - /* istanbul ignore next */ if (this.dev && req.url.indexOf(self.options.build.publicPath) === 0 && req.url.includes('.hot-update.json')) { 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) { return html } @@ -66,7 +65,6 @@ export async function render (req, res) { res.end(html, 'utf8') return html } catch (err) { - /* istanbul ignore next */ if (context.redirected) { console.error(err) // eslint-disable-line no-console return err diff --git a/test/basic.dev.test.js b/test/basic.dev.test.js index 6a8ebf38f7..ae9e528f2c 100644 --- a/test/basic.dev.test.js +++ b/test/basic.dev.test.js @@ -1,5 +1,6 @@ import test from 'ava' import { resolve } from 'path' +import rp from 'request-promise-native' const port = 4005 const url = (route) => 'http://localhost:' + port + route @@ -25,6 +26,15 @@ test('/stateless', async t => { t.true(html.includes('

My component!

')) }) +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 test.after('Closing server and nuxt.js', t => { server.close() diff --git a/test/basic.test.js b/test/basic.test.js index 27fe5a5922..6dfe898c03 100755 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -1,6 +1,8 @@ import test from 'ava' import { resolve } from 'path' import rp from 'request-promise-native' +import stdMocks from 'std-mocks' + const port = 4000 const url = (route) => 'http://localhost:' + port + route @@ -128,11 +130,20 @@ test('/error2 status code', async t => { try { await rp(url('/error2')) } catch (err) { - t.true(err.statusCode === 500) + t.is(err.statusCode, 500) 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 test.after('Closing server and nuxt.js', t => { server.close() diff --git a/test/fixtures/basic/middleware/redirect.js b/test/fixtures/basic/middleware/redirect.js new file mode 100644 index 0000000000..1a0784ed66 --- /dev/null +++ b/test/fixtures/basic/middleware/redirect.js @@ -0,0 +1,3 @@ +export default function ({ redirect }) { + redirect('/') +} diff --git a/test/fixtures/basic/pages/redirect2.vue b/test/fixtures/basic/pages/redirect2.vue new file mode 100644 index 0000000000..1b44bef173 --- /dev/null +++ b/test/fixtures/basic/pages/redirect2.vue @@ -0,0 +1,10 @@ + + +