Quickly mock various console methods in Vitest and track their calls with custom matchers.
MIT License
Quickly mock various console
methods in Vitest and track their calls with custom matchers.
Install vitest-console
with your favorite package manager:
$ pnpm add -D vitest-console
Add a setup file to your Vitest configuration and inline the vitest-console
module:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
deps: {
inline: ['vitest-console'],
},
setupFiles: ['tests-setup.ts'],
},
})
Extends the built-in Vitest matchers with vitest-console
in your setup file:
import { expect } from 'vitest'
import { matchers } from 'vitest-console'
expect.extend(matchers)
You can now mock various console
methods either globally in your setup file, in a test file or even a describe
block:
import { afterAll, afterEach } from 'vitest'
import { mockConsole } from 'vitest-console'
const { clearConsole, restoreConsole } = mockConsole()
afterEach(clearConsole)
afterAll(restoreConsole)
Note
You can also mock
console
methods in a specific test:test('should do the thing', () => { const { restoreConsole } = mockConsole() doTheThing() expect(console).toHaveLogged('The thing was done.') restoreConsole() })
Mocks are set up using the mockConsole
function:
mockConsole(methods?: ConsoleMethods): ConsoleMock
mockConsole(options?: ConsoleMockOptions): ConsoleMock
mockConsole(methods: ConsoleMethods, options: ConsoleMockOptions): ConsoleMock
methods
By default, console.error
, console.info
, console.log
and console.warn
are mocked but you can specify your own list of methods to mock:
import { mockConsole } from 'vitest-console'
mockConsole(['log', 'table'])
options
You can silence any console output during tests using the quiet
option:
import { mockConsole } from 'vitest-console'
mockConsole({ quiet: true })
The mockConsole
function returns an object that contains two functions to clear (reset all informations about every call) and restore (revert the original console method implementations) the console
:
import { afterAll, afterEach } from 'vitest'
import { mockConsole } from 'vitest-console'
const { clearConsole, restoreConsole } = mockConsole()
afterEach(clearConsole)
afterAll(restoreConsole)
Asserts that an error was logged.
import { expect, test } from 'vitest'
test('should test if an error was logged', () => {
expect(console).toHaveErrored()
})
Asserts that a certain amount of errors were logged.
import { expect, test } from 'vitest'
test('should test if 3 errors were logged', () => {
expect(console).toHaveErroredTimes(3)
})
Asserts that an error was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if an error was logged with a specific text and number', () => {
expect(console).toHaveErroredWith('the error message', 123)
})
Asserts that errors were logged and that the last one was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if the last error was logged with a specific text and number', () => {
expect(console).toHaveLastErroredWith('the last error message', 123)
})
Asserts that errors were logged and that a specific one was logged with certain parameters.
The count starts at 1.
import { expect, test } from 'vitest'
test('should test if the second error was logged with a specific text and number', () => {
expect(console).toHaveNthErroredWith(2, 'the second error message', 123)
})
Asserts that an informational message was logged.
import { expect, test } from 'vitest'
test('should test if an informational message was logged', () => {
expect(console).toHaveInformed()
})
Asserts that a certain amount of informational messages were logged.
import { expect, test } from 'vitest'
test('should test if 3 informational messages were logged', () => {
expect(console).toHaveInformedTimes(3)
})
Asserts that an informational message was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if an informational message was logged with a specific text and number', () => {
expect(console).toHaveInformedWith('the informational message', 123)
})
Asserts that informational messages were logged and that the last one was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if the last informational message was logged with a specific text and number', () => {
expect(console).toHaveLastInformedWith('the last informational message', 123)
})
Asserts that informational messages were logged and that a specific one was logged with certain parameters.
The count starts at 1.
import { expect, test } from 'vitest'
test('should test if the second informational message was logged with a specific text and number', () => {
expect(console).toHaveNthInformedWith(2, 'the second informational message', 123)
})
Asserts that a message was logged.
import { expect, test } from 'vitest'
test('should test if a message was logged', () => {
expect(console).toHaveLogged()
})
Asserts that a certain amount of messages were logged.
import { expect, test } from 'vitest'
test('should test if 3 messages were logged', () => {
expect(console).toHaveLoggedTimes(3)
})
Asserts that a message was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if a message was logged with a specific text and number', () => {
expect(console).toHaveLoggedWith('the message', 123)
})
Asserts that messages were logged and that the last one was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if the last message was logged with a specific text and number', () => {
expect(console).toHaveLastLoggedWith('the last message', 123)
})
Asserts that messages were logged and that a specific one was logged with certain parameters.
The count starts at 1.
import { expect, test } from 'vitest'
test('should test if the second message was logged with a specific text and number', () => {
expect(console).toHaveNthLoggedWith(2, 'the second message', 123)
})
Asserts that a warning was logged.
import { expect, test } from 'vitest'
test('should test if a message was logged', () => {
expect(console).toHaveWarned()
})
Asserts that a certain amount of warnings were logged.
import { expect, test } from 'vitest'
test('should test if 3 warnings were logged', () => {
expect(console).toHaveWarnedTimes(3)
})
Asserts that a warning was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if a warning was logged with a specific text and number', () => {
expect(console).toHaveWarnedWith('the warning', 123)
})
Asserts that warnings were logged and that the last one was logged with certain parameters.
import { expect, test } from 'vitest'
test('should test if the last warning was logged with a specific text and number', () => {
expect(console).toHaveLastWarnedWith('the last warning', 123)
})
Asserts that warnings were logged and that a specific one was logged with certain parameters.
The count starts at 1.
import { expect, test } from 'vitest'
test('should test if the second warning was logged with a specific text and number', () => {
expect(console).toHaveNthWarnedWith(2, 'the second warning', 123)
})
If needed, you can also use the built-in matchers from Vitest with mocks created using vitest-console
.
import { expect, test } from 'vitest'
import { mockConsole } from 'vitest-console'
test('should test if a message was logged', () => {
const { restoreConsole } = mockConsole()
expect(console.log).toHaveBeenCalled()
restoreConsole()
})
Licensed under the MIT License, Copyright © HiDeoo.
See LICENSE for more information.