Jest Console Mocking Utility
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.
Common errors
-
TypeError: console.error.mock is not a function
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.fixEnsure `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.
cause The `mockConsole()` function was either not called in the test/beforeEach, or its effect was overridden, or the `setupFilesAfterEnv` configuration is incorrect.fixConfirm `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).
Warnings
- breaking 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.
- gotcha 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.
- gotcha 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.
Install
-
npm install jest-mock-console -
yarn add jest-mock-console -
pnpm add jest-mock-console
Imports
- mockConsole
import { mockConsole } from 'jest-mock-console'; const mockConsole = require('jest-mock-console');import mockConsole from 'jest-mock-console';
- setupTestFramework.js
import 'jest-mock-console/dist/setupTestFramework.js';
setupFilesAfterEnv: ['jest-mock-console/dist/setupTestFramework.js']
Quickstart
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
});
});