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 { resolve } from 'path'
import rp from 'request-promise-native'
import stdMocks from 'std-mocks'
import { Nuxt, Builder } from '../index.js'
import { interceptLog, interceptError } from './helpers/console'
const port = 4003
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 => {
@ -61,17 +63,19 @@ test('/store', async t => {
})
test('/head', async t => {
stdMocks.use()
const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false })
const html = window.document.body.innerHTML
const metas = window.document.getElementsByTagName('meta')
stdMocks.restore()
const { stdout } = stdMocks.flush()
t.is(stdout[0], 'Body script!\n')
t.is(window.document.title, 'My title - Nuxt.js')
t.is(metas[0].getAttribute('content'), 'my meta')
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
t.true(html.includes('<script data-n-head="true" src="/body.js" data-body="true">'))
const logSpy = await interceptLog(async () => {
const window = await nuxt.renderAndGetWindow(url('/head'), { virtualConsole: false })
t.is(window.document.title, 'My title - Nuxt.js')
const html = window.document.body.innerHTML
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>'))
t.true(html.includes('<script data-n-head="true" src="/body.js" data-body="true">'))
const metas = window.document.getElementsByTagName('meta')
t.is(metas[0].getAttribute('content'), 'my meta')
})
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Body script!')
})
test('/async-data', async t => {
@ -124,20 +128,18 @@ test('/special-state -> check window.__NUXT__.test = true', async t => {
})
test('/error', async t => {
try {
await nuxt.renderRoute('/error', { req: {}, res: {} })
} catch (err) {
t.true(err.message.includes('Error mouahahah'))
}
const err = await t.throws(nuxt.renderRoute('/error', { req: {}, res: {} }))
t.true(err.message.includes('Error mouahahah'))
})
test('/error status code', async t => {
try {
await rp(url('/error'))
} catch (err) {
const errorSpy = await interceptError(async () => {
const err = await t.throws(rp(url('/error')))
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(errorSpy.calledOnce)
t.true(errorSpy.args[0][0].message.includes('Error mouahahah'))
})
test('/error2', async t => {
@ -157,26 +159,22 @@ test('/error2 status code', async t => {
})
test('/error-midd', async t => {
stdMocks.use()
try {
await rp(url('/error-midd'))
} catch (err) {
stdMocks.restore()
const errorSpy = await interceptError(async () => {
const err = await t.throws(rp(url('/error-midd')))
t.is(err.statusCode, 505)
t.true(err.response.body.includes('Middleware Error'))
const output = stdMocks.flush()
// Don't display error since redirect returns a noopApp
t.true(output.stderr.length === 0)
}
})
// Don't display error since redirect returns a noopApp
t.true(errorSpy.notCalled)
})
test('/redirect2', async t => {
stdMocks.use()
await rp(url('/redirect2')) // Should not console.error
stdMocks.restore()
const output = stdMocks.flush()
const errorSpy = await interceptError(async () => {
await rp(url('/redirect2')) // Should not console.error
})
// Don't display error since redirect returns a noopApp
t.true(output.stderr.length === 0)
t.true(errorSpy.notCalled)
})
test('/no-ssr', async t => {
@ -191,12 +189,16 @@ test('/no-ssr (client-side)', async t => {
})
test('ETag Header', async t => {
const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true })
// Validate etag
t.regex(etag, /W\/".*"$/)
// Verify functionality
const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } }))
t.is(error.statusCode, 304)
const errorSpy = await interceptError(async () => {
const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true })
// Validate etag
t.regex(etag, /W\/".*"$/)
// Verify functionality
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 => {
@ -227,6 +229,6 @@ test('/fn-midd?please=true', async t => {
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
nuxt.close()
test.after('Closing server and nuxt.js', async t => {
await nuxt.close()
})

View File

@ -1,9 +1,12 @@
import sinon from 'sinon'
const getContext = t => { return t.context ? t.context : t }
let context = null
export function release(t) {
const context = getContext(t)
export function release() {
if (context === null) {
process.stderr.write('Console spy context was empty, did a previous test already release it?\n')
return
}
if (context.log) {
console.log = context.log // eslint-disable-line no-console
@ -17,47 +20,52 @@ export function release(t) {
if (context.error) {
console.error = context.error // eslint-disable-line no-console
}
context = null
}
export async function intercept(t, types, msg, cb) {
const context = getContext(t)
export async function intercept(levels, msg, cb) {
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') {
cb = msg
msg = undefined
if (typeof types === 'string') {
msg = types
types = undefined
if (typeof levels === 'string') {
msg = levels
levels = undefined
}
}
if (cb === undefined && msg === undefined && typeof types === 'function') {
cb = types
types = undefined
if (cb === undefined && msg === undefined && typeof levels === 'function') {
cb = levels
levels = undefined
}
const all = types === undefined || types === {}
const { log, info, warn, error } = types || {}
const all = levels === undefined || levels === {}
const spies = {}
if (log || all) {
if (all || levels.log) {
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
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
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
console.error = sinon.spy() // eslint-disable-line no-console
spies.error = console.error = sinon.spy() // eslint-disable-line no-console
}
if (cb) {
@ -71,22 +79,28 @@ export async function intercept(t, types, msg, cb) {
process.stdout.write('\n')
}
release(context)
release()
}
return spies
}
export async function interceptLog(t, msg, cb) {
await intercept(t, { log: true }, msg, cb)
export async function interceptLog(msg, cb) {
const { log } = await intercept({ log: true }, msg, cb)
return log
}
export async function interceptInfo(t, msg, cb) {
await intercept(t, { info: true }, msg, cb)
export async function interceptInfo(msg, cb) {
const { info } = await intercept({ info: true }, msg, cb)
return info
}
export async function interceptWarn(t, msg, cb) {
await intercept(t, { warn: true }, msg, cb)
export async function interceptWarn(msg, cb) {
const { warn } = await intercept({ warn: true }, msg, cb)
return warn
}
export async function interceptError(t, msg, cb) {
await intercept(t, { error: true }, msg, cb)
export async function interceptError(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 rp from 'request-promise-native'
import { Nuxt, Builder } from '../index.js'
import * as testconsole from './helpers/console'
import { interceptLog, release } from './helpers/console'
const port = 4007
const url = (route) => 'http://localhost:' + port + route
@ -17,17 +17,19 @@ test.before('Init Nuxt.js', async t => {
config.dev = false
nuxt = new Nuxt(config)
await testconsole.interceptLog(t, 'building nuxt', async () => {
await interceptLog('building nuxt', async () => {
await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost')
})
})
test('/', async t => {
testconsole.interceptLog(t)
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom configurations</h1>'))
testconsole.release(t)
const logSpy = await interceptLog(async () => {
const { html } = await nuxt.renderRoute('/')
t.true(html.includes('<h1>I have custom configurations</h1>'))
})
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
})
test('/ (global styles inlined)', async t => {
@ -56,22 +58,25 @@ test('/ (custom postcss.config.js)', async t => {
})
test('/test/ (router base)', async t => {
testconsole.interceptLog(t)
const window = await nuxt.renderAndGetWindow(url('/test/'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
const logSpy = await interceptLog(async () => {
const window = await nuxt.renderAndGetWindow(url('/test/'))
const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'default')
t.true(html.includes('<h1>Default layout</h1>'))
t.true(html.includes('<h1>I have custom configurations</h1>'))
const html = window.document.body.innerHTML
t.is(window.__NUXT__.layout, 'default')
t.true(html.includes('<h1>Default layout</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 => {
testconsole.interceptLog(t)
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/about'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML
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 => {
testconsole.interceptLog(t)
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/desktop'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML
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 => {
testconsole.interceptLog(t)
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/mobile'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML
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 => {
testconsole.interceptLog(t)
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/env'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML
t.true(html.includes('<h1>Custom env layout</h1>'))
@ -121,30 +129,33 @@ test('/test/env', async t => {
})
test('/test/error', async t => {
testconsole.interceptLog(t)
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/error'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML
t.true(html.includes('Error page'))
})
test('/test/user-agent', async t => {
testconsole.interceptLog(t)
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML
t.true(html.includes('<pre>Mozilla'))
})
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'))
t.true(console.log.calledOnce) // eslint-disable-line no-console
testconsole.release(t)
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
release()
const html = window.document.body.innerHTML
t.true(html.includes('<h1>Custom layout</h1>'))