{"id":13769656,"url":"https://github.com/ValentinH/jest-fail-on-console","last_synced_at":"2025-05-11T02:33:04.233Z","repository":{"id":39752923,"uuid":"327962407","full_name":"ValentinH/jest-fail-on-console","owner":"ValentinH","description":"Utility to make jest tests fail when console.error() or any other methods are used","archived":false,"fork":false,"pushed_at":"2024-10-02T07:57:35.000Z","size":240,"stargazers_count":148,"open_issues_count":3,"forks_count":21,"subscribers_count":7,"default_branch":"main","last_synced_at":"2024-11-11T19:45:32.588Z","etag":null,"topics":["jest"],"latest_commit_sha":null,"homepage":"","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/ValentinH.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":"2021-01-08T16:58:44.000Z","updated_at":"2024-10-14T11:02:40.000Z","dependencies_parsed_at":"2024-06-18T13:38:40.498Z","dependency_job_id":"775d9be5-bc9d-4f2a-aae2-c0a0247b093b","html_url":"https://github.com/ValentinH/jest-fail-on-console","commit_stats":{"total_commits":55,"total_committers":14,"mean_commits":"3.9285714285714284","dds":0.6909090909090909,"last_synced_commit":"72fc01b942027f01fb1f405bfd1f119c896bf371"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValentinH%2Fjest-fail-on-console","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValentinH%2Fjest-fail-on-console/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValentinH%2Fjest-fail-on-console/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ValentinH%2Fjest-fail-on-console/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ValentinH","download_url":"https://codeload.github.com/ValentinH/jest-fail-on-console/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224952246,"owners_count":17397527,"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":["jest"],"created_at":"2024-08-03T17:00:30.158Z","updated_at":"2024-11-17T05:31:17.856Z","avatar_url":"https://github.com/ValentinH.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":["Library extensions"],"readme":"# jest-fail-on-console\n\nUtility to make jest tests fail when `console.error()`, `console.warn()`, etc. are used\n\n[![version][version-badge]][package] [![MIT License][license-badge]][license] [![PRs Welcome][prs-badge]][prs]\n\n## What problem is this solving?\n\nJest doesn't fail the tests when there is a `console.error`. In large codebase, we can end up with the test output overloaded by a lot of errors, warnings, etc..\nTo prevent this, we want to fail each test that is logging to the console. We also want to conserve a clear output of the original error.\n\nThis is what this utility is doing.\n\n![image](https://user-images.githubusercontent.com/2678610/104045400-cbe05b80-51de-11eb-820c-b96190bbff7f.png)\n\n## Downloads\n\n[![jest-fail-on-console npminsights](https://npminsights.vercel.app/api/package/readme-image/jest-fail-on-console?v=2023-02-22)](https://npminsights.vercel.app/package/jest-fail-on-console)\n\n## Install\n\n```shell\nyarn add -D jest-fail-on-console\n```\n\nor\n\n```shell\nnpm install -D jest-fail-on-console\n```\n\n## How to use\n\nIn a file used in the `setupFilesAfterEnv` option of Jest, add this code:\n\n```ts\nimport failOnConsole from 'jest-fail-on-console'\n\nfailOnConsole()\n\n// or with options:\nfailOnConsole({\n  shouldFailOnWarn: false,\n})\n```\n\n## But I have some expected console errors/warning\n\nIf a `console.error()` is expected, then you should assert for it:\n\n```ts\ntest('should log an error', () =\u003e {\n  jest.spyOn(console, 'error').mockImplementation()\n  // do your logic\n  expect(console.error).toHaveBeenCalledWith('your error message')\n})\n```\n\n## Options\n\nYou can pass an object with options to the function:\n\n### errorMessage\n\nUse this if you want to override the default error message of this library.\n\n```ts\n// signature\ntype errorMessage = (\n  methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',\n  bold: (string: string) =\u003e string\n) =\u003e string\n```\n\n### shouldFailOnAssert\n\nUse this to make a test fail when a `console.assert()` is logged.\n\n- Type: `boolean`\n- Default: `false`\n\n### shouldFailOnDebug\n\nUse this to make a test fail when a `console.debug()` is logged.\n\n- Type: `boolean`\n- Default: `false`\n\n### shouldFailOnError\n\nUse this to make a test fail when a `console.error()` is logged.\n\n- Type: `boolean`\n- Default: `true`\n\n### shouldFailOnInfo\n\nUse this to make a test fail when a `console.info()` is logged.\n\n- Type: `boolean`\n- Default: `false`\n\n### shouldFailOnLog\n\nUse this to make a test fail when a `console.log()` is logged.\n\n- Type: `boolean`\n- Default: `false`\n\n### shouldFailOnWarn\n\nUse this to make a test fail when a `console.warn()` is logged.\n\n- Type: `boolean`\n- Default: `true`\n\n### allowMessage\n\n```ts\n// signature\ntype allowMessage = (\n  message: string,\n  methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',\n  context: { group: string; groups: string[] }\n) =\u003e boolean\n```\n\nThis function is called for every console method supported by this utility.\nIf `true` is returned, the message will show in the console and the test won't fail.\n\nExample:\n\n```ts\nfailOnConsole({\n  allowMessage: (errorMessage) =\u003e {\n    if (/An expected error/.test(errorMessage)) {\n      return true\n    }\n    return false\n  },\n})\n```\n\n### silenceMessage\n\n```ts\n// signature\ntype silenceMessage = (\n  message: string,\n  methodName: 'assert' | 'debug' | 'error' | 'info' | 'log' | 'warn',\n  context: { group: string; groups: string[] }\n) =\u003e boolean\n```\n\nThis function is called for every console method supported by this utility.\nIf `true` is returned, the message will not show in the console and the test won't fail.\n\nExample:\n\n```ts\nfailOnConsole({\n  silenceMessage: (errorMessage) =\u003e {\n    if (/Not implemented: navigation/.test(errorMessage)) {\n      return true\n    }\n    return false\n  },\n})\n```\n\n### skipTest\n\nUse this if you want to ignore checks introduced by this library for specific tests determined by\nthe return of the callback function. Return `false` if you do not want to skip console checks for\nthe specific test and return `true` if you would like to skip it.\n\n```ts\nconst ignoreList = [/.*components\\/SomeComponent.test.tsx/]\nconst ignoreNameList = ['some component some test name']\n\nfailOnConsole({\n  skipTest: ({ testPath, testName }) =\u003e {\n    for (const pathExp of ignoreList) {\n      const result = pathExp.test(testPath)\n      if (result) return true\n    }\n\n    if (ignoreNameList.includes(testName)) {\n      return true\n    }\n\n    return false\n  },\n})\n```\n\n### shouldPrintMessage\n\nUse this to print the message immediately when called not awaiting the test to finish. This is useful to show the message if there are\nother or earlier test failures which will result in the fail on console error to be hidden by jest.\n\n- Type: `boolean`\n- Default: `false`\n\n## License\n\n[MIT](https://github.com/ValentinH/jest-fail-on-console/blob/master/LICENSE)\n\n## Maintainers\n\nThis project is maintained by Valentin Hervieu.\n\nThis project was originally part of [@ricardo-ch](https://github.com/ricardo-ch/) organisation because I (Valentin) was working at Ricardo.\nAfter leaving this company, they gracefully accepted to transfer the project to me. ❤️\n\n## Credits\n\nMost of the logic is taken from [React's setupTests file](https://github.com/facebook/react/blob/master/scripts/jest/setupTests.js).\n\n[version-badge]: https://img.shields.io/npm/v/jest-fail-on-console.svg?style=flat-square\n[package]: https://www.npmjs.com/package/jest-fail-on-console\n[downloads-badge]: https://img.shields.io/npm/dm/jest-fail-on-console.svg?style=flat-square\n[npmstats]: http://npm-stat.com/charts.html?package=jest-fail-on-console\n[npmstats-badge]: https://img.shields.io/npm/dm/jest-fail-on-console.svg?style=flat-square\n[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square\n[license]: https://github.com/ValentinH/jest-fail-on-console/blob/master/LICENSE\n[prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square\n[prs]: http://makeapullrequest.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FValentinH%2Fjest-fail-on-console","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FValentinH%2Fjest-fail-on-console","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FValentinH%2Fjest-fail-on-console/lists"}