Registry / testing / console-fail-test

console-fail-test

JSON →
library0.6.1jsnpmunverified

console-fail-test is a utility library designed to enforce clean testing practices by automatically failing individual test runs if any console methods (e.g., console.log, console.warn, console.error) are invoked during their execution. The current stable version is 0.6.1, with a release cadence that focuses on minor updates for dependency maintenance, bug fixes, and broader test runner compatibility. A key differentiator is its framework-agnostic integration through a setup file, supporting popular test runners like Jest, Vitest, Mocha, and Ava. It provides detailed summaries of all console calls, including arguments, upon test failure, helping developers quickly identify and rectify unintended outputs. The library ensures that failures are reported *after* tests complete, allowing other test failures to surface normally, preventing test suites from passing when they shouldn't due to overlooked console messages. It's strictly ESM-only since version 0.6.0 and requires Node.js >=20.19.0.

npm install console-fail-test
INSTALL
IMPORT
SIG · CONSOLE-FAIL-TEST
C
console-fail-test
testingjavascriptv0.6.1
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.

setup file
import 'console-fail-test/setup';
const setup = require('console-fail-test/setup.cjs');
The primary usage is via a test runner's `setupFilesAfterEnv` configuration, typically using a direct import path. Since v0.6.0, the package is ESM-only and the path changed from 'console-fail-test/setup.mjs' or 'console-fail-test/setup.cjs' to simply 'console-fail-test/setup'.
Root package (programmatic)
/* No direct programmatic import for functionality */
import consoleFailTest from 'console-fail-test'; // OR const cft = require('console-fail-test');
The package is designed for side-effect configuration within a test environment and does not export a primary API from its root. Attempting a direct import of the package root will likely result in an empty object or module not found errors for ESM.
Type definitions
/* No public types for direct import and usage */
import type { SomeInternalType } from 'console-fail-test';
While `console-fail-test` ships TypeScript types, these are primarily for its internal implementation and the type definitions of the setup file. There are no explicitly exposed public types or interfaces intended for direct import and consumption by library users.

Demonstrates how to integrate `console-fail-test` into a Jest project using `setupFilesAfterEnv` and illustrates how a `console.log` call within a test will lead to a test failure.

// jest.config.js module.exports = { // Other Jest configurations... testEnvironment: 'node', setupFilesAfterEnv: [ // This path configures console-fail-test to run before each test file // and ensure no console methods are called. This must be an ESM-compatible path. 'console-fail-test/setup' ], }; // src/example.test.js describe('example suite', () => { test('should pass without console logs', () => { // This test will pass because no console output occurs. expect(true).toBe(true); }); test('should fail with a console log', () => { // This console.log will be caught by console-fail-test and cause the test to fail. console.log('This will cause the test to fail!'); expect(true).toBe(true); }); });
Debug
Known issues
breakingStarting with version 0.6.0, `console-fail-test` transitioned to an ESM-only package, discontinuing support for CommonJS (CJS) files. The primary setup path for test runners was unified.
fix
Update your test runner configuration to use the new, unified ESM path: `console-fail-test/setup`. If you were using `console-fail-test/setup.cjs` or `console-fail-test/setup.mjs`, these paths are no longer valid. Ensure your project's environment and test runner setup are configured for ESM compatibility.
affects: >=0.6.0
breakingVersion 0.6.0 introduced a significant bump in the minimum Node.js version requirement.
fix
Upgrade your Node.js runtime to version `20.19.0` or newer to be compatible with this package version. Older Node.js versions will likely encounter compatibility errors.
affects: >=0.6.0
gotchaAny invocation of `console.*` methods (e.g., `console.log`, `console.warn`, `console.error`) during a test run will cause that specific test to fail when `console-fail-test` is active. This is the core functionality, but can be surprising if not anticipated.
fix
Proactively remove all unnecessary `console.*` calls from your tests and the code under test. If console output is intentionally part of a test scenario (e.g., testing that a utility *does* log a warning), you might need to use test-runner specific features to temporarily disable or mock console methods for that particular test.
affects: >=0.4.0
gotchaThe library primarily operates by injecting side-effects into the test environment's lifecycle hooks. It does not provide a direct programmatic API (e.g., functions or classes to import and call) to enable, disable, or configure its behavior at runtime within test code.
fix
Configuration is exclusively via the test runner's setup mechanism (e.g., `setupFilesAfterEnv` for Jest/Vitest). Ensure the `console-fail-test/setup` file is correctly listed in your test runner's configuration.
affects: >=0.4.0
Errors
Common errors & fixes
Error: Oh no! Your test called the following console method:
A `console.*` method (like `console.log`, `console.warn`, `console.error`) was invoked during the execution of a test where `console-fail-test` is active.
fix
Locate the offending `console.*` call in your test file or the application code being tested and remove it. If the console output is expected for specific testing scenarios, consult your test runner's documentation for ways to mock or temporarily disable console methods.
ERR_MODULE_NOT_FOUND: Cannot find module 'console-fail-test/setup.cjs'
This error occurs when attempting to use the deprecated CommonJS setup path (`.cjs`) with `console-fail-test` versions 0.6.0 or newer, which are ESM-only.
fix
Update your test runner configuration to refer to the current ESM-only setup path: `console-fail-test/setup`.
Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported. Instead change the require of ... to a dynamic import()
You are attempting to `require()` `console-fail-test` (or its setup file) in a CommonJS context, but the package is now ESM-only since version 0.6.0.
fix
Ensure your project and test runner are configured for an ESM environment. This might involve setting `"type": "module"` in your `package.json` or configuring your test runner (e.g., Jest's `preset`) to handle ESM. Avoid `require()` for this package entirely.
Upgrade
Version history
0.6.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
OpenAI (training)
1
Resources
console-fail-test — npm install console-fail-test · libregistry