npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vitest-console

v0.1.1

Published

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

Downloads

1,350

Readme

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.