Registry / testing / babel-jest-assertions

babel-jest-assertions

JSON →
library0.1.0jsnpmunverified

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.

npm install babel-jest-assertions
INSTALL
IMPORT
SIG · BABEL-JEST-ASSERTI
B
babel-jest-assertions
testingjavascriptv0.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

(plugin)
Add `'babel-jest-assertions'` to your `.babelrc` plugins array
import babelJestAssertions from 'babel-jest-assertions'
This is a Babel plugin, not a runtime module. Use in Babel configuration only.

Shows Babel plugin configuration and how the plugin transforms a test with two assertions.

// .babelrc { "plugins": ["babel-jest-assertions"] } // test.js it('counts assertions', () => { expect(1 + 0).toBe(1); expect(0 + 1).toBe(1); }); // Transforms to: // it('counts assertions', () => { // expect.hasAssertions(); // expect.assertions(2); // expect(1 + 0).toBe(1); // expect(0 + 1).toBe(1); // });
Debug
Known issues
gotchaPlugin counts `expect` calls, not actual assertions; non-standard matcher chains may be miscounted.
fix
Manually add `expect.assertions(n)` if the count is off, or avoid complex expect chains.
affects: >=0.0.1
gotchaDoes not support dynamically computed assertion counts; `expect.assertions` uses a static integer.
fix
Use manual `expect.assertions(0)` or disable plugin for dynamic cases.
affects: >=0.0.1
breakingIn 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.
fix
Review tests that have assertions in lifecycle hooks; adjust expected counts or add assertions.
affects: >=0.1.0
gotchaPlugin may not work with custom test wrapper functions (e.g., `describe.each`) if they don't match internal patterns.
fix
Ensure test blocks use standard `it`, `test`, `xit`, `fit`, `it.only` only.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: babel is not a function
Using plugin with Babel 7+ without updating Babel API
fix
Downgrade to Babel 6 or use the Babel 7 compatible fork of this plugin.
ReferenceError: expect is not defined
Plugin inserts `expect.assertions` but `expect` is not in scope (e.g., missing `@jest/globals` import in ESM).
fix
Add `import { expect } from '@jest/globals';` to test files or configure Jest globals properly.
The plugin "babel-jest-assertions" didn't return a function.
Using plugin string in Babel config but plugin module not installed.
fix
Run `npm install --save-dev babel-jest-assertions` and ensure it's in `node_modules`.
Upgrade
Version history
0.1.0latest on npm
Audit
Dependencies
babel-corerequiredRequired at runtime to transform code via Babel plugin API
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
2
Resources
babel-jest-assertions — npm install babel-jest-assertions · libregistry