Я поступил об этом неправильно. Вместо того, чтобы использовать spy / mock, я пытался перехватить stdout / stderr напрямую. Я решил это, используя следующую функцию.
/* eslint-disable no-undef */
export function spyConsole() {
let spy = {}
beforeEach(() => {
spy.console = jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
spy.console.mockClear()
})
afterAll(() => {
spy.console.mockRestore()
})
return spy
}
Который используется следующим образом:
import { createLocalVue, mount } from '@vue/test-utils'
import { spyConsole } from '@tst/helpers/test-utils'
import Vuetify from 'vuetify'
import VBtnPlus from '@/components/common/VBtnPlus.vue'
describe('VStatsCard.vue', () => {
let localVue = null
beforeEach(() => {
localVue = createLocalVue()
localVue.use(Vuetify)
})
describe('test prop warnings', () => {
let spy = spyConsole()
it('displays warning messages when both label and icon are not specified', () => {
mount(VBtnPlus, {
localVue: localVue
})
expect(console.error).toHaveBeenCalledTimes(1)
expect(spy.console.mock.calls[0][0]).toContain(
'[Vue warn]: Missing required prop, specify at least one of the following: "label" or "icon"'
)
})
})
})