vitest-console
v0.1.1
Published
Quickly mock various console methods in Vitest and track their calls with custom matchers
Downloads
1,350
Maintainers
Readme
Quickly mock various console
methods in Vitest and track their calls with custom matchers.
- Usage
- Mock
- Matchers
- toHaveErrored
- toHaveErroredTimes
- toHaveErroredWith
- toHaveLastErroredWith
- toHaveNthErroredWith
- toHaveInformed
- toHaveInformedTimes
- toHaveInformedWith
- toHaveLastInformedWith
- toHaveNthInformedWith
- toHaveLogged
- toHaveLoggedTimes
- toHaveLoggedWith
- toHaveLastLoggedWith
- toHaveNthLoggedWith
- toHaveWarned
- toHaveWarnedTimes
- toHaveWarnedWith
- toHaveLastWarnedWith
- toHaveNthWarnedWith
Usage
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() })
Mock
Mocks are set up using the mockConsole
function:
mockConsole(methods?: ConsoleMethods): ConsoleMock
mockConsole(options?: ConsoleMockOptions): ConsoleMock
mockConsole(methods: ConsoleMethods, options: ConsoleMockOptions): ConsoleMock
Parameters
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 })
Return value
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)
Matchers
toHaveErrored
Asserts that an error was logged.
import { expect, test } from 'vitest'
test('should test if an error was logged', () => {
expect(console).toHaveErrored()
})
toHaveErroredTimes
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)
})
toHaveErroredWith
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)
})
toHaveLastErroredWith
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)
})
toHaveNthErroredWith
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)
})
toHaveInformed
Asserts that an informational message was logged.
import { expect, test } from 'vitest'
test('should test if an informational message was logged', () => {
expect(console).toHaveInformed()
})
toHaveInformedTimes
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)
})
toHaveInformedWith
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)
})
toHaveLastInformedWith
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)
})
toHaveNthInformedWith
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)
})
toHaveLogged
Asserts that a message was logged.
import { expect, test } from 'vitest'
test('should test if a message was logged', () => {
expect(console).toHaveLogged()
})
toHaveLoggedTimes
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)
})
toHaveLoggedWith
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)
})
toHaveLastLoggedWith
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)
})
toHaveNthLoggedWith
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)
})
toHaveWarned
Asserts that a warning was logged.
import { expect, test } from 'vitest'
test('should test if a message was logged', () => {
expect(console).toHaveWarned()
})
toHaveWarnedTimes
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)
})
toHaveWarnedWith
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)
})
toHaveLastWarnedWith
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)
})
toHaveNthWarnedWith
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)
})
Vitest matchers
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()
})
License
Licensed under the MIT License, Copyright © HiDeoo.
See LICENSE for more information.