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

jest-fail-on-console

v3.2.0

Published

Utility to make jest tests fail when console.error() or console.warn() are used

Downloads

1,165,264

Readme

jest-fail-on-console

Utility to make jest tests fail when console.error(), console.warn(), etc. are used

version MIT License PRs Welcome

What problem is this solving?

Jest doesn't fail the tests when there is a console.error. In large codebase, we can end up with the test output overloaded by a lot of errors, warnings, etc.. To prevent this, we want to fail each test that is logging to the console. We also want to conserve a clear output of the original error.

This is what this utility is doing.

image

Downloads

jest-fail-on-console npminsights

Install

yarn add -D jest-fail-on-console

or

npm install -D jest-fail-on-console

How to use

In a file used in the setupFilesAfterEnv option of Jest, add this code:

import failOnConsole from 'jest-fail-on-console'

failOnConsole()

// or with options:
failOnConsole({
  shouldFailOnWarn: false,
})

But I have some expected console errors/warning

If a console.error() is expected, then you should assert for it:

test('should log an error', () => {
  jest.spyOn(console, 'error').mockImplementation()
  // do your logic
  expect(console.error).toHaveBeenCalledWith('your error message')
})

Options

You can pass an object with options to the function:

errorMessage

Use this if you want to override the default error message of this library.

// signature
type errorMessage = (
  methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',
  bold: (string: string) => string
) => string

shouldFailOnAssert

Use this to make a test fail when a console.assert() is logged.

  • Type: boolean
  • Default: false

shouldFailOnDebug

Use this to make a test fail when a console.debug() is logged.

  • Type: boolean
  • Default: false

shouldFailOnError

Use this to make a test fail when a console.error() is logged.

  • Type: boolean
  • Default: true

shouldFailOnInfo

Use this to make a test fail when a console.info() is logged.

  • Type: boolean
  • Default: false

shouldFailOnLog

Use this to make a test fail when a console.log() is logged.

  • Type: boolean
  • Default: false

shouldFailOnWarn

Use this to make a test fail when a console.warn() is logged.

  • Type: boolean
  • Default: true

allowMessage

// signature
type allowMessage = (
  message: string,
  methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',
  context: { group: string; groups: string[] }
) => boolean

This function is called for every console method supported by this utility. If true is returned, the message will show in the console and the test won't fail.

Example:

failOnConsole({
  allowMessage: (errorMessage) => {
    if (/An expected error/.test(errorMessage)) {
      return true
    }
    return false
  },
})

silenceMessage

// signature
type silenceMessage = (
  message: string,
  methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',
  context: { group: string; groups: string[] }
) => boolean

This function is called for every console method supported by this utility. If true is returned, the message will not show in the console and the test won't fail.

Example:

failOnConsole({
  silenceMessage: (errorMessage) => {
    if (/Not implemented: navigation/.test(errorMessage)) {
      return true
    }
    return false
  },
})

skipTest

Use this if you want to ignore checks introduced by this library for specific tests determined by the return of the callback function. Return false if you do not want to skip console checks for the specific test and return true if you would like to skip it.

const ignoreList = [/.*components\/SomeComponent.test.tsx/]
const ignoreNameList = ['some component some test name']

failOnConsole({
  skipTest: ({ testPath, testName }) => {
    for (const pathExp of ignoreList) {
      const result = pathExp.test(testPath)
      if (result) return true
    }

    if (ignoreNameList.includes(testName)) {
      return true
    }

    return false
  },
})

License

MIT

Maintainers

This project is maintained by Valentin Hervieu.

This project was originally part of @ricardo-ch organisation because I (Valentin) was working at Ricardo. After leaving this company, they gracefully accepted to transfer the project to me. ❤️

Credits

Most of the logic is taken from React's setupTests file.