{"id":24771228,"url":"https://github.com/chema22r/minimal-jest-mock-console","last_synced_at":"2026-05-15T22:38:41.316Z","repository":{"id":51102147,"uuid":"263594031","full_name":"Chema22R/minimal-jest-mock-console","owner":"Chema22R","description":"Minimal Jest utility to mock the console","archived":false,"fork":false,"pushed_at":"2024-05-02T19:42:40.000Z","size":166,"stargazers_count":1,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T10:06:43.658Z","etag":null,"topics":["console","javascript","jest","minimal","mit","mit-license","mock","mocking","testing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/minimal-jest-mock-console","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Chema22R.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":"chema22r","patreon":"chema22r","open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2020-05-13T10:10:39.000Z","updated_at":"2022-08-30T13:43:02.000Z","dependencies_parsed_at":"2023-02-08T09:01:34.928Z","dependency_job_id":null,"html_url":"https://github.com/Chema22R/minimal-jest-mock-console","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chema22R%2Fminimal-jest-mock-console","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chema22R%2Fminimal-jest-mock-console/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chema22R%2Fminimal-jest-mock-console/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Chema22R%2Fminimal-jest-mock-console/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Chema22R","download_url":"https://codeload.github.com/Chema22R/minimal-jest-mock-console/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245164109,"owners_count":20571058,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["console","javascript","jest","minimal","mit","mit-license","mock","mocking","testing"],"created_at":"2025-01-29T03:59:35.868Z","updated_at":"2026-05-15T22:38:41.285Z","avatar_url":"https://github.com/Chema22R.png","language":"JavaScript","funding_links":["https://github.com/sponsors/chema22r","https://patreon.com/chema22r"],"categories":[],"sub_categories":[],"readme":"[![CodeQL](https://github.com/Chema22R/minimal-jest-mock-console/actions/workflows/codeql.yml/badge.svg)](https://github.com/Chema22R/minimal-jest-mock-console/actions/workflows/codeql.yml)\n[![MIT License](https://camo.githubusercontent.com/d59450139b6d354f15a2252a47b457bb2cc43828/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f6c2f7365727665726c6573732e737667)](LICENSE)\n\n# Minimal Jest Mock Console\nMinimal Jest utility to mock the console.\n\nIf you use console or prop-types in your app, and you use jest to test more than just the correct behavior, then you probably end up with tests that pass and look like this:\n\n![Screenshot](https://i.postimg.cc/1XnBhrY1/pre.png)\n\nThis could be very annoying and unhelpful. Specially if there is an actual failure and you have to search through all the red to find the actual failed test.\n\nThis package aims to solve this, mocking the console and intercepting all the expected errors we don't want to see, but letting pass the unexpected ones! Isn't it great?\n\n## Installation\n\n```\nnpm install --save-dev minimal-jest-mock-console\n```\n\n# Examples\n\n## Most Basic Example: Intercept All the Errors\n\n```js\nimport MockConsole from 'minimal-jest-mock-console';\n\ndescribe(...\n    it(...\n        const mockConsole = new MockConsole();\n\n        ...\n\n        mockConsole.restore(); // Always restore the console after each test\n    );\n);\n```\n\nThis example mocks the console without any configuration, intercepting all the calls to `console.error`. This implementation will catch and suppress the expected and **unexpected** errors, so, despite being the most basic way of doing it, it is **not** the recommended one.\n\nThe default configuration is to intercept the console error level, but we can do the same with any other console level (`error|warn|log|debug`):\n\n```js\nimport MockConsole from 'minimal-jest-mock-console';\n\ndescribe(...\n    it(...\n        const mockConsole = new MockConsole('warn');\n\n        ...\n\n        mockConsole.restore(); // Always restore the console after each test\n    );\n);\n```\nThis will intercept all the calls to `console.warn` (expected and **unexpected**, so also **not** recommended).\n\n\u003e `new MockConsole()` is equivalent to `new MockConsole('error', [/.*/u])`\n\n\u003e `new MockConsole('warn')` is equivalent to `new MockConsole('warn', [/.*/u])`\n\n## Intercept Only The Expected\n\nThe `MockConsole` class (`new MockConsole(level: string, regexList: RegExp[])`) receives two parameters:\n- level: the console level in which to look for calls (`error|warn|log|debug`)\n- regexList: the list of regular expressions against which to test console calls\n\n```js\nimport MockConsole from 'minimal-jest-mock-console';\n\ndescribe(...\n    it(...\n        const mockConsole = new MockConsole('error', [/Failed prop type:/]);\n\n        ...\n\n        mockConsole.restore(); // Always restore the console after each test\n    );\n);\n```\n\nThis example will intercept all the calls to `console.error` with the text \"Failed prop type:\".\n\nWith this implementation, looking at the image above, the suite `/src/components/LinkList/test.js` would pass and the suite `/src/components/LinkListItem/test.js` would fail with only one error. To intercept all the errors, we could implement something like this:\n\n```js\nimport MockConsole from 'minimal-jest-mock-console';\n\ndescribe(...\n    it(...\n        const mockConsole = new MockConsole('error', [\n            /Failed prop type:/,\n            /LinkListItem components should be rendered within a LinkList component/\n        ]);\n\n        ...\n\n        mockConsole.restore(); // Always restore the console after each test\n    );\n);\n```\n\n## Check Under the Hood and Catch Some Extra Errors\n\nThis package allows us to do some extra checks to ensure everything was as expected.\n\n```js\nimport MockConsole from 'minimal-jest-mock-console';\n\ndescribe(...\n    it(...\n        const mockConsole = new MockConsole('error', [/Failed prop type:/]);\n\n        ...\n\n        expect(mockConsole.expected('matches')).toBeTruthy();\n        expect(mockConsole.expected('handled')).toBeTruthy();\n        expect(mockConsole.expected('unhandled')).toBeTruthy();\n        expect(mockConsole.expected('errors')).toBeTruthy();\n        expect(mockConsole.expected()).toBeTruthy();\n\n        mockConsole.restore(); // Always restore the console after each test\n    );\n);\n```\n\nLet's see what is happening here:\n- `mockConsole.expected('matches')` returns `true` if the number of matches using the list of regular expressions (`regexList`) is equal to its length. This is very useful if we want to ensure each error matches with one and only one regex. If there is an error that matches with zero or more than one of the regular expressions, the test suite will fail.\n- `mockConsole.expected('handled')` returns `true` if the number of intercepted calls to `console.\u003clevel\u003e` is equal to the length of the list of regular expressions (`regexList`). This is useful if we want to ensure each regex matches at least one error. If we specify one regex per expected error and one of them matches zero, the test suite will fail.\n- `mockConsole.expected('unhandled')` returns `true` if the number of non-intercepted calls to `console.\u003clevel\u003e` is zero. This is useful to force the test suite to fail if there are unhandled errors (without this, console errors would be displayed but the test suite would pass like in the image above). This is equivalent to `expect(console.error).not.toBeCalled()`.\n- `mockConsole.expected('errors')` is a wrapper of the last two. This is useful using the second parameter (next example).\n- `mockConsole.expected()` is a wrapper of all these checks. This is recommended to check everything with just one line, but could be difficult to debug using only this. This is a good option for CI, but not for development.\n\nAll the checks perform comparisons against the length of the list of regular expressions (`regexList`). If we have only one regex to only one expected error, this behavior is perfect, but, if not, some of these checks would be useless. To solve this, use the second argument to define the number against which to do the comparisons:\n\n```js\nimport MockConsole from 'minimal-jest-mock-console';\n\ndescribe(...\n    it(...\n        const mockConsole = new MockConsole('error', [\n            /Failed prop type:/,\n            /Failed prop/\n        ]);\n\n        ...\n\n        expect(mockConsole.expected('matches', 2)).toBeTruthy();\n        expect(mockConsole.expected('handled', 1)).toBeTruthy();\n        expect(mockConsole.expected('unhandled', 1)).toBeTruthy();\n        expect(mockConsole.expected('errors', 2)).toBeTruthy();\n\n        mockConsole.restore(); // Always restore the console after each test\n    );\n);\n```\n\nWith this implementation, if there are two errors (one expected with the text \"Failed prop type:\" and one unexpected without that text), the suite will pass:\n- The expected error will match with both regular expressions (matches: 0 -\u003e 2) and will be intercepted (handled: 0 -\u003e 1, errors: 0 -\u003e 1)\n- The unexpected error will not match with any of the regular expressions and will not be intercepted (unhandled: 0 -\u003e 1, errors: 1 -\u003e 2)\n\n**With the default implementation (without the second parameter), the suite will fail, indicating that there is one non-intercepted error and one redundant regular expression.**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchema22r%2Fminimal-jest-mock-console","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchema22r%2Fminimal-jest-mock-console","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchema22r%2Fminimal-jest-mock-console/lists"}