{"id":12993,"library":"console-fail-test","title":"Fail Tests on Console Output","description":"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.","status":"active","version":"0.6.1","language":"javascript","source_language":"en","source_url":"https://github.com/JoshuaKGoldberg/console-fail-test","tags":["javascript","typescript"],"install":[{"cmd":"npm install console-fail-test","lang":"bash","label":"npm"},{"cmd":"yarn add console-fail-test","lang":"bash","label":"yarn"},{"cmd":"pnpm add console-fail-test","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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'.","wrong":"const setup = require('console-fail-test/setup.cjs');","symbol":"setup file","correct":"import 'console-fail-test/setup';"},{"note":"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.","wrong":"import consoleFailTest from 'console-fail-test';\n// OR\nconst cft = require('console-fail-test');","symbol":"Root package (programmatic)","correct":"/* No direct programmatic import for functionality */"},{"note":"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.","wrong":"import type { SomeInternalType } from 'console-fail-test';","symbol":"Type definitions","correct":"/* No public types for direct import and usage */"}],"quickstart":{"code":"// jest.config.js\nmodule.exports = {\n  // Other Jest configurations...\n  testEnvironment: 'node',\n  setupFilesAfterEnv: [\n    // This path configures console-fail-test to run before each test file\n    // and ensure no console methods are called. This must be an ESM-compatible path.\n    'console-fail-test/setup'\n  ],\n};\n\n// src/example.test.js\ndescribe('example suite', () => {\n  test('should pass without console logs', () => {\n    // This test will pass because no console output occurs.\n    expect(true).toBe(true);\n  });\n\n  test('should fail with a console log', () => {\n    // This console.log will be caught by console-fail-test and cause the test to fail.\n    console.log('This will cause the test to fail!');\n    expect(true).toBe(true);\n  });\n});","lang":"javascript","description":"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."},"warnings":[{"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.","message":"Starting 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.","severity":"breaking","affected_versions":">=0.6.0"},{"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.","message":"Version 0.6.0 introduced a significant bump in the minimum Node.js version requirement.","severity":"breaking","affected_versions":">=0.6.0"},{"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.","message":"Any 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.","severity":"gotcha","affected_versions":">=0.4.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=0.4.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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.","cause":"A `console.*` method (like `console.log`, `console.warn`, `console.error`) was invoked during the execution of a test where `console-fail-test` is active.","error":"Error: Oh no! Your test called the following console method:"},{"fix":"Update your test runner configuration to refer to the current ESM-only setup path: `console-fail-test/setup`.","cause":"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.","error":"ERR_MODULE_NOT_FOUND: Cannot find module 'console-fail-test/setup.cjs'"},{"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.","cause":"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.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported. Instead change the require of ... to a dynamic import()"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}