mirror of
https://github.com/nuxt/nuxt.git
synced 2025-03-09 03:03:18 +00:00
refactor: extract a unified console util
This commit is contained in:
parent
a283788d9c
commit
7890d9c078
@ -1,46 +1,39 @@
|
||||
|
||||
import { Utils } from '../utils'
|
||||
import mockConsole from '../utils/console'
|
||||
|
||||
describe('utils', () => {
|
||||
test('printWarn', () => {
|
||||
const spy = jest.spyOn(console, 'warn').mockImplementation()
|
||||
const console = mockConsole()
|
||||
|
||||
test('printWarn', () => {
|
||||
Utils.printWarn('Testing printWarn', 'utils.test.js')
|
||||
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
expect(spy.mock.calls[0][0]).toContain('WARN')
|
||||
expect(spy.mock.calls[0][0]).toContain('Testing printWarn')
|
||||
|
||||
spy.mockRestore()
|
||||
expect(console.warn).toHaveBeenCalledTimes(1)
|
||||
expect(console.warn.mock.calls[0][0]).toContain('WARN')
|
||||
expect(console.warn.mock.calls[0][0]).toContain('Testing printWarn')
|
||||
})
|
||||
|
||||
test('printError', () => {
|
||||
const spy = jest.spyOn(console, 'error').mockImplementation()
|
||||
Utils.printError(new Error('Error object'), 'utils.test.js')
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
expect(spy.mock.calls[0][0]).toContain('Error: Error object')
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls[0][0]).toContain('Error: Error object')
|
||||
|
||||
spy.mockReset()
|
||||
console.error.mockClear()
|
||||
|
||||
Utils.printError('Error string', 'utils.test.js')
|
||||
expect(spy).toHaveBeenCalledTimes(1)
|
||||
expect(spy.mock.calls[0][0]).toContain('Error string')
|
||||
|
||||
spy.mockRestore()
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls[0][0]).toContain('Error string')
|
||||
})
|
||||
|
||||
test('fatalError', () => {
|
||||
const errorSpy = jest.spyOn(console, 'error').mockImplementation()
|
||||
const exitSpy = jest.spyOn(process, 'exit').mockImplementation()
|
||||
|
||||
Utils.fatalError('Testing fatalError')
|
||||
|
||||
expect(errorSpy).toHaveBeenCalledTimes(1)
|
||||
expect(errorSpy.mock.calls[0][0]).toContain('Testing fatalError')
|
||||
expect(console.error).toHaveBeenCalledTimes(1)
|
||||
expect(console.error.mock.calls[0][0]).toContain('Testing fatalError')
|
||||
expect(exitSpy).toHaveBeenCalledTimes(1)
|
||||
expect(exitSpy).toHaveBeenCalledWith(1)
|
||||
|
||||
errorSpy.mockRestore()
|
||||
exitSpy.mockRestore()
|
||||
})
|
||||
|
||||
|
22
test/utils/console.js
Normal file
22
test/utils/console.js
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user