Merge pull request #2234 from pimlie/feat-cleanup-test-console-output

Intercept console output in tests
This commit is contained in:
Sébastien Chopin 2017-12-12 17:36:59 +01:00 committed by GitHub
commit 6fc57895b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 213 additions and 56 deletions

View File

@ -140,6 +140,7 @@
"puppeteer": "^0.13.0", "puppeteer": "^0.13.0",
"request": "^2.83.0", "request": "^2.83.0",
"request-promise-native": "^1.0.5", "request-promise-native": "^1.0.5",
"sinon": "^4.1.2",
"std-mocks": "^1.0.1", "std-mocks": "^1.0.1",
"uglify-js": "^3.2.2" "uglify-js": "^3.2.2"
}, },

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 '..' import { Nuxt, Builder } from '..'
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 => {
} }
} }
} }
await interceptLog('building nuxt', async () => {
nuxt = new Nuxt(options) nuxt = new Nuxt(options)
await new Builder(nuxt).build() await new Builder(nuxt).build()
await nuxt.listen(port, 'localhost') 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
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(window.document.title, 'My title - Nuxt.js')
t.is(metas[0].getAttribute('content'), 'my meta')
const html = window.document.body.innerHTML
t.true(html.includes('<div><h1>I can haz meta tags</h1></div>')) 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">')) 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 => { test('/async-data', async t => {
@ -137,20 +141,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: {} })
} catch (err) {
t.true(err.message.includes('Error mouahahah')) 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 => {
@ -170,26 +172,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 => {
@ -204,12 +202,16 @@ test('/no-ssr (client-side)', async t => {
}) })
test('ETag Header', async t => { test('ETag Header', async t => {
const errorSpy = await interceptError(async () => {
const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true }) const { headers: { etag } } = await rp(url('/stateless'), { resolveWithFullResponse: true })
// Validate etag // Validate etag
t.regex(etag, /W\/".*"$/) t.regex(etag, /W\/".*"$/)
// Verify functionality // Verify functionality
const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } })) const error = await t.throws(rp(url('/stateless'), { headers: { 'If-None-Match': etag } }))
t.is(error.statusCode, 304) 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 => {
@ -256,6 +258,6 @@ test('/js-link', 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()
}) })

106
test/helpers/console.js Normal file
View File

@ -0,0 +1,106 @@
import sinon from 'sinon'
let context = null
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
}
if (context.info) {
console.info = context.info // eslint-disable-line no-console
}
if (context.warn) {
console.warn = context.warn // eslint-disable-line no-console
}
if (context.error) {
console.error = context.error // eslint-disable-line no-console
}
context = null
}
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 levels === 'string') {
msg = levels
levels = undefined
}
}
if (cb === undefined && msg === undefined && typeof levels === 'function') {
cb = levels
levels = undefined
}
const all = levels === undefined || levels === {}
const spies = {}
if (all || levels.log) {
context.log = console.log // eslint-disable-line no-console
spies.log = console.log = sinon.spy() // eslint-disable-line no-console
}
if (all || levels.info) {
context.info = console.info // eslint-disable-line no-console
spies.info = console.info = sinon.spy() // eslint-disable-line no-console
}
if (all || levels.warn) {
context.warn = console.warn // eslint-disable-line no-console
spies.warn = console.warn = sinon.spy() // eslint-disable-line no-console
}
if (all || levels.error) {
context.error = console.error // eslint-disable-line no-console
spies.error = console.error = sinon.spy() // eslint-disable-line no-console
}
if (cb) {
if (msg) {
process.stdout.write(` ${msg}`)
}
await cb()
if (msg) {
process.stdout.write('\n')
}
release()
}
return spies
}
export async function interceptLog(msg, cb) {
const { log } = await intercept({ log: true }, msg, cb)
return log
}
export async function interceptInfo(msg, cb) {
const { info } = await intercept({ info: true }, msg, cb)
return info
}
export async function interceptWarn(msg, cb) {
const { warn } = await intercept({ warn: true }, msg, cb)
return warn
}
export async function interceptError(msg, cb) {
const { error } = await intercept({ error: true }, msg, cb)
return error
}

View File

@ -2,6 +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 '..' import { Nuxt, Builder } from '..'
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,13 +18,19 @@ test.before('Init Nuxt.js', async t => {
config.dev = false config.dev = false
nuxt = new Nuxt(config) nuxt = new Nuxt(config)
builder = new Builder(nuxt) builder = new Builder(nuxt)
await interceptLog('building nuxt', async () => {
await builder.build() await builder.build()
})
await nuxt.listen(port, 'localhost') await nuxt.listen(port, 'localhost')
}) })
test('/', async t => { test('/', async 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>'))
})
t.true(logSpy.calledOnce)
t.is(logSpy.args[0][0], 'Test plugin!')
}) })
test('/ (global styles inlined)', async t => { test('/ (global styles inlined)', async t => {
@ -52,15 +59,26 @@ test('/ (custom postcss.config.js)', async t => {
}) })
test('/test/ (router base)', async t => { test('/test/ (router base)', async t => {
const logSpy = await interceptLog(async () => {
const window = await nuxt.renderAndGetWindow(url('/test/')) const window = await nuxt.renderAndGetWindow(url('/test/'))
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 => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/about')) const window = await nuxt.renderAndGetWindow(url('/test/about'))
t.true(logSpy.calledOnce)
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')
t.true(html.includes('<h1>Custom layout</h1>')) t.true(html.includes('<h1>Custom layout</h1>'))
@ -68,7 +86,12 @@ 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 => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/desktop')) const window = await nuxt.renderAndGetWindow(url('/test/desktop'))
t.true(logSpy.calledOnce)
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')
t.true(html.includes('<h1>Default desktop layout</h1>')) t.true(html.includes('<h1>Default desktop layout</h1>'))
@ -76,7 +99,12 @@ 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 => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/mobile')) const window = await nuxt.renderAndGetWindow(url('/test/mobile'))
t.true(logSpy.calledOnce)
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')
t.true(html.includes('<h1>Default mobile layout</h1>')) t.true(html.includes('<h1>Default mobile layout</h1>'))
@ -84,7 +112,12 @@ test('/test/mobile (custom layout in mobile folder)', async t => {
}) })
test('/test/env', async t => { test('/test/env', async t => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/env')) const window = await nuxt.renderAndGetWindow(url('/test/env'))
t.true(logSpy.calledOnce)
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>'))
t.true(html.includes('"bool": true')) t.true(html.includes('"bool": true'))
@ -97,19 +130,34 @@ test('/test/env', async t => {
}) })
test('/test/error', async t => { test('/test/error', async t => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/error')) const window = await nuxt.renderAndGetWindow(url('/test/error'))
t.true(logSpy.calledOnce)
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 => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/user-agent')) const window = await nuxt.renderAndGetWindow(url('/test/user-agent'))
t.true(logSpy.calledOnce)
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 => {
const logSpy = await interceptLog()
const window = await nuxt.renderAndGetWindow(url('/test/about-bis')) const window = await nuxt.renderAndGetWindow(url('/test/about-bis'))
t.true(logSpy.calledOnce)
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>'))
t.true(html.includes('<h1>About page</h1>')) t.true(html.includes('<h1>About page</h1>'))
@ -142,6 +190,6 @@ test('Check build.styleResources for style-resources-loader', 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()
}) })