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

@erickrodrcodes/jest-capture-console

v1.2.0

Published

Based on jest-fail-on-console. A Jest setup that helps you to warn or make your tests to fail if a console method is captured.

Downloads

16

Readme

Jest Capture Console

A Jest setup inspired on jest-fail-on-console. This setup can display warnings or fail tests if a console method was run during a test.

Reasoning behind it

While jest-fail-on-console is ideal to identify and stop console methods to make test fails (makes our CI's clean and healthier), it was noted that it is GOOD when you start a fresh new project.

However, on large projects where migrations are needed, it would immediately enter in panic mode specially if we have hundreds of tests to examine and determine what caused certain consoles to throw errors.

Here is where @erickrodrcodes/jest-capture-console enters. It can allow warnings of console methods being called on test, so your dev team can mitigate the problem, and once your team solves everything, you can switch the flag to make sure tests fails if a console method was called.

if you set the flag for your test to warn you about console implementations, you will see this:

| Warning on Test | | :-----------------------------------------------------------------------: | | Your team will be warned of unexpected console calls when running tests |

When you have controlled all the issues, turn on the flag to fail tests:

| Fail on Test | | :-------------------------------------------------------------------------------------: | | When the flag is turned to fail when finding console implementations, tests will fail |

Install

yarn add -D @erickrodrcodes/jest-capture-console

or

npm install -D @erickrodrcodes/jest-capture-console

Usage

Identify your jest.config.[js|ts] file and look for your setupFilesAfterEnv option. If you do not have one, add it to your export configuration:

export default {
  setupFilesAfterEnv: ['./jest.setup.ts'],
};

Usually, if you don't have a setup file for test, you will need to create a jest.setup.[js|ts] file. On it we will setup your test to capture console methods:

import { captureConsoleInTest } from '@erickrodrcodes/jest-capture-console';

// by default it will capture and warn console.error, console.log and console.warn
// test won't fail, but you will be warned on how to solve it.
captureConsoleInTest();

Options

The setup accepts an object with multiple parameters for you to use.

captureConsoleInTest({
  ... // options described below will come here
})

errorMessage

Use this if you want to override the default error message of this library with a custom function.

Signature

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

itShould

This will indicate to captureConsoleInTest how it will treat captured consoles. Use error to make a Test fail if a console method was used. use warn if you only want to warn in the terminal if a console was used.

| Type | Values | Default Value | | -------- | -------------------- | ------------- | | string | 'warn' - 'error' | 'warn' |

Signature

itShould: 'warn' | 'error';

onAssert

Use this option to warn or make a test fail if console.assert() was captured on the test.

| Type | Default Value | | --------- | ------------- | | boolean | false |

onDebug

Use this option to warn or make a test fail if console.debug() was captured on the test.

| Type | Default Value | | --------- | ------------- | | boolean | false |

onError

Use this option to warn or make a test fail if console.error() was captured on the test.

| Type | Default Value | | --------- | ------------- | | boolean | true |

onInfo

Use this option to warn or make a test fail if console.info() was captured on the test.

| Type | Default Value | | --------- | ------------- | | boolean | false |

onTrace

Use this option to warn or make a test fail if console.trace() was captured on the test.

| Type | Default Value | | --------- | ------------- | | boolean | false |

onWarn

Use this option to warn or make a test fail if console.warn() was captured on the test.

| Type | Default Value | | --------- | ------------- | | boolean | true |

allowMessage

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, even if itShould is 'error'

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

silenceMessage

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, even if itShould is 'error'

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

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'];

captureConsoleInTest({
  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;
  },
});

shouldPrintMessage

Use this to print the message immediately when called not awaiting the test to finish. This is useful to show the message if there are other or earlier test failures which will result in the fail on console error to be hidden by jest.

| Type | Default Value | | --------- | ------------- | | boolean | false |

License

MIT

Credits

This project is based on the work of Valentin Herview library jest-fail-on-console.


Built with love with