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-consoleVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage with `beforeEach` and `afterEach` hooks to mock and restore the console, capturing a `console.error` call and asserting its arguments.
Always call `restoreConsole()` in an `afterEach` hook or ensure `setupFilesAfterEnv: ['jest-mock-console/dist/setupTestFramework.js']` is correctly configured in your Jest setup.
Temporarily comment out `mockConsole()` during debugging or inspect `console.<method>.mock.calls` directly within your tests to see logged arguments without un-mocking.
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.
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.
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).