{"id":20986,"library":"babel-jest-assertions","title":"babel-jest-assertions","description":"A Babel plugin (v0.1.0) that automatically inserts `expect.assertions(n)` and `expect.hasAssertions()` into Jest test blocks by counting `expect` calls. It supports sync, async, arrow, named, and only tests, including `beforeEach`/`afterEach` blocks. Unlike manual insertion, it eliminates human error and verbosity. Current stable version is 0.1.0; no major releases planned. Minimal alternative to manual assertion count management.","status":"active","version":"0.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/mattphillips/babel-jest-assertions","tags":["javascript","babel","jest","plugin","assertions","expect","test"],"install":[{"cmd":"npm install babel-jest-assertions","lang":"bash","label":"npm"},{"cmd":"yarn add babel-jest-assertions","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-jest-assertions","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required at runtime to transform code via Babel plugin API","package":"babel-core","optional":false}],"imports":[{"note":"This is a Babel plugin, not a runtime module. Use in Babel configuration only.","wrong":"import babelJestAssertions from 'babel-jest-assertions'","symbol":"(plugin)","correct":"Add `'babel-jest-assertions'` to your `.babelrc` plugins array"}],"quickstart":{"code":"// .babelrc\n{\n  \"plugins\": [\"babel-jest-assertions\"]\n}\n\n// test.js\nit('counts assertions', () => {\n  expect(1 + 0).toBe(1);\n  expect(0 + 1).toBe(1);\n});\n// Transforms to:\n// it('counts assertions', () => {\n//   expect.hasAssertions();\n//   expect.assertions(2);\n//   expect(1 + 0).toBe(1);\n//   expect(0 + 1).toBe(1);\n// });","lang":"javascript","description":"Shows Babel plugin configuration and how the plugin transforms a test with two assertions."},"warnings":[{"fix":"Manually add `expect.assertions(n)` if the count is off, or avoid complex expect chains.","message":"Plugin counts `expect` calls, not actual assertions; non-standard matcher chains may be miscounted.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Use manual `expect.assertions(0)` or disable plugin for dynamic cases.","message":"Does not support dynamically computed assertion counts; `expect.assertions` uses a static integer.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Review tests that have assertions in lifecycle hooks; adjust expected counts or add assertions.","message":"In v0.1.0, support for counting `expect` in `beforeEach` and `afterEach` was added; existing tests may fail if those blocks contain assertions they didn't before.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Ensure test blocks use standard `it`, `test`, `xit`, `fit`, `it.only` only.","message":"Plugin may not work with custom test wrapper functions (e.g., `describe.each`) if they don't match internal patterns.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Downgrade to Babel 6 or use the Babel 7 compatible fork of this plugin.","cause":"Using plugin with Babel 7+ without updating Babel API","error":"TypeError: babel is not a function"},{"fix":"Add `import { expect } from '@jest/globals';` to test files or configure Jest globals properly.","cause":"Plugin inserts `expect.assertions` but `expect` is not in scope (e.g., missing `@jest/globals` import in ESM).","error":"ReferenceError: expect is not defined"},{"fix":"Run `npm install --save-dev babel-jest-assertions` and ensure it's in `node_modules`.","cause":"Using plugin string in Babel config but plugin module not installed.","error":"The plugin \"babel-jest-assertions\" didn't return a function."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}