mirror of https://github.com/nuxt/nuxt.git
refactor: extract a unified console util
This commit is contained in:
parent
a283788d9c
commit
7890d9c078
|
@ -1,46 +1,39 @@
|
||||||
|
|
||||||
import { Utils } from '../utils'
|
import { Utils } from '../utils'
|
||||||
|
import mockConsole from '../utils/console'
|
||||||
|
|
||||||
describe('utils', () => {
|
describe('utils', () => {
|
||||||
test('printWarn', () => {
|
const console = mockConsole()
|
||||||
const spy = jest.spyOn(console, 'warn').mockImplementation()
|
|
||||||
|
|
||||||
|
test('printWarn', () => {
|
||||||
Utils.printWarn('Testing printWarn', 'utils.test.js')
|
Utils.printWarn('Testing printWarn', 'utils.test.js')
|
||||||
|
|
||||||
expect(spy).toHaveBeenCalledTimes(1)
|
expect(console.warn).toHaveBeenCalledTimes(1)
|
||||||
expect(spy.mock.calls[0][0]).toContain('WARN')
|
expect(console.warn.mock.calls[0][0]).toContain('WARN')
|
||||||
expect(spy.mock.calls[0][0]).toContain('Testing printWarn')
|
expect(console.warn.mock.calls[0][0]).toContain('Testing printWarn')
|
||||||
|
|
||||||
spy.mockRestore()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('printError', () => {
|
test('printError', () => {
|
||||||
const spy = jest.spyOn(console, 'error').mockImplementation()
|
|
||||||
Utils.printError(new Error('Error object'), 'utils.test.js')
|
Utils.printError(new Error('Error object'), 'utils.test.js')
|
||||||
expect(spy).toHaveBeenCalledTimes(1)
|
expect(console.error).toHaveBeenCalledTimes(1)
|
||||||
expect(spy.mock.calls[0][0]).toContain('Error: Error object')
|
expect(console.error.mock.calls[0][0]).toContain('Error: Error object')
|
||||||
|
|
||||||
spy.mockReset()
|
console.error.mockClear()
|
||||||
|
|
||||||
Utils.printError('Error string', 'utils.test.js')
|
Utils.printError('Error string', 'utils.test.js')
|
||||||
expect(spy).toHaveBeenCalledTimes(1)
|
expect(console.error).toHaveBeenCalledTimes(1)
|
||||||
expect(spy.mock.calls[0][0]).toContain('Error string')
|
expect(console.error.mock.calls[0][0]).toContain('Error string')
|
||||||
|
|
||||||
spy.mockRestore()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('fatalError', () => {
|
test('fatalError', () => {
|
||||||
const errorSpy = jest.spyOn(console, 'error').mockImplementation()
|
|
||||||
const exitSpy = jest.spyOn(process, 'exit').mockImplementation()
|
const exitSpy = jest.spyOn(process, 'exit').mockImplementation()
|
||||||
|
|
||||||
Utils.fatalError('Testing fatalError')
|
Utils.fatalError('Testing fatalError')
|
||||||
|
|
||||||
expect(errorSpy).toHaveBeenCalledTimes(1)
|
expect(console.error).toHaveBeenCalledTimes(1)
|
||||||
expect(errorSpy.mock.calls[0][0]).toContain('Testing fatalError')
|
expect(console.error.mock.calls[0][0]).toContain('Testing fatalError')
|
||||||
expect(exitSpy).toHaveBeenCalledTimes(1)
|
expect(exitSpy).toHaveBeenCalledTimes(1)
|
||||||
expect(exitSpy).toHaveBeenCalledWith(1)
|
expect(exitSpy).toHaveBeenCalledWith(1)
|
||||||
|
|
||||||
errorSpy.mockRestore()
|
|
||||||
exitSpy.mockRestore()
|
exitSpy.mockRestore()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* eslint-disable no-console */
|
||||||
|
export default function mockConsole(levels = 'all') {
|
||||||
|
if (levels === 'all') {
|
||||||
|
levels = ['trace', 'debug', 'log', 'info', 'warn', 'error']
|
||||||
|
}
|
||||||
|
beforeAll(() => {
|
||||||
|
for (let level of levels) {
|
||||||
|
console[level] = jest.fn()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
beforeEach(() => {
|
||||||
|
for (let level of levels) {
|
||||||
|
console[level].mockClear()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
afterAll(() => {
|
||||||
|
for (let level of levels) {
|
||||||
|
console[level].mockRestore()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return console
|
||||||
|
}
|
Loading…
Reference in New Issue