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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fail-on-console

v1.3.1

Published

Fail Vitest or Jest tests when unexpected console logs, warnings or errors occur.

Readme

Fail on Console

Fail Vitest or Jest tests when unexpected console logs, warnings or errors occur.

npm version license

The fail-on-console utility fails test suites whenever unexpected console output or process writes are triggered, keeping test results clear and easy to read.

Features

  • ⚡ Vitest and Jest Native: Seamless integration with Vitest (including Browser Mode) and Jest using standard lifecycle hooks.
  • 📡 Raw Stream Monitoring: Fails on unexpected writes to process.stdout or process.stderr on top of standard console calls. Output from libraries that write directly to the stream doesn't slip through.
  • 🪶 Zero Dependencies: Pure, lightweight JavaScript with a tiny footprint.
  • 🎯 Configurable Targets: Choose exactly which console methods and / or process streams to monitor.
  • 📋 Flexible Allowlist: Easily suppress expected console noise globally, per suite, or per test using strings, regular expressions, or custom predicates.

Installation

# pnpm
pnpm add --save-dev fail-on-console

# npm
npm install --save-dev fail-on-console

# yarn
yarn add --dev fail-on-console

Setup

With Vitest

Initialize setupConsole inside a configured setupFiles module (e.g., vitest.setup.js).

// vitest.setup.js
import {beforeEach, afterEach} from "vitest";
import {setupConsole} from "fail-on-console";

setupConsole({beforeEach, afterEach});

With Jest

Initialize setupConsole inside a configured setupFilesAfterEnv module (e.g., jest.setup.js).

// jest.setup.js
import {beforeEach, afterEach} from "@jest/globals";
import {setupConsole} from "fail-on-console";

setupConsole({beforeEach, afterEach});

Customizing Monitored Methods and Streams

By default, console.debug is not monitored but error, warn, info, and log are. This can be customized by passing a methods array. Similarly, process.stdout and process.stderr are not monitored by default, but can be configured with a streams array:

setupConsole({
    beforeEach,
    afterEach,
    // Fail on console.error, console.warn, and console.debug.
    methods: ["error", "warn", "debug"],
    // Fail when libraries like Bunyan write straight to process.stdout,
    // bypassing console entirely.
    streams: ["stdout", "stderr"]
});

Suppressing Expected Logs

If a specific test or third-party dependency intentionally logs to the console, allowConsole and allowStream can be used to allow the test to pass.

These functions can be invoked globally, inside a describe block, or inside a specific test/it block.

import {allowConsole, allowStream} from "fail-on-console";

// Allow a substring.
allowConsole("warn", "third-party library warning");

// Allow a Regular Expression.
allowStream("stderr", /^Warning: Each child in a list/);

// Allow with a custom predicate function
allowConsole("log", (message) => message.startsWith("[analytics]"));

// An array of mixed matchers
allowStream("stdout", ["known warning", /deprecated/, (msg) => msg.includes("third-party")]);

API Reference

setupConsole(options)

Initializes console spies that monitor active tests.

  • options.beforeEach: The framework's beforeEach hook.
  • options.afterEach: The framework's afterEach hook.
  • options.methods: (Optional) Array of console methods to track. Defaults to ["error", "warn", "info", "log"].
  • options.streams: (Optional) Array of process streams to track. Defaults to [] (no streams monitored).

allowConsole(method, rules)

Registers a temporary or global allowlist rule for a monitored console method.

  • method: "error" | "warn" | "info" | "log" | "debug"
  • rules: A single rule or an array of rules. A rule can be:
    • string: Allowed if the console message contains this substring.
    • RegExp: Allowed if the regex tests true against the message.
    • Function: A predicate (message: string) => boolean returning true to allow the message.

allowStream(stream, rules)

Registers a temporary or global allowlist rule for a monitored process stream.

  • stream: "stdout" | "stderr"
  • rules: A single rule or an array of rules. A rule can be:
    • string: Allowed if the written message contains this substring.
    • RegExp: Allowed if the regex tests true against the message.
    • Function: A predicate (message: string) => boolean returning true to allow the message.

Limitations

Concurrency

fail-on-console is not compatible with concurrent asynchronous tests (e.g., test.concurrent). Because concurrent tests execute simultaneously within the same environment context, console logs cannot be isolated reliably per individual test. For suites requiring specific log suppression, tests must be run sequentially.

Assert

Monitoring console.assert is currently unsupported. It has a distinct signature and unique assertion mechanics compared to standard logging methods.

Mocha

Mocha is unsupported due to API incompatibilities related to test context. There are no clean or reliable workarounds for integration.

Browser Mode

process.stdout and process.stderr don't exist in a real browser environment, so the streams option has no effect under Vitest Browser Mode. setupConsole detects this and silently skips stream monitoring rather than throwing. methods monitoring is unaffected and works normally.

Credits & Prior Art

This package is inspired by and builds on the excellent foundation laid by:

License

MIT