mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 22:25:12 +00:00
refactor: mock consola in test
This commit is contained in:
parent
50be809b1c
commit
c395e20e5b
@ -2,9 +2,6 @@ const consola = require('consola')
|
|||||||
const { buildFixture } = require('../../utils/build')
|
const { buildFixture } = require('../../utils/build')
|
||||||
|
|
||||||
describe('with-config', () => {
|
describe('with-config', () => {
|
||||||
beforeAll(() => {
|
|
||||||
consola.warn = jest.fn()
|
|
||||||
})
|
|
||||||
buildFixture('with-config', () => {
|
buildFixture('with-config', () => {
|
||||||
expect(consola.warn).toHaveBeenCalledTimes(1)
|
expect(consola.warn).toHaveBeenCalledTimes(1)
|
||||||
expect(consola.warn.mock.calls[0]).toMatchObject([{
|
expect(consola.warn.mock.calls[0]).toMatchObject([{
|
||||||
|
@ -15,14 +15,10 @@ describe('basic config defaults', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('vendor has been deprecated', () => {
|
test('vendor has been deprecated', () => {
|
||||||
jest.spyOn(consola, 'warn')
|
|
||||||
|
|
||||||
const options = Options.from({
|
const options = Options.from({
|
||||||
build: { vendor: 'vue' }
|
build: { vendor: 'vue' }
|
||||||
})
|
})
|
||||||
expect(options.build.vendor).toBeUndefined()
|
expect(options.build.vendor).toBeUndefined()
|
||||||
expect(consola.warn).toHaveBeenCalledWith('vendor has been deprecated due to webpack4 optimization')
|
expect(consola.warn).toHaveBeenCalledWith('vendor has been deprecated due to webpack4 optimization')
|
||||||
|
|
||||||
consola.warn.mockRestore()
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -57,8 +57,6 @@ describe('basic ssr', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('/head', async () => {
|
test('/head', async () => {
|
||||||
jest.spyOn(consola, 'log')
|
|
||||||
|
|
||||||
const window = await nuxt.renderAndGetWindow(url('/head'))
|
const window = await nuxt.renderAndGetWindow(url('/head'))
|
||||||
expect(window.document.title).toBe('My title - Nuxt.js')
|
expect(window.document.title).toBe('My title - Nuxt.js')
|
||||||
|
|
||||||
@ -71,8 +69,6 @@ describe('basic ssr', () => {
|
|||||||
const metas = window.document.getElementsByTagName('meta')
|
const metas = window.document.getElementsByTagName('meta')
|
||||||
expect(metas[0].getAttribute('content')).toBe('my meta')
|
expect(metas[0].getAttribute('content')).toBe('my meta')
|
||||||
expect(consola.log).toHaveBeenCalledWith('Body script!')
|
expect(consola.log).toHaveBeenCalledWith('Body script!')
|
||||||
|
|
||||||
consola.log.mockRestore()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('/async-data', async () => {
|
test('/async-data', async () => {
|
||||||
|
@ -39,9 +39,10 @@ describe('error', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('Error: callHook()', async () => {
|
test('Error: callHook()', async () => {
|
||||||
|
consola.error.mockClear()
|
||||||
|
|
||||||
const errorHook = jest.fn()
|
const errorHook = jest.fn()
|
||||||
const error = new Error('test hook error')
|
const error = new Error('test hook error')
|
||||||
jest.spyOn(consola, 'error')
|
|
||||||
|
|
||||||
nuxt.hook('error', errorHook)
|
nuxt.hook('error', errorHook)
|
||||||
nuxt.hook('test:error', () => { throw error })
|
nuxt.hook('test:error', () => { throw error })
|
||||||
@ -51,8 +52,6 @@ describe('error', () => {
|
|||||||
expect(errorHook).toHaveBeenCalledWith(error)
|
expect(errorHook).toHaveBeenCalledWith(error)
|
||||||
expect(consola.error).toHaveBeenCalledTimes(1)
|
expect(consola.error).toHaveBeenCalledTimes(1)
|
||||||
expect(consola.error).toHaveBeenCalledWith(error)
|
expect(consola.error).toHaveBeenCalledWith(error)
|
||||||
|
|
||||||
consola.error.mockRestore()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Close server and ask nuxt to stop listening to file changes
|
// Close server and ask nuxt to stop listening to file changes
|
||||||
|
@ -60,12 +60,8 @@ describe('module', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('AddVendor - deprecated', () => {
|
test('AddVendor - deprecated', () => {
|
||||||
jest.spyOn(consola, 'warn')
|
|
||||||
|
|
||||||
nuxt.moduleContainer.addVendor('nuxt-test')
|
nuxt.moduleContainer.addVendor('nuxt-test')
|
||||||
expect(consola.warn).toHaveBeenCalledWith('addVendor has been deprecated due to webpack4 optimization')
|
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
|
// Close server and ask nuxt to stop listening to file changes
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
import consola from 'consola'
|
import consola from 'consola'
|
||||||
import mockLog from '../utils/mock-log'
|
|
||||||
import { loadFixture, getPort, Nuxt } from '../utils'
|
import { loadFixture, getPort, Nuxt } from '../utils'
|
||||||
|
|
||||||
let nuxt = null
|
let nuxt, port
|
||||||
|
|
||||||
let port
|
|
||||||
const url = route => 'http://localhost:' + port + route
|
const url = route => 'http://localhost:' + port + route
|
||||||
|
|
||||||
const renderRoute = async (_url) => {
|
const renderRoute = async (_url) => {
|
||||||
@ -15,8 +12,6 @@ const renderRoute = async (_url) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('spa', () => {
|
describe('spa', () => {
|
||||||
mockLog(['log'], consola)
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
const config = loadFixture('spa')
|
const config = loadFixture('spa')
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
@ -29,6 +24,7 @@ describe('spa', () => {
|
|||||||
expect(html).toMatch('Hello SPA!')
|
expect(html).toMatch('Hello SPA!')
|
||||||
expect(consola.log).not.toHaveBeenCalledWith('created')
|
expect(consola.log).not.toHaveBeenCalledWith('created')
|
||||||
expect(consola.log).toHaveBeenCalledWith('mounted')
|
expect(consola.log).toHaveBeenCalledWith('mounted')
|
||||||
|
consola.log.mockClear()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('/custom (custom layout)', async () => {
|
test('/custom (custom layout)', async () => {
|
||||||
@ -36,6 +32,7 @@ describe('spa', () => {
|
|||||||
expect(html).toMatch('Custom layout')
|
expect(html).toMatch('Custom layout')
|
||||||
expect(consola.log).toHaveBeenCalledWith('created')
|
expect(consola.log).toHaveBeenCalledWith('created')
|
||||||
expect(consola.log).toHaveBeenCalledWith('mounted')
|
expect(consola.log).toHaveBeenCalledWith('mounted')
|
||||||
|
consola.log.mockClear()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('/mounted', async () => {
|
test('/mounted', async () => {
|
||||||
|
@ -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
|
|
||||||
}
|
|
@ -1,10 +1,16 @@
|
|||||||
// eslint-disable
|
|
||||||
require('consola').clear().add({
|
|
||||||
log: jest.fn()
|
|
||||||
})
|
|
||||||
|
|
||||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60 * 1000
|
|
||||||
|
|
||||||
const isAppveyor = !!process.env.APPVEYOR
|
const isAppveyor = !!process.env.APPVEYOR
|
||||||
describe.skip.appveyor = isAppveyor ? describe.skip : describe
|
describe.skip.appveyor = isAppveyor ? describe.skip : describe
|
||||||
test.skip.appveyor = isAppveyor ? test.skip : test
|
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
|
||||||
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user