Socket
Socket
Sign inDemoInstall

@closeio/test-console

Package Overview
Dependencies
0
Maintainers
8
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @closeio/test-console

Library for handling console logs in tests in a flexible way


Version published
Weekly downloads
1.1K
decreased by-10.25%
Maintainers
8
Created
Weekly downloads
 

Readme

Source

test-console

A flexible framework for managing console messages in your tests.

When to Use This?

Test output can get clutterd with logs and warnings for many reasons:

  • You upgraded a library and it started showing warnings where it didn't before. There are many of these and you don't have time to resolve them all right now
  • You started using a new testing library and it's showing you output or warnings about many things all at once
  • A warning started showing and you just didn't notice at the time, but now you do

Whatever the reason, if you find that there's a lot of noise in the output of your tests and you'd like to manage that, this library is for you!

Approach

Our approach is to give you a succinct way to identify output in your tests and deal with them in a systematic manner.

For instance, at Close we set the test output in our CI system to only show CRITICAL items. That way we have only the most important information in the output from our CI system, and if a test fails it's very easy to see why. While testing in watch mode in our development environment shows everything.

When we want to eliminate a particular warning from our test system, we set that matcher to be CRITICAL and run our tests with an CRITICAL threshold so we only see the items we need to fix. This helps us focus on the task at hand and see clearly when we've reached our goal.

Install

yarn add -D @closeio/test-console
npm i --save-dev @closeio/test-console

Usage

In a file that you load before running your tests, you can do something like this:

import { patchConsoleMethods } from '@closeio/test-console';

// Make the threshold customizable from the command line
const threshold = process.env.TEST_CONSOLE_LEVEL || 'ERROR';

patchConsoleMethods(
  ['debug', 'info', 'log', 'warn', 'error'], // the console methods to patch
  // A list of tests to compare against logged messages, and their associated
  // log level if matched.
  [
    { matcher: /warning: failed prop type:/i, level: 'WARNING' },
    {
      matcher: 'Each child in a list should have a unique "key" prop',
      level: 'ERROR',
    },
    {
      matcher: /warning: an update to .* inside a test was not wrapped in act/i,
      level: 'CRITICAL',
    },
  ],
  {
    // filenameRegex determines what lines from the stack trace will show when
    // logging out the test location.
    filenameRegex: /\.test\.[jt]sx?/,
    // If given, getTestName will be called when logging out test location, to
    // help you track down where logs are coming from.
    getTestName: () => expect.getState().currentTestName,
    // Log messages whose level is >= the threshold will be shown, along with
    // log messages that didn't match one of the log tests.
    threshold,
  },
);

And then you can run your tests with the level you want like:

TEST_CONSOLE_LEVEL=INFO yarn test

FAQs

Last updated on 07 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc