Coder Social home page Coder Social logo

hideoo / vitest-console Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 0.0 80 KB

Quickly mock various console methods in Vitest and track their calls with custom matchers.

License: MIT License

Shell 0.21% TypeScript 99.79%
console custom matchers mock test vitest

vitest-console's Introduction

vitest-console ๐Ÿ“ฐ

Vitest console mocks and custom matchers

Quickly mock various console methods in Vitest and track their calls with custom matchers.

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.

vitest-console's People

Contributors

hideoo avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.