Refine console intercept implementation

Adapt basic.ssr
This commit is contained in:
pimlie 2017-11-27 23:35:42 +01:00
parent 87bb0dea64
commit 194bcdd21f
3 changed files with 138 additions and 111 deletions

View File

@ -1,8 +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'
import { Nuxt, Builder } from '../index.js' import { Nuxt, Builder } from '../index.js'
import { interceptLog, interceptError } from './helpers/console'
const port = 4003 const port = 4003
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -20,10 +20,12 @@ test.before('Init Nuxt.js', async t => {
} }
} }
} }
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') await interceptLog('building nuxt', async () => {
nuxt = new Nuxt(options)
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
}) })
test('/stateless', async t => { test('/stateless', async t => {
@ -61,17 +63,19 @@ test('/store', async t => {
}) })
test('/head', async t => { test('/head', async t => {
stdMocks.use() const logSpy = await interceptLog(async () => {
const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false }) const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false })
const html = window.document.body.innerHTML t.is(window.document.title, 'My title - Nuxt.js')
const metas = window.document.getElementsByTagName('meta')
stdMocks.restore() const html = window.document.body.innerHTML
const { stdout } = stdMocks.flush() t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
t.is(stdout[0], 'Body script!\n') t.true(html.includes('<script data-n-head="true" src="/body.js" data-body="true">'))
t.is(window.document.title, 'My title - Nuxt.js')
t.is(metas[0].getAttribute('content'), 'my meta') const metas = window.document.getElementsByTagName('meta')
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>')) t.is(metas[0].getAttribute('content'), 'my meta')
t.true(html.includes('<script data-n-head="true" src="/body.js" data-body="true">')) })
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Body script!')
}) })
test('/async-data', async t => { test('/async-data', async t => {
@ -124,20 +128,18 @@ test('/special-state -> check window.__NUXT__.test = true', async t => {
}) })
test('/error', async t => { test('/error', async t => {
try { const err = await t.throws(nuxt.renderRoute('/error', { req: {}, res: {} }))
await nuxt.renderRoute('/error', { req: {}, res: {} }) t.true(err.message.includes('Error mouahahah'))
} catch (err) {
t.true(err.message.includes('Error mouahahah'))
}
}) })
test('/error status code', async t => { test('/error status code', async t => {
try { const errorSpy = await interceptError(async () => {
await rp(url('/error')) const err = await t.throws(rp(url('/error')))
} catch (err) {
t.true(err.statusCode === 500) t.true(err.statusCode === 500)
t.true(err.response.body.includes('An error occurred in the application and your page could not be served')) t.true(err.response.body.includes('An error occurred in the application and your page could not be served'))
} })
t.true(errorSpy.calledOnce)
t.true(errorSpy.args[0][0].message.includes('Error mouahahah'))
}) })
test('/error2', async t => { test('/error2', async t => {
@ -157,26 +159,22 @@ test('/error2 status code', async t => {
}) })
test('/error-midd', async t => { test('/error-midd', async t => {
stdMocks.use() const errorSpy = await interceptError(async () => {
try { const err = await t.throws(rp(url('/error-midd')))
await rp(url('/error-midd'))
} catch (err) {
stdMocks.restore()
t.is(err.statusCode, 505) t.is(err.statusCode, 505)
t.true(err.response.body.includes('Middleware Error')) t.true(err.response.body.includes('Middleware Error'))
const output = stdMocks.flush() })
// Don't display error since redirect returns a noopApp // Don't display error since redirect returns a noopApp
t.true(output.stderr.length === 0) t.true(errorSpy.notCalled)
}
}) })
test('/redirect2', async t => { test('/redirect2', async t => {
stdMocks.use() const errorSpy = await interceptError(async () => {
await rp(url('/redirect2')) // Should not console.error await rp(url('/redirect2')) // Should not console.error
stdMocks.restore() })
const output = stdMocks.flush()
// Don't display error since redirect returns a noopApp // Don't display error since redirect returns a noopApp
t.true(output.stderr.length === 0) t.true(errorSpy.notCalled)
}) })
test('/no-ssr', async t => { test('/no-ssr', async t => {
@ -191,12 +189,16 @@ test('/no-ssr (client-side)', async t => {
}) })
test('ETag Header', async t => { test('ETag Header', async t => {
const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true }) const errorSpy = await interceptError(async () => {
// Validate etag const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true })
t.regex(etag, /W\/".*"$/) // Validate etag
// Verify functionality t.regex(etag, /W\/".*"$/)
const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } })) // Verify functionality
t.is(error.statusCode, 304) const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } }))
t.is(error.statusCode, 304)
})
t.true(errorSpy.calledOnce)
t.true(errorSpy.args[0][0].includes('TypeError: Cannot read property \'split\' of undefined'))
}) })
test('/_nuxt/server-bundle.json should return 404', async t => { test('/_nuxt/server-bundle.json should return 404', async t => {
@ -227,6 +229,6 @@ test('/fn-midd?please=true', async t => {
}) })
// 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', async t => {
nuxt.close() await nuxt.close()
}) })

View File

@ -1,9 +1,12 @@
import sinon from 'sinon' import sinon from 'sinon'
const getContext = t => { return t.context ? t.context : t } let context = null
export function release(t) { export function release() {
const context = getContext(t) if (context === null) {
process.stderr.write('Console spy context was empty, did a previous test already release it?\n')
return
}
if (context.log) { if (context.log) {
console.log = context.log // eslint-disable-line no-console console.log = context.log // eslint-disable-line no-console
@ -17,47 +20,52 @@ export function release(t) {
if (context.error) { if (context.error) {
console.error = context.error // eslint-disable-line no-console console.error = context.error // eslint-disable-line no-console
} }
context = null
} }
export async function intercept(t, types, msg, cb) { export async function intercept(levels, msg, cb) {
const context = getContext(t) if (context !== null) {
process.stderr.write('Console spy context was not empty, did a previous test not release it?\n')
}
context = {}
if (cb === undefined && typeof msg === 'function') { if (cb === undefined && typeof msg === 'function') {
cb = msg cb = msg
msg = undefined msg = undefined
if (typeof types === 'string') { if (typeof levels === 'string') {
msg = types msg = levels
types = undefined levels = undefined
} }
} }
if (cb === undefined && msg === undefined && typeof types === 'function') { if (cb === undefined && msg === undefined && typeof levels === 'function') {
cb = types cb = levels
types = undefined levels = undefined
} }
const all = types === undefined || types === {} const all = levels === undefined || levels === {}
const { log, info, warn, error } = types || {} const spies = {}
if (log || all) { if (all || levels.log) {
context.log = console.log // eslint-disable-line no-console context.log = console.log // eslint-disable-line no-console
console.log = sinon.spy() // eslint-disable-line no-console spies.log = console.log = sinon.spy() // eslint-disable-line no-console
} }
if (info || all) { if (all || levels.info) {
context.info = console.info // eslint-disable-line no-console context.info = console.info // eslint-disable-line no-console
console.info = sinon.spy() // eslint-disable-line no-console spies.info = console.info = sinon.spy() // eslint-disable-line no-console
} }
if (warn || all) { if (all || levels.warn) {
context.warn = console.warn // eslint-disable-line no-console context.warn = console.warn // eslint-disable-line no-console
console.warn = sinon.spy() // eslint-disable-line no-console spies.warn = console.warn = sinon.spy() // eslint-disable-line no-console
} }
if (error || all) { if (all || levels.error) {
context.error = console.error // eslint-disable-line no-console context.error = console.error // eslint-disable-line no-console
console.error = sinon.spy() // eslint-disable-line no-console spies.error = console.error = sinon.spy() // eslint-disable-line no-console
} }
if (cb) { if (cb) {
@ -71,22 +79,28 @@ export async function intercept(t, types, msg, cb) {
process.stdout.write('\n') process.stdout.write('\n')
} }
release(context) release()
} }
return spies
} }
export async function interceptLog(t, msg, cb) { export async function interceptLog(msg, cb) {
await intercept(t, { log: true }, msg, cb) const { log } = await intercept({ log: true }, msg, cb)
return log
} }
export async function interceptInfo(t, msg, cb) { export async function interceptInfo(msg, cb) {
await intercept(t, { info: true }, msg, cb) const { info } = await intercept({ info: true }, msg, cb)
return info
} }
export async function interceptWarn(t, msg, cb) { export async function interceptWarn(msg, cb) {
await intercept(t, { warn: true }, msg, cb) const { warn } = await intercept({ warn: true }, msg, cb)
return warn
} }
export async function interceptError(t, msg, cb) { export async function interceptError(msg, cb) {
await intercept(t, { error: true }, msg, cb) const { error } = await intercept({ error: true }, msg, cb)
return error
} }

View File

@ -2,7 +2,7 @@ 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 { Nuxt, Builder } from '../index.js' import { Nuxt, Builder } from '../index.js'
import * as testconsole from './helpers/console' import { interceptLog, release } from './helpers/console'
const port = 4007 const port = 4007
const url = (route) => 'http://localhost:' + port + route const url = (route) => 'http://localhost:' + port + route
@ -17,17 +17,19 @@ test.before('Init Nuxt.js', async t => {
config.dev = false config.dev = false
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
await testconsole.interceptLog(t, 'building nuxt', async () => { await interceptLog('building nuxt', async () => {
await new Builder(nuxt).build() await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}) })
}) })
test('/', async t => { test('/', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog(async () => {
const { html } = await nuxt.renderRoute('/') const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom configurations</h1>')) t.true(html.includes('<h1>I have custom configurations</h1>'))
testconsole.release(t) })
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
}) })
test('/ (global styles inlined)', async t => { test('/ (global styles inlined)', async t => {
@ -56,22 +58,25 @@ test('/ (custom postcss.config.js)', async t => {
}) })
test('/test/ (router base)', async t => { test('/test/ (router base)', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog(async () => {
const window = await nuxt.renderAndGetWindow(url('/test/')) const window = await nuxt.renderAndGetWindow(url('/test/'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'default') t.is(window.__NUXT__.layout, 'default')
t.true(html.includes('<h1>Default layout</h1>')) t.true(html.includes('<h1>Default layout</h1>'))
t.true(html.includes('<h1>I have custom configurations</h1>')) t.true(html.includes('<h1>I have custom configurations</h1>'))
})
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
}) })
test('/test/about (custom layout)', async t => { test('/test/about (custom layout)', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/about')) const window = await nuxt.renderAndGetWindow(url('/test/about'))
t.true(console.log.calledOnce) // eslint-disable-line no-console t.true(logSpy.calledOnce)
testconsole.release(t) t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'custom') t.is(window.__NUXT__.layout, 'custom')
@ -80,10 +85,11 @@ test('/test/about (custom layout)', async t => {
}) })
test('/test/desktop (custom layout in desktop folder)', async t => { test('/test/desktop (custom layout in desktop folder)', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/desktop')) const window = await nuxt.renderAndGetWindow(url('/test/desktop'))
t.true(console.log.calledOnce) // eslint-disable-line no-console t.true(logSpy.calledOnce)
testconsole.release(t) t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'desktop/default') t.is(window.__NUXT__.layout, 'desktop/default')
@ -92,10 +98,11 @@ test('/test/desktop (custom layout in desktop folder)', async t => {
}) })
test('/test/mobile (custom layout in mobile folder)', async t => { test('/test/mobile (custom layout in mobile folder)', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/mobile')) const window = await nuxt.renderAndGetWindow(url('/test/mobile'))
t.true(console.log.calledOnce) // eslint-disable-line no-console t.true(logSpy.calledOnce)
testconsole.release(t) t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'mobile/default') t.is(window.__NUXT__.layout, 'mobile/default')
@ -104,10 +111,11 @@ test('/test/mobile (custom layout in mobile folder)', async t => {
}) })
test('/test/env', async t => { test('/test/env', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/env')) const window = await nuxt.renderAndGetWindow(url('/test/env'))
t.true(console.log.calledOnce) // eslint-disable-line no-console t.true(logSpy.calledOnce)
testconsole.release(t) t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<h1>Custom env layout</h1>')) t.true(html.includes('<h1>Custom env layout</h1>'))
@ -121,30 +129,33 @@ test('/test/env', async t => {
}) })
test('/test/error', async t => { test('/test/error', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/error')) const window = await nuxt.renderAndGetWindow(url('/test/error'))
t.true(console.log.calledOnce) // eslint-disable-line no-console t.true(logSpy.calledOnce)
testconsole.release(t) t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('Error page')) t.true(html.includes('Error page'))
}) })
test('/test/user-agent', async t => { test('/test/user-agent', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/user-agent')) const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
t.true(console.log.calledOnce) // eslint-disable-line no-console t.true(logSpy.calledOnce)
testconsole.release(t) t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<pre>Mozilla')) t.true(html.includes('<pre>Mozilla'))
}) })
test('/test/about-bis (added with extendRoutes)', async t => { test('/test/about-bis (added with extendRoutes)', async t => {
testconsole.interceptLog(t) const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/about-bis')) const window = await nuxt.renderAndGetWindow(url('/test/about-bis'))
t.true(console.log.calledOnce) // eslint-disable-line no-console t.true(logSpy.calledOnce)
testconsole.release(t) t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML const html = window.document.body.innerHTML
t.true(html.includes('<h1>Custom layout</h1>')) t.true(html.includes('<h1>Custom layout</h1>'))