refactor: make mocked log object mutable

This commit is contained in:
Clark Du 2018-04-13 14:59:22 +08:00
parent 20010a6f47
commit 9ddd56c97f
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9

View File

@ -1,22 +1,22 @@
/* eslint-disable no-console */
export default function mockConsole(levels = 'all') {
export default function mockLog(levels = 'all', logger = console) {
if (levels === 'all') {
levels = ['trace', 'debug', 'log', 'info', 'warn', 'error']
}
beforeAll(() => {
for (let level of levels) {
console[level] = jest.fn()
logger[level] = jest.fn()
}
})
beforeEach(() => {
for (let level of levels) {
console[level].mockClear()
logger[level].mockClear()
}
})
afterAll(() => {
for (let level of levels) {
console[level].mockRestore()
logger[level].mockRestore()
}
})
return console
return logger
}