{"id":11142,"library":"jest-mock-console","title":"Jest Console Mocking Utility","description":"jest-mock-console is a Jest utility designed to temporarily silence or capture console output (like `console.log`, `console.warn`, `console.error`) during unit tests. This prevents noisy test reports where successful tests might display 'red' output due to expected warnings or errors, which can obscure actual test failures. The package is currently at version 2.0.0 and is actively maintained, providing a stable solution for managing console output in Jest environments. Its key differentiators include a simple API for mocking individual or multiple console methods, the provision of a `restoreConsole()` function to revert mocks, and a convenient `setupFilesAfterEnv` integration for automatic cleanup after each test. It primarily focuses on reducing visual clutter and enabling specific assertions on console calls, making test results clearer and easier to parse.","status":"active","version":"2.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/bpedersen/jest-mock-console","tags":["javascript","typescript"],"install":[{"cmd":"npm install jest-mock-console","lang":"bash","label":"npm"},{"cmd":"yarn add jest-mock-console","lang":"bash","label":"yarn"},{"cmd":"pnpm add jest-mock-console","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for testing environment integration.","package":"jest","optional":false}],"imports":[{"note":"The `mockConsole` function is the default export of the package. It ships with TypeScript types for improved developer experience.","wrong":"import { mockConsole } from 'jest-mock-console';\nconst mockConsole = require('jest-mock-console');","symbol":"mockConsole","correct":"import mockConsole from 'jest-mock-console';"},{"note":"This path is intended for Jest's `setupFilesAfterEnv` configuration option in `jest.config.js` or `package.json`, not for direct import within test files. It globally configures automatic console restoration.","wrong":"import 'jest-mock-console/dist/setupTestFramework.js';","symbol":"setupTestFramework.js","correct":"setupFilesAfterEnv: ['jest-mock-console/dist/setupTestFramework.js']"}],"quickstart":{"code":"import mockConsole from 'jest-mock-console';\n\ndescribe('My Feature', () => {\n  let restoreConsole: () => void;\n\n  beforeEach(() => {\n    // Mocks console.log, console.warn, and console.error by default.\n    // Returns a function to restore the console to its original state.\n    restoreConsole = mockConsole();\n  });\n\n  afterEach(() => {\n    // It is CRITICAL to restore the console after each test to prevent Jest\n    // from breaking in subsequent tests or producing unexpected behavior.\n    restoreConsole();\n  });\n\n  it('should capture console.error calls without displaying them in output', () => {\n    const errorMessage = 'An expected error occurred!';\n    console.error(errorMessage);\n    expect(console.error).toHaveBeenCalledTimes(1);\n    expect(console.error).toHaveBeenCalledWith(errorMessage);\n    expect(console.log).not.toHaveBeenCalled(); // Other methods are also mocked by default\n  });\n\n  it('can be configured to mock specific console methods only', () => {\n    restoreConsole(); // Restore previous mock to apply a new, specific one\n    restoreConsole = mockConsole(['info', 'debug']); // Only mock 'info' and 'debug'\n    console.info('This is debugging information.');\n    expect(console.info).toHaveBeenCalledWith('This is debugging information.');\n    expect(console.warn).not.toHaveBeenCalled(); // console.warn is no longer mocked\n  });\n});\n","lang":"typescript","description":"Demonstrates basic usage with `beforeEach` and `afterEach` hooks to mock and restore the console, capturing a `console.error` call and asserting its arguments."},"warnings":[{"fix":"Always call `restoreConsole()` in an `afterEach` hook or ensure `setupFilesAfterEnv: ['jest-mock-console/dist/setupTestFramework.js']` is correctly configured in your Jest setup.","message":"Forgetting to call `restoreConsole()` after `mockConsole()` will break subsequent Jest tests, leading to `console.log.mock` being undefined errors or unexpected console output in other tests.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Temporarily comment out `mockConsole()` during debugging or inspect `console.<method>.mock.calls` directly within your tests to see logged arguments without un-mocking.","message":"When `jest-mock-console` is active, console output is suppressed, which can make debugging difficult. Developers might expect to see `console.log` messages for debugging purposes but find them hidden.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Verify the path `jest-mock-console/dist/setupTestFramework.js` is correct in your `jest.config.js` or `package.json` and that Jest's configuration cache is cleared if changes aren't taking effect.","message":"Incorrect configuration of `setupFilesAfterEnv` in Jest can lead to `jest-mock-console` not being applied or restored correctly, resulting in inconsistent test behavior or console noise.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `mockConsole()` has been called before the `console.<method>` call and that `restoreConsole()` has not been called prematurely. If using global setup, verify `setupFilesAfterEnv` is active.","cause":"Attempting to use Jest's mock expectations (e.g., `toHaveBeenCalled`) on a console method that has not been mocked by `jest-mock-console` or has already been restored.","error":"TypeError: console.error.mock is not a function"},{"fix":"Confirm `mockConsole()` is called for each relevant test or suite. If using `setupFilesAfterEnv`, double-check the configuration path and ensure Jest is picking up the configuration (e.g., clear Jest cache).","cause":"The `mockConsole()` function was either not called in the test/beforeEach, or its effect was overridden, or the `setupFilesAfterEnv` configuration is incorrect.","error":"Jest output still shows console logs despite using jest-mock-console."}],"ecosystem":"npm"}