refactor: extract a unified console util

This commit is contained in:
Clark Du 2018-03-30 16:27:18 +08:00
parent a283788d9c
commit 7890d9c078
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9
2 changed files with 35 additions and 20 deletions

View File

@ -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()
}) })

22
test/utils/console.js Normal file
View File

@ -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
}