Nuxt/test/helpers/console.js

143 lines
3.4 KiB
JavaScript
Raw Normal View History

2017-11-24 21:19:39 +00:00
import sinon from 'sinon'
let context = null
2017-11-24 21:19:39 +00:00
export function release() {
if (context === null) {
2018-01-13 05:22:11 +00:00
process.stderr.write(
'Console spy context was empty, did a previous test already release it?\n'
)
return
}
2017-11-24 21:19:39 +00:00
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
}
if (context.stdout) {
process.stdout.write = context.stdout
}
if (context.stderr) {
process.stderr.write = context.stderr
}
context = null
delete console.spiedInTest // eslint-disable-line no-console
2017-11-24 21:19:39 +00:00
}
export async function intercept(levels, msg, cb) {
if (context !== null) {
2018-01-13 05:22:11 +00:00
process.stderr.write(
'Console spy context was not empty, did a previous test not release it?\n'
)
}
context = {}
console.spiedInTest = true // eslint-disable-line no-console
2017-11-24 21:19:39 +00:00
if (cb === undefined && typeof msg === 'function') {
cb = msg
msg = undefined
if (typeof levels === 'string') {
msg = levels
levels = undefined
2017-11-24 21:19:39 +00:00
}
}
if (cb === undefined && msg === undefined && typeof levels === 'function') {
cb = levels
levels = undefined
2017-11-24 21:19:39 +00:00
}
const all = levels === undefined || levels === {}
const spies = {}
2017-11-24 21:19:39 +00:00
if (all || levels.log) {
2017-11-24 21:19:39 +00:00
context.log = console.log // eslint-disable-line no-console
spies.log = console.log = sinon.spy() // eslint-disable-line no-console
2017-11-24 21:19:39 +00:00
}
if (all || levels.info) {
2017-11-24 21:19:39 +00:00
context.info = console.info // eslint-disable-line no-console
spies.info = console.info = sinon.spy() // eslint-disable-line no-console
2017-11-24 21:19:39 +00:00
}
if (all || levels.warn) {
2017-11-24 21:19:39 +00:00
context.warn = console.warn // eslint-disable-line no-console
spies.warn = console.warn = sinon.spy() // eslint-disable-line no-console
2017-11-24 21:19:39 +00:00
}
if (all || levels.error) {
2017-11-24 21:19:39 +00:00
context.error = console.error // eslint-disable-line no-console
spies.error = console.error = sinon.spy() // eslint-disable-line no-console
2017-11-24 21:19:39 +00:00
}
if (levels && levels.stdout) {
context.stdout = process.stdout.write
spies.stdout = process.stdout.write = sinon.spy()
}
if (levels && levels.stderr) {
2017-12-17 21:14:05 +00:00
context.stderr = process.stderr.write
spies.stderr = process.stderr.write = sinon.spy()
}
2017-11-24 21:19:39 +00:00
if (cb) {
if (msg) {
if (context.stdout) {
context.stdout(` ${msg}`)
} else {
process.stdout.write(` ${msg}`)
}
2017-11-24 21:19:39 +00:00
}
await cb()
release()
2017-11-24 21:19:39 +00:00
if (msg) {
process.stdout.write('\n')
}
}
return spies
2017-11-24 21:19:39 +00:00
}
export async function interceptLog(msg, cb) {
const { log } = await intercept({ log: true }, msg, cb)
return log
2017-11-24 21:19:39 +00:00
}
export async function interceptInfo(msg, cb) {
const { info } = await intercept({ info: true }, msg, cb)
return info
2017-11-24 21:19:39 +00:00
}
export async function interceptWarn(msg, cb) {
const { warn } = await intercept({ warn: true }, msg, cb)
return warn
2017-11-24 21:19:39 +00:00
}
export async function interceptError(msg, cb) {
const { error } = await intercept({ error: true }, msg, cb)
return error
2017-11-24 21:19:39 +00:00
}
export async function interceptStdout(msg, cb) {
2017-12-17 21:14:05 +00:00
const { stdout } = await intercept({ stdout: true }, msg, cb)
return stdout
}
export async function interceptStderr(msg, cb) {
const { stderr } = await intercept({ stderr: true }, msg, cb)
return stderr
}