Adds expect.assertions(n) and expect.hasAssertions to all tests automatically
Problem
Ever wondered if your tests are actually running their assertions, especially in asynchronous tests? Jest has two features
built in to help with this: expect.assertions(number)
and expect.hasAssertions(). These can be
useful when doing something like:
The issue here is the catch case is not dealt with in this test, which is fine as we are testing the happy path,
but this test will currently pass even though the Promise rejects and the assertion is never ran.
Solution
One solution is to manually adjust the above test to include expect.assertions(number) and expect.hasAssertions()
this is quite verbose and prone to human error.
An alternative is a babel plugin to automate adding these additional properties, and this is such plugin 😉
Installation
With npm:
npm install --save-dev babel-jest-assertions
With yarn:
yarn add -D babel-jest-assertions
Setup
.babelrc
{
"plugins":["babel-jest-assertions"]
}
CLI
babel --plugins babel-jest-assertions script.js
Node
require('babel-core').transform('code',{
plugins:['babel-jest-assertions'],
})
Usage
Simply write your tests as you would normally and this plugin will add the verification of assertions in the background.
If you have expectations inside either of beforeEach or afterEach blocks for your test then these expects will be
included in the count - even if you have nested describe blocks each with their own beforeEach/afterEach the count
will accumulate.