{"id":26030056,"url":"https://github.com/bpedersen/jest-mock-console","last_synced_at":"2025-07-17T20:34:13.247Z","repository":{"id":38822127,"uuid":"123656825","full_name":"bpedersen/jest-mock-console","owner":"bpedersen","description":"Jest utility to mock the console","archived":false,"fork":false,"pushed_at":"2023-04-03T19:44:40.000Z","size":1688,"stargazers_count":40,"open_issues_count":2,"forks_count":11,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T19:18:43.110Z","etag":null,"topics":["hacktoberfest","jest","mock"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/bpedersen.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-03T03:51:57.000Z","updated_at":"2024-08-25T03:40:16.000Z","dependencies_parsed_at":"2024-06-18T13:49:56.256Z","dependency_job_id":"c47c3a83-b422-4f7b-9ec9-d6fa5b2cf19d","html_url":"https://github.com/bpedersen/jest-mock-console","commit_stats":{"total_commits":51,"total_committers":9,"mean_commits":5.666666666666667,"dds":0.6274509803921569,"last_synced_commit":"392c783ea743cec3582416b9140b6d16abf45d80"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpedersen%2Fjest-mock-console","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpedersen%2Fjest-mock-console/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpedersen%2Fjest-mock-console/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bpedersen%2Fjest-mock-console/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bpedersen","download_url":"https://codeload.github.com/bpedersen/jest-mock-console/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248094990,"owners_count":21046770,"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":["hacktoberfest","jest","mock"],"created_at":"2025-03-06T18:19:39.863Z","updated_at":"2025-04-09T19:18:47.897Z","avatar_url":"https://github.com/bpedersen.png","language":"TypeScript","readme":"# jest-mock-console\n\nJest utility to mock the console\n\n[![codecov][codecov-badge]][codecov]\n[![build][build-badge]][build]\n[![Dependencies][dependencyci-badge]][dependencyci]\n[![MIT License][license-badge]][license]\n\n## Table of Contents\n\n- [Problem](#the-problem)\n- [Solution](#the-solution)\n- [Installation](#installation)\n- [Basic Example](#basic-example)\n- [Advanced Example](#advanced-example)\n- [Analyze all logs](#analyze-all-logs)\n- [mockConsole(mocks)](#mockconsolemocks)\n  - [default](#mock-default)\n  - [string](#mock-string)\n  - [array](#mock-array)\n  - [object](#mock-object)\n\n## The problem\n\nIf you use console or prop-types in your app, and you use jest then you end up with tests that look like:\n\n\u003cimg\n  src=\"https://github.com/bpedersen/jest-mock-console/raw/master/img/screenshot-problem.png\"\n  alt=\"Terminal Screenshot\"\n  title=\"Terminal Screenshot\"\n  width=\"500px\"\n/\u003e\n\nThis is not helpful as all of the tests have passed, but you are seeing red. It is especially unhelpful when there is an actual failure as you have to search through all the red to find the actual failed test.\n\n## The solution\n\nThis allows you to mock and unmock the console at will, so your tests look like:\n\n\u003cimg\n  src=\"https://github.com/bpedersen/jest-mock-console/raw/master/img/screenshot-solution.png\"\n  alt=\"Terminal Screenshot\"\n  title=\"Terminal Screenshot\"\n  width=\"500px\"\n/\u003e\n\nThis is much more helpful as you don't see red on success anymore. Then you know when you see red text you know something went wrong.\n\n## Installation\n\nThis module is distributed view [npm][npm] which is bundled with [node][node] and should be installed as one of your project's `devDependencies`:\n\n```\nnpm install --save-dev jest-mock-console\n```\n\n## Basic Example\n\nAt the top of your test file:\n\n```javascript\nimport mockConsole from \"jest-mock-console\";\n```\n\nThen your tests\n\n```javascript\ndescribe(...\n  it(...\n    const restoreConsole = mockConsole();  // mockConsole returns a function to restore it back to normal\n    console.error('This will not show in the test report');\n    expect(console.error).toHaveBeenCalled();\n    restoreConsole();\n  )\n)\n```\n\nHowever you always need to restore the console after each test or you will break jest. This is where the [setupTestFramework](#setuptestframework) file comes in.\n\n## Advanced Example\n\nIf you don't want to worry about accidentally forgetting to `restoreConsole()` after your tests you can modify jest to unmock after every `it(...)`.\n\nIn your jest config:\n\n```javascript\nsetupFilesAfterEnv: [\"jest-mock-console/dist/setupTestFramework.js\"];\n```\n\nThen in your test files:\n\n```javascript\nimport mockConsole from 'jest-mock-console';\n\ndescribe(...\n  it(...\n    mockConsole();\n    console.error('This will not show in the test report');\n    expect(console.error).toHaveBeenCalled();\n    // No need for restoreConsole as it is called after every `it(...)`\n  )\n)\n```\n\n## Analyze all logs\n\nIn some circumstances Jest is infamous for logging because it overwrites the bash output. If you're experiencing this kind of logging frustration and you need to analyze all the `console.log` call arguments (typically to understand why a test is failing) you can temporarily add an expect statement on the console itself.\n\nIn your test files:\n\n```javascript\ndescribe(...\n  it(...\n    const restoreConsole = mockConsole();\n    console.error('This will not show in the test report');\n    expect(console.error.mock.calls).toEqual([]);\n    restoreConsole();\n  )\n)\n```\n\nthe test looks like:\n\n\u003cimg\n  src=\"img/screenshot-analyze-logs.jpg?raw=true\"\n  alt=\"Terminal Screenshot\"\n  title=\"Terminal Screenshot\"\n  width=\"500px\"\n/\u003e\n\n## mockConsole(mocks)\n\n- **`mocks[string,array,object]`**: The properties of the console you want to mock. Defaults to ['log','warn','error']\n  - \u003ca id='mock-default'\u003e\u003c/a\u003e default - Will mock console.log, console.warn, and console.error\n    ```\n    mockConsole() // same as `mockConsole(['log','warn','error'])\n    ```\n  - \u003ca id='mock-string'\u003e\u003c/a\u003e string - You can mock a single function\n    ```\n    mockConsole('error')\n    ```\n  - \u003ca id='mock-array'\u003e\u003c/a\u003e array - You can mock multiple functions\n    ```\n    mockConsole(['log', 'info'])\n    ```\n  - \u003ca id='mock-object'\u003e\u003c/a\u003e object - You can set custom functions for console\n    ```\n    const originalConsole = window.console;\n    mockConsole({\n      error: (...args) =\u003e originalConsole.log(`console.error of: ${...args}`)\n    })\n    ```\n\n## LICENSE\n\nMIT\n\n[npm]: https://www.npmjs.com/\n[node]: https://nodejs.org/\n[codecov-badge]: https://codecov.io/gh/bpedersen/jest-mock-console/branch/master/graph/badge.svg\n[codecov]: https://codecov.io/gh/bpedersen/jest-mock-console\n[build-badge]: https://travis-ci.org/bpedersen/jest-mock-console.svg\n[build]: https://travis-ci.org/bpedersen/jest-mock-console\n[dependencyci-badge]: https://dependencyci.com/github/bpedersen/jest-mock-console/badge\n[dependencyci]: https://dependencyci.com/github/bpedersen/jest-mock-console\n[license-badge]: https://img.shields.io/npm/l/jest-mock-console.svg\n[license]: https://github.com/bpedersen/jest-mock-console/blob/master/other/LICENSE\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbpedersen%2Fjest-mock-console","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbpedersen%2Fjest-mock-console","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbpedersen%2Fjest-mock-console/lists"}