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

@aarondewes/wp-jest-console

v5.0.0

Published

Custom Jest matchers for the Console object.

Downloads

6

Readme

Jest Console

Custom Jest matchers for the Console object to test JavaScript code in WordPress.

This package converts console.error, console.info, console.log and console.warn functions into mocks and tracks their calls. It also enforces usage of one of the related matchers whenever tested code calls one of the mentioned console methods. It means that you need to assert with .toHaveErrored() or .toHaveErroredWith( arg1, arg2, ... ) when console.error gets executed, and use the corresponding methods when console.info, console.log or console.warn are called. Your test will fail otherwise! This is a conscious design decision which helps to detect deprecation warnings when upgrading dependent libraries or smaller errors when refactoring code.

Installation

Install the module:

npm install @wordpress/jest-console --save-dev

Note: This package requires Node.js 12.0.0 or later. It is not compatible with older versions.

Setup

The simplest setup is to use Jest's setupFilesAfterEnv config option:

"jest": {
  "setupFilesAfterEnv": [
    "@wordpress/jest-console"
  ]
},

Usage

.toHaveErrored()

Use .toHaveErrored to ensure that console.error function was called.

For example, let's say you have a drinkAll( flavor ) function that makes you drink all available beverages. You might want to check if function calls console.error for 'octopus' instead, because 'octopus' flavor is really weird and why would anything be octopus-flavored? You can do that with this test suite:

describe( 'drinkAll', () => {
	test( 'drinks something lemon-flavored', () => {
		drinkAll( 'lemon' );
		expect( console ).not.toHaveErrored();
	} );

	test( 'errors when something is octopus-flavored', () => {
		drinkAll( 'octopus' );
		expect( console ).toHaveErrored();
	} );
} );

.toHaveErroredWith( arg1, arg2, ... )

Use .toHaveErroredWith to ensure that console.error function was called with specific arguments.

For example, let's say you have a drinkAll( flavor ) function again makes you drink all available beverages. You might want to check if function calls console.error with a specific message for 'octopus' instead, because 'octopus' flavor is really weird and why would anything be octopus-flavored? To make sure this works, you could write:

describe( 'drinkAll', () => {
	test( 'errors with message when something is octopus-flavored', () => {
		drinkAll( 'octopus' );
		expect( console ).toHaveErroredWith(
			'Should I really drink something that is octopus-flavored?'
		);
	} );
} );

.toHaveInformed()

Use .toHaveInformed to ensure that console.info function was called.

Almost identical usage as .toHaveErrored().

.toHaveInformedWith( arg1, arg2, ... )

Use .toHaveInformedWith to ensure that console.info function was called with specific arguments.

Almost identical usage as .toHaveErroredWith().

.toHaveLogged()

Use .toHaveLogged to ensure that console.log function was called.

Almost identical usage as .toHaveErrored().

.toHaveLoggedWith( arg1, arg2, ... )

Use .toHaveLoggedWith to ensure that console.log function was called with specific arguments.

Almost identical usage as .toHaveErroredWith().

.toHaveWarned()

Use .toHaveWarned to ensure that console.warn function was called.

Almost identical usage as .toHaveErrored().

.toHaveWarnedWith( arg1, arg2, ... )

Use .toHaveWarneddWith to ensure that console.warn function was called with specific arguments.

Almost identical usage as .toHaveErroredWith().