Registry / testing / jest-mock-console

jest-mock-console

JSON →
library2.0.0jsnpmunverified

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.

npm install jest-mock-console
INSTALL
IMPORT
SIG · JEST-MOCK-CONSOLE
J
jest-mock-console
testingjavascriptv2.0.0
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.

mockConsole
import mockConsole from 'jest-mock-console';
import { mockConsole } from 'jest-mock-console'; const mockConsole = require('jest-mock-console');
The `mockConsole` function is the default export of the package. It ships with TypeScript types for improved developer experience.
setupTestFramework.js
setupFilesAfterEnv: ['jest-mock-console/dist/setupTestFramework.js']
import 'jest-mock-console/dist/setupTestFramework.js';
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.

Demonstrates basic usage with `beforeEach` and `afterEach` hooks to mock and restore the console, capturing a `console.error` call and asserting its arguments.

import mockConsole from 'jest-mock-console'; describe('My Feature', () => { let restoreConsole: () => void; beforeEach(() => { // Mocks console.log, console.warn, and console.error by default. // Returns a function to restore the console to its original state. restoreConsole = mockConsole(); }); afterEach(() => { // It is CRITICAL to restore the console after each test to prevent Jest // from breaking in subsequent tests or producing unexpected behavior. restoreConsole(); }); it('should capture console.error calls without displaying them in output', () => { const errorMessage = 'An expected error occurred!'; console.error(errorMessage); expect(console.error).toHaveBeenCalledTimes(1); expect(console.error).toHaveBeenCalledWith(errorMessage); expect(console.log).not.toHaveBeenCalled(); // Other methods are also mocked by default }); it('can be configured to mock specific console methods only', () => { restoreConsole(); // Restore previous mock to apply a new, specific one restoreConsole = mockConsole(['info', 'debug']); // Only mock 'info' and 'debug' console.info('This is debugging information.'); expect(console.info).toHaveBeenCalledWith('This is debugging information.'); expect(console.warn).not.toHaveBeenCalled(); // console.warn is no longer mocked }); });
Debug
Known issues
breakingForgetting 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.
fix
Always call `restoreConsole()` in an `afterEach` hook or ensure `setupFilesAfterEnv: ['jest-mock-console/dist/setupTestFramework.js']` is correctly configured in your Jest setup.
affects: >=1.0.0
gotchaWhen `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.
fix
Temporarily comment out `mockConsole()` during debugging or inspect `console.<method>.mock.calls` directly within your tests to see logged arguments without un-mocking.
affects: >=1.0.0
gotchaIncorrect 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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: console.error.mock is not a function
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.
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.
Jest output still shows console logs despite using jest-mock-console.
The `mockConsole()` function was either not called in the test/beforeEach, or its effect was overridden, or the `setupFilesAfterEnv` configuration is incorrect.
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).
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
jestrequiredPeer dependency required for testing environment integration.
Agent activity
6 hits · last 30 days
node
6
Resources
jest-mock-console — npm install jest-mock-console · libregistry