{"id":11138,"library":"jest-fail-on-console","title":"Jest Fail On Console","description":"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.","status":"active","version":"3.3.4","language":"javascript","source_language":"en","source_url":"https://github.com/ValentinH/jest-fail-on-console","tags":["javascript","jest","console","fail"],"install":[{"cmd":"npm install jest-fail-on-console","lang":"bash","label":"npm"},{"cmd":"yarn add jest-fail-on-console","lang":"bash","label":"yarn"},{"cmd":"pnpm add jest-fail-on-console","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required 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`.","package":"@jest/globals","optional":false}],"imports":[{"note":"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.","wrong":"const failOnConsole = require('jest-fail-on-console')","symbol":"failOnConsole","correct":"import failOnConsole from 'jest-fail-on-console'"}],"quickstart":{"code":"import failOnConsole from 'jest-fail-on-console';\n\n// Basic usage: fail on console.error and console.warn\nfailOnConsole();\n\n// Or with options for more granular control:\nfailOnConsole({\n  shouldFailOnWarn: false, // Don't fail on warnings\n  shouldFailOnLog: true,   // Do fail on console.log\n  allowMessage: (message, methodName) => {\n    // Allow specific messages to pass without failing tests\n    if (methodName === 'error' && message.includes('Expected error message')) {\n      return true;\n    }\n    return false;\n  },\n  silenceMessage: (message) => {\n    // Prevent specific messages from even showing up in the console\n    if (message.includes('Ignore this log')) {\n      return true;\n    }\n    return false;\n  }\n});\n\n// Example test file (e.g., my-component.test.ts)\ndescribe('My Component', () => {\n  it('should not log errors or warnings', () => {\n    // Simulate some logic that might accidentally log\n    console.log('This is a log that might fail if shouldFailOnLog is true');\n    console.warn('This is a warning that will fail by default');\n    console.error('This is an error that will always fail by default');\n    // ... your actual test logic ...\n  });\n\n  it('should handle expected errors gracefully', () => {\n    // To test an expected console.error without failing the test\n    const spy = jest.spyOn(console, 'error').mockImplementation(() => {});\n    console.error('Expected error message');\n    expect(spy).toHaveBeenCalledWith('Expected error message');\n    spy.mockRestore(); // Important to restore the spy after the test\n  });\n});","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Tests 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.","severity":"breaking","affected_versions":">=1.0.0"},{"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.","message":"Jest's `injectGlobals: false` configuration can lead to compatibility issues if not properly handled by `jest-fail-on-console` versions older than `3.3.2`.","severity":"gotcha","affected_versions":"<3.3.2"},{"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.","message":"Expected `console.error` or `console.warn` messages can still cause tests to fail unless explicitly handled, preventing legitimate error logging from being tested.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The peer dependency on `@jest/globals` requires `>=27.5.1`. Older versions of Jest or `@jest/globals` might cause installation issues or runtime errors.","severity":"breaking","affected_versions":">=3.3.4"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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') })`.","cause":"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.","error":"Tests are unexpectedly failing with messages like 'A test failed due to console.error' even though I expect some console output."},{"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.","cause":"The `failOnConsole()` function is not being imported or executed in your Jest test environment before tests run.","error":"ReferenceError: failOnConsole is not defined (or similar import errors) when running Jest tests."},{"fix":"Explicitly configure `failOnConsole({ shouldFailOnLog: false })` if you want to ignore `console.log` messages and only fail on `error` or `warn`.","cause":"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.","error":"My tests are failing due to `console.log` messages, but I only want errors and warnings to fail."}],"ecosystem":"npm"}