Jest 11.0
Today we're announcing a switch to major revisions for Jest with Jest 11.0 being the first major release. Jest has been used by Facebook engineers and on our continuous integration systems for years and we believe Jest has been way beyond a “1.0 release” for a long time. This is similar to a change the React team has made.
If you are using Jest 0.9 or Jest 0.10 the upgrade should be seamless. All changes from the last few months were rolled into Jest 11.0.
New in Jest 11.0
Babel Integration and Simplified Setup
babel-jest
was adopted within the newly modularized Jest repository and it is now seamlessly integrated into Jest. If you are upgrading from an older version of Jest or are looking to adopt Jest, we recommend reading the Getting Started guide.
Previously Jest provided APIs such as jest.dontMock
which unmocks a module that is subsequently being required using the require
function. Testing code usually looked like this:
jest.dontMock('LikeButton');
const LikeButton = require('LikeButton'); // LikeButton is unmocked
However, together with ES2015 import statements this will no longer work. Per the specification import
s are hoisted to the top of their code block. Code written like this:
jest.dontMock('LikeButton');
import LikeButton from 'LikeButton';
when executed, would actually be run in this order:
import LikeButton from 'LikeButton'; // This happens before the dontMock call.
jest.dontMock('LikeButton');
The LikeButton module would therefore be mocked even though we explicitly call dontMock
.
When the latest versions of Jest and babel-jest are used together, calls to the new APIs jest.unmock
, jest.mock
, jest.disableAutomock
and jest.enableAutomock
are hoisted to the top of their block, before ES2015 import statements.
jest.unmock('LikeButton');
import LikeButton from 'LikeButton'; // LikeButton is properly unmocked!
(Auto)Mocking Improvements
We have made numerous improvements and bug fixes to Jest's automocking feature, improved npm3 support and added new manual mocking APIs. Many people have expressed a desire use Jest with the automocking feature disabled. A global configuration option automock
, which can be set to false
, was added.
We have also added two new APIs to simplify manual mocks. jest.mock
specifies a manual mock factory for a specific test:
// Implement a mock for a hypothetical "sum" module.
jest.mock('sum', () => {
return (a, b) => a + b;
});
const sum = require('sum');
sum(1, 4); // 5
And jest.fn
was added to make it easier to create mock functions:
// Create a mock function
const mockFn = jest.fn(() => 42);
mockFn(); // 42
expect(mockFn.calls.length).toBe(1);