{"id":13769569,"url":"https://github.com/mattphillips/babel-jest-assertions","last_synced_at":"2025-08-20T18:32:09.458Z","repository":{"id":46938719,"uuid":"103391968","full_name":"mattphillips/babel-jest-assertions","owner":"mattphillips","description":"Babel Plugin that adds safety to your tests by verifying assertions are actually ran 🃏⁉️","archived":false,"fork":false,"pushed_at":"2023-01-11T18:46:46.000Z","size":602,"stargazers_count":96,"open_issues_count":21,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-09T17:43:53.622Z","etag":null,"topics":["assertion","babel","babel-plugin","jest","testing"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/babel-jest-assertions","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/mattphillips.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":["mattphillips"]}},"created_at":"2017-09-13T11:33:58.000Z","updated_at":"2023-12-19T17:41:01.000Z","dependencies_parsed_at":"2023-02-09T04:17:40.835Z","dependency_job_id":null,"html_url":"https://github.com/mattphillips/babel-jest-assertions","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fbabel-jest-assertions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fbabel-jest-assertions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fbabel-jest-assertions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattphillips%2Fbabel-jest-assertions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattphillips","download_url":"https://codeload.github.com/mattphillips/babel-jest-assertions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230231405,"owners_count":18193930,"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":["assertion","babel","babel-plugin","jest","testing"],"created_at":"2024-08-03T17:00:28.620Z","updated_at":"2024-12-19T14:06:45.581Z","avatar_url":"https://github.com/mattphillips.png","language":"JavaScript","funding_links":["https://github.com/sponsors/mattphillips"],"categories":["Packages"],"sub_categories":["Library extensions"],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003ebabel-jest-assertions\u003c/h1\u003e\n\n  🃏⁉️\n\n  Adds expect.assertions(n) and expect.hasAssertions to all tests automatically\n\u003c/div\u003e\n\n\u003chr /\u003e\n\n[![Build Status](https://img.shields.io/travis/mattphillips/babel-jest-assertions.svg?style=flat-square)](https://travis-ci.org/mattphillips/babel-jest-assertions)\n[![Code Coverage](https://img.shields.io/codecov/c/github/mattphillips/babel-jest-assertions.svg?style=flat-square)](https://codecov.io/github/mattphillips/babel-jest-assertions)\n[![version](https://img.shields.io/npm/v/babel-jest-assertions.svg?style=flat-square)](https://www.npmjs.com/package/babel-jest-assertions)\n[![downloads](https://img.shields.io/npm/dm/babel-jest-assertions.svg?style=flat-square)](http://npm-stat.com/charts.html?package=babel-jest-assertions\u0026from=2017-09-14)\n[![MIT License](https://img.shields.io/npm/l/babel-jest-assertions.svg?style=flat-square)](https://github.com/mattphillips/babel-jest-assertions/blob/master/LICENSE)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)\n[![Roadmap](https://img.shields.io/badge/%F0%9F%93%94-roadmap-CD9523.svg?style=flat-square)](https://github.com/mattphillips/babel-jest-assertions/blob/master/docs/ROADMAP.md)\n[![Examples](https://img.shields.io/badge/%F0%9F%92%A1-examples-ff615b.svg?style=flat-square)](https://github.com/mattphillips/babel-jest-assertions/blob/master/docs/EXAMPLES.md)\n\n## Problem\n\nEver wondered if your tests are actually running their assertions, especially in asynchronous tests? Jest has two features\nbuilt in to help with this: [`expect.assertions(number)`](https://facebook.github.io/jest/docs/en/expect.html#expectassertionsnumber)\nand [`expect.hasAssertions()`](https://facebook.github.io/jest/docs/en/expect.html#expecthasassertions). These can be\nuseful when doing something like:\n\n```js\nit('resolves to one', () =\u003e {\n  Promise.reject(1).then(value =\u003e expect(value).toBe(1));\n});\n```\n\nThe issue here is the `catch` case is not dealt with in this test, _which is fine as we are testing the happy path_,\nbut this test will currently pass even though the `Promise` rejects and the assertion is never ran.\n\n## Solution\n\nOne solution is to manually adjust the above test to include `expect.assertions(number)` and `expect.hasAssertions()`\nthis is quite verbose and prone to human error.\n\nAn alternative is a babel plugin to automate adding these additional properties, and this is such plugin 😉\n\n## Installation\n\nWith npm:\n```sh\nnpm install --save-dev babel-jest-assertions\n```\n\nWith yarn:\n```sh\nyarn add -D babel-jest-assertions\n```\n\n## Setup\n\n### .babelrc\n\n```json\n{\n  \"plugins\": [\"babel-jest-assertions\"]\n}\n```\n\n### CLI\n\n```sh\nbabel --plugins babel-jest-assertions script.js\n```\n\n### Node\n\n```javascript\nrequire('babel-core').transform('code', {\n  plugins: ['babel-jest-assertions'],\n})\n```\n\n## Usage\n\nSimply write your tests as you would normally and this plugin will add the verification of assertions in the background.\n\n**One assertion**\n```js\nit('resolves to one', () =\u003e {\n  Promise.reject(1).then(value =\u003e expect(value).toBe(1));\n});\n```\n\n`↓ ↓ ↓ ↓ ↓ ↓`\n\n```js\nit('resolves to one', () =\u003e {\n  expect.hasAssertions();\n  expect.assertions(1);\n  Promise.reject(1).then(value =\u003e expect(value).toBe(1));\n});\n```\n_Note_: this test will now fail 🎉\n\n**Multiple assertions**\n```js\nit('counts multiple assertions too', () =\u003e {\n  expect(1 + 0).toBe(1);\n  expect(0 + 1).toBe(1);\n});\n```\n\n`↓ ↓ ↓ ↓ ↓ ↓`\n\n```js\nit('counts multiple assertions too', () =\u003e {\n  expect.hasAssertions();\n  expect.assertions(2);\n  expect(1 + 0).toBe(1);\n  expect(0 + 1).toBe(1);\n});\n```\n\n**Asynchronous assertions**\n```js\nit('counts multiple assertions too', async () =\u003e {\n  const res = await fetch('www.example.com');\n  expect(res.json).toBeTruthy();\n  const json = await res.json();\n  expect(json).toEqual({ whatever: 'trevor' });\n});\n```\n\n`↓ ↓ ↓ ↓ ↓ ↓`\n\n```js\nit('counts multiple assertions too', async () =\u003e {\n  expect.hasAssertions();\n  expect.assertions(2);\n  const res = await fetch('www.example.com');\n  expect(res.json).toBeTruthy();\n  const json = await res.json();\n  expect(json).toEqual({ whatever: 'trevor' });\n});\n```\n\n**beforeEach and afterEach blocks**\n\nIf you have expectations inside either of `beforeEach` or `afterEach` blocks for your test then these expects will be\nincluded in the count - even if you have nested describe blocks each with their own `beforeEach`/`afterEach` the count\nwill accumulate.\n\n```js\nbeforeEach(() =\u003e {\n  expect(true).toBe(true);\n});\n\nafterEach(() =\u003e {\n  expect(true).toBe(true);\n});\n\ndescribe('.add', () =\u003e {\n  beforeEach(() =\u003e {\n    expect(true).toBe(true);\n  });\n  afterEach(() =\u003e {\n    expect(true).toBe(true);\n  });\n  it('returns 1 when given 0 and 1', () =\u003e {\n    expect(add(1, 0)).toEqual(1);\n  });\n\n  describe('.add2', () =\u003e {\n    beforeEach(() =\u003e {\n      expect(true).toBe(true);\n    });\n    afterEach(() =\u003e {\n      expect(true).toBe(true);\n    });\n    it('returns 1 when given 0 and 1', () =\u003e {\n      expect(add2(1, 0)).toEqual(1);\n    });\n  });\n});\n\n      ↓ ↓ ↓ ↓ ↓ ↓\n\nbeforeEach(() =\u003e {\n  expect(true).toBe(true);\n});\n\nafterEach(() =\u003e {\n  expect(true).toBe(true);\n});\n\ndescribe('.add', () =\u003e {\n  beforeEach(() =\u003e {\n    expect(true).toBe(true);\n  });\n  afterEach(() =\u003e {\n    expect(true).toBe(true);\n  });\n  it('returns 1 when given 0 and 1', () =\u003e {\n    expect.assertions(5);\n    expect.hasAssertions();\n\n    expect(add2(1, 0)).toEqual(1);\n  });\n\n  describe('.add2', () =\u003e {\n    beforeEach(() =\u003e {\n      expect(true).toBe(true);\n    });\n    afterEach(() =\u003e {\n      expect(true).toBe(true);\n    });\n    it('returns 1 when given 0 and 1', () =\u003e {\n      expect.assertions(7);\n      expect.hasAssertions();\n\n      expect(add2(1, 0)).toEqual(1);\n    });\n  });\n});\n```\n\n**Comments are ignored**\n```js\nit('ignores commented-out assertions', async () =\u003e {\n  const res = await fetch('www.example.com');\n  // expect(res.json).toBeTruthy();\n  const json = await res.json();\n  /*\n    expect(json).toEqual({ whatever: 'trevor1' });\n  */\n  expect(json).toEqual({ whatever: 'trevor' });\n  /* expect(json).toEqual({ whatever: 'trevor2' }); */\n});\n```\n\n`↓ ↓ ↓ ↓ ↓ ↓`\n\n```js\nit('counts multiple assertions too', async () =\u003e {\n  expect.hasAssertions();\n  expect.assertions(1);\n  const res = await fetch('www.example.com');\n  // expect(res.json).toBeTruthy();\n  const json = await res.json();\n  /*\n    expect(json).toEqual({ whatever: 'trevor1' });\n  */\n  expect(json).toEqual({ whatever: 'trevor' });\n  /* expect(json).toEqual({ whatever: 'trevor2' }); */\n});\n```\n\n### Override\n\nIf you add either `expect.assertions(number)` or `expect.hasAssertions()` then your defaults will be favoured and the\nplugin will skip the test.\n\n```js\nit('will leave test as override supplied', () =\u003e {\n  expect.hasAssertions();\n  expect.assertions(1);\n\n  if (true) {\n    expect(true).toBe(true);\n  }\n\n  if (false) {\n    expect(false).toBe(false);\n  }\n});\n```\n\n`↓ ↓ ↓ ↓ ↓ ↓`\n\n```js\nit('will leave test as override supplied', () =\u003e {\n  expect.hasAssertions();\n  expect.assertions(1);\n\n  if (true) {\n    expect(true).toBe(true);\n  }\n\n  if (false) {\n    expect(false).toBe(false);\n  }\n});\n```\n\n## Contributors\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n| [\u003cimg src=\"https://avatars0.githubusercontent.com/u/5610087?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003eMatt Phillips\u003c/sub\u003e](http://mattphillips.io)\u003cbr /\u003e[💻](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips \"Code\") [📖](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips \"Documentation\") [🚇](#infra-mattphillips \"Infrastructure (Hosting, Build-Tools, etc)\") [⚠️](https://github.com/mattphillips/babel-jest-assertions/commits?author=mattphillips \"Tests\") | [\u003cimg src=\"https://avatars0.githubusercontent.com/u/266594?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003eRamesh Nair\u003c/sub\u003e](https://hiddentao.com/)\u003cbr /\u003e[💻](https://github.com/mattphillips/babel-jest-assertions/commits?author=hiddentao \"Code\") [📖](https://github.com/mattphillips/babel-jest-assertions/commits?author=hiddentao \"Documentation\") [💡](#example-hiddentao \"Examples\") [⚠️](https://github.com/mattphillips/babel-jest-assertions/commits?author=hiddentao \"Tests\") | [\u003cimg src=\"https://avatars1.githubusercontent.com/u/7352279?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003eHuy Nguyen\u003c/sub\u003e](https://www.huy-nguyen.com/)\u003cbr /\u003e[🐛](https://github.com/mattphillips/babel-jest-assertions/issues?q=author%3Ahuy-nguyen \"Bug reports\") | [\u003cimg src=\"https://avatars2.githubusercontent.com/u/923865?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003eSimon Boudrias\u003c/sub\u003e](http://simonboudrias.com)\u003cbr /\u003e[🐛](https://github.com/mattphillips/babel-jest-assertions/issues?q=author%3ASBoudrias \"Bug reports\") | [\u003cimg src=\"https://avatars3.githubusercontent.com/u/711311?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003eGiuseppe\u003c/sub\u003e](http://giuseppe.pizza)\u003cbr /\u003e[🤔](#ideas-giuseppeg \"Ideas, Planning, \u0026 Feedback\") |\n| :---: | :---: | :---: | :---: | :---: |\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\n## LICENSE\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattphillips%2Fbabel-jest-assertions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattphillips%2Fbabel-jest-assertions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattphillips%2Fbabel-jest-assertions/lists"}