Registry / testing / jest-fail-on-console

jest-fail-on-console

JSON →
library3.3.4jsnpmunverified

jest-fail-on-console is a utility library for Jest that enhances test suite robustness by automatically failing tests whenever `console.error()` or `console.warn()` methods are invoked during their execution. This helps developers identify and resolve unintended console output, which often indicates underlying issues or unnecessary noise in large codebases. The package is currently at version 3.3.4 and receives regular maintenance and feature updates, as evidenced by its recent patch releases addressing Jest compatibility and adding new configuration options. Its key differentiators include configurable options to fail on other console methods (`log`, `info`, `debug`, `assert`), advanced filtering with `allowMessage` and `silenceMessage` callbacks, and the ability to skip checks for specific tests, providing fine-grained control over console behavior during testing. It explicitly addresses a common pain point where Jest does not inherently fail tests for console output.

npm install jest-fail-on-console
INSTALL
IMPORT
SIG · JEST-FAIL-ON-CONSO
J
jest-fail-on-console
testingjavascriptv3.3.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

failOnConsole
import failOnConsole from 'jest-fail-on-console'
const failOnConsole = require('jest-fail-on-console')
This library primarily uses a default export. Ensure your Jest configuration (`setupFilesAfterEnv`) supports ESM if you're using `import` statements. While CommonJS `require` might work in some Jest setups, `import` is the recommended and typical usage.

This quickstart demonstrates how to integrate `jest-fail-on-console` into a Jest setup file, configure its options, and show examples of how it affects test outcomes, including how to handle expected console output.

import failOnConsole from 'jest-fail-on-console'; // Basic usage: fail on console.error and console.warn failOnConsole(); // Or with options for more granular control: failOnConsole({ shouldFailOnWarn: false, // Don't fail on warnings shouldFailOnLog: true, // Do fail on console.log allowMessage: (message, methodName) => { // Allow specific messages to pass without failing tests if (methodName === 'error' && message.includes('Expected error message')) { return true; } return false; }, silenceMessage: (message) => { // Prevent specific messages from even showing up in the console if (message.includes('Ignore this log')) { return true; } return false; } }); // Example test file (e.g., my-component.test.ts) describe('My Component', () => { it('should not log errors or warnings', () => { // Simulate some logic that might accidentally log console.log('This is a log that might fail if shouldFailOnLog is true'); console.warn('This is a warning that will fail by default'); console.error('This is an error that will always fail by default'); // ... your actual test logic ... }); it('should handle expected errors gracefully', () => { // To test an expected console.error without failing the test const spy = jest.spyOn(console, 'error').mockImplementation(() => {}); console.error('Expected error message'); expect(spy).toHaveBeenCalledWith('Expected error message'); spy.mockRestore(); // Important to restore the spy after the test }); });
Debug
Known issues
breakingTests will fail by default for any `console.error()` or `console.warn()` calls. This can cause existing test suites to fail unexpectedly upon integration without explicit configuration.
fix
Configure `jest-fail-on-console` with `shouldFailOnError: false` or `shouldFailOnWarn: false` if this behavior is not desired globally, or use `allowMessage` / `silenceMessage` for specific expected outputs. Alternatively, refactor code to avoid console calls or explicitly spy on `console` methods in tests.
affects: >=1.0.0
gotchaJest's `injectGlobals: false` configuration can lead to compatibility issues if not properly handled by `jest-fail-on-console` versions older than `3.3.2`.
fix
Upgrade to `jest-fail-on-console@3.3.2` or higher to ensure compatibility with Jest's `injectGlobals: false` setting. Alternatively, ensure `injectGlobals` is `true` in your Jest config if possible.
affects: <3.3.2
gotchaExpected `console.error` or `console.warn` messages can still cause tests to fail unless explicitly handled, preventing legitimate error logging from being tested.
fix
For expected console output, use `jest.spyOn(console, 'error').mockImplementation()` and assert on its calls, or utilize `allowMessage` or `silenceMessage` options within `failOnConsole` to explicitly permit or suppress specific messages.
affects: >=1.0.0
breakingThe peer dependency on `@jest/globals` requires `>=27.5.1`. Older versions of Jest or `@jest/globals` might cause installation issues or runtime errors.
fix
Ensure your project's Jest and `@jest/globals` versions meet the peer dependency requirement of `>=27.5.1`. Update Jest to a compatible version if necessary.
affects: >=3.3.4
Errors
Common errors & fixes
Tests are unexpectedly failing with messages like 'A test failed due to console.error' even though I expect some console output.
The library's default behavior is to fail tests on `console.error` and `console.warn`. You are likely logging an error or warning that is not explicitly allowed or spied upon.
fix
For expected `console.error`s or `console.warn`s, either use `jest.spyOn(console, 'error').mockImplementation()` and `expect(console.error).toHaveBeenCalledWith(...)`, or configure `failOnConsole({ allowMessage: (msg, methodName) => msg.includes('expected message') })`.
ReferenceError: failOnConsole is not defined (or similar import errors) when running Jest tests.
The `failOnConsole()` function is not being imported or executed in your Jest test environment before tests run.
fix
Ensure you have a file configured in your `setupFilesAfterEnv` Jest option (e.g., `jest.setup.ts`) that contains `import failOnConsole from 'jest-fail-on-console'; failOnConsole();`. Verify the path in your `jest.config.js` or `jest.config.ts` matches.
My tests are failing due to `console.log` messages, but I only want errors and warnings to fail.
The `shouldFailOnLog` option is likely enabled in your `failOnConsole` configuration, or a custom `allowMessage`/`silenceMessage` configuration is causing `console.log` to be treated as a failure.
fix
Explicitly configure `failOnConsole({ shouldFailOnLog: false })` if you want to ignore `console.log` messages and only fail on `error` or `warn`.
Upgrade
Version history
3.3.4latest on npm
Audit
Dependencies
@jest/globalsrequiredRequired for Jest global utilities, specifically for mocking and extending Jest's test environment. Version `>=27.5.1` is necessary for compatibility with newer Jest versions, including those using `jsdom 27`.
Agent activity
4 hits · last 30 days
node
4
Resources