refactor: mock consola in test

This commit is contained in:
Clark Du 2018-08-16 16:46:42 +01:00
parent 50be809b1c
commit c395e20e5b
8 changed files with 17 additions and 54 deletions

View File

@ -2,9 +2,6 @@ const consola = require('consola')
const { buildFixture } = require('../../utils/build')
describe('with-config', () => {
beforeAll(() => {
consola.warn = jest.fn()
})
buildFixture('with-config', () => {
expect(consola.warn).toHaveBeenCalledTimes(1)
expect(consola.warn.mock.calls[0]).toMatchObject([{

View File

@ -15,14 +15,10 @@ describe('basic config defaults', () => {
})
test('vendor has been deprecated', () => {
jest.spyOn(consola, 'warn')
const options = Options.from({
build: { vendor: 'vue' }
})
expect(options.build.vendor).toBeUndefined()
expect(consola.warn).toHaveBeenCalledWith('vendor has been deprecated due to webpack4 optimization')
consola.warn.mockRestore()
})
})

View File

@ -57,8 +57,6 @@ describe('basic ssr', () => {
})
test('/head', async () => {
jest.spyOn(consola, 'log')
const window = await nuxt.renderAndGetWindow(url('/head'))
expect(window.document.title).toBe('My title - Nuxt.js')
@ -71,8 +69,6 @@ describe('basic ssr', () => {
const metas = window.document.getElementsByTagName('meta')
expect(metas[0].getAttribute('content')).toBe('my meta')
expect(consola.log).toHaveBeenCalledWith('Body script!')
consola.log.mockRestore()
})
test('/async-data', async () => {

View File

@ -39,9 +39,10 @@ describe('error', () => {
})
test('Error: callHook()', async () => {
consola.error.mockClear()
const errorHook = jest.fn()
const error = new Error('test hook error')
jest.spyOn(consola, 'error')
nuxt.hook('error', errorHook)
nuxt.hook('test:error', () => { throw error })
@ -51,8 +52,6 @@ describe('error', () => {
expect(errorHook).toHaveBeenCalledWith(error)
expect(consola.error).toHaveBeenCalledTimes(1)
expect(consola.error).toHaveBeenCalledWith(error)
consola.error.mockRestore()
})
// Close server and ask nuxt to stop listening to file changes

View File

@ -60,12 +60,8 @@ describe('module', () => {
})
test('AddVendor - deprecated', () => {
jest.spyOn(consola, 'warn')
nuxt.moduleContainer.addVendor('nuxt-test')
expect(consola.warn).toHaveBeenCalledWith('addVendor has been deprecated due to webpack4 optimization')
consola.warn.mockRestore()
})
// Close server and ask nuxt to stop listening to file changes

View File

@ -1,10 +1,7 @@
import consola from 'consola'
import mockLog from '../utils/mock-log'
import { loadFixture, getPort, Nuxt } from '../utils'
let nuxt = null
let port
let nuxt, port
const url = route => 'http://localhost:' + port + route
const renderRoute = async (_url) => {
@ -15,8 +12,6 @@ const renderRoute = async (_url) => {
}
describe('spa', () => {
mockLog(['log'], consola)
beforeAll(async () => {
const config = loadFixture('spa')
nuxt = new Nuxt(config)
@ -29,6 +24,7 @@ describe('spa', () => {
expect(html).toMatch('Hello SPA!')
expect(consola.log).not.toHaveBeenCalledWith('created')
expect(consola.log).toHaveBeenCalledWith('mounted')
consola.log.mockClear()
})
test('/custom (custom layout)', async () => {
@ -36,6 +32,7 @@ describe('spa', () => {
expect(html).toMatch('Custom layout')
expect(consola.log).toHaveBeenCalledWith('created')
expect(consola.log).toHaveBeenCalledWith('mounted')
consola.log.mockClear()
})
test('/mounted', async () => {

View File

@ -1,24 +0,0 @@
/* eslint-disable no-console */
export default function mockLog(levels = 'all', logger = console) {
if (levels === 'all') {
levels = ['trace', 'debug', 'log', 'info', 'warn', 'error']
} else if (typeof levels === 'string') {
levels = [levels]
}
beforeAll(() => {
for (const level of levels) {
jest.spyOn(logger, level).mockImplementation(() => {})
}
})
beforeEach(() => {
for (const level of levels) {
logger[level].mockClear()
}
})
afterAll(() => {
for (const level of levels) {
logger[level].mockRestore()
}
})
return logger
}

View File

@ -1,10 +1,16 @@
// eslint-disable
require('consola').clear().add({
log: jest.fn()
})
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60 * 1000
const isAppveyor = !!process.env.APPVEYOR
describe.skip.appveyor = isAppveyor ? describe.skip : describe
test.skip.appveyor = isAppveyor ? test.skip : test
jest.setTimeout(60000)
jest.mock('consola', () => {
const consola = {}
for (const level of [
'fatal', 'error', 'warn', 'log', 'info',
'start', 'success', 'ready', 'debug', 'trace'
]) {
consola[level] = jest.fn()
}
return consola
})