Jest 28 is finally here, and it comes with some long requested features such as support for sharding a test run across multiple machines, package exports
and the ability to customize the behavior of fake timers. These are just some personal highlights, and we'll be highlighting more in this blog post.
Additionally, as announced in the Jest 27 blog post last year, we have removed some packages that no longer are used by default from the default installation. As a result the installation size has dropped by about 1/3.
Breaking changes
The list of breaking changes is long (and can be seen fully in the changelog), but for migration purposes, we've also written a guide you can follow. Hopefully this makes the upgrade experience as frictionless as possible!
Main breaking changes likely to impact your migration are dropped support for Node 10 and 15 (but not Node 12, which will be EOL in a few days) and some renamed configuration options.
Please note that both of the removed modules (jest-environment-jsdom
and jest-jasmine2
) are still actively maintained and tested in the same way, so the only breaking change here is that you'll need to explicitly install them.
The guide should hopefully make migration trivial, but note that if you use any of the packages Jest consists of directly (such as jest-worker
or pretty-format
), instead of just running jest
, then you need to go through the changelog to view any breaking changes.
Features
Now let's talk about the new features in Jest 28, which is way more exciting! And there's quite a few of them, so buckle up.
Sharding of test run
Jest now includes a new --shard
CLI option, contributed by Mario Nebl. It allows you to run parts of your test across different machine, and has been one of Jest's oldest feature requests.
Jest's own test suite on CI went from about 10 minutes to 3 on Ubuntu, and on Windows from 20 minutes to 7.
package.json
exports
Jest shipped minimal support of exports
in 27.3. However, it only supported the "main" entry point (.
), and only if no main
field was present in package.json
. With Jest 28 we're excited to finally be shipping full support!
Related, in Jest 27, we provided either require
or import
condition. In Jest 28, jest-environment-node
will now automatically provide node
and node-addons
conditions, while jest-environment-jsdom
will provide the browser
condition.
This has been one of the biggest compatibility issues of Jest, and hopefully this is now resolved once and for all.
Fake timers
Jest 26 introduced the concept of "modern" fake timers, which uses @sinonjs/fake-timers
under the hood, and Jest 27 made it the default. In Jest 28, we are now exposing more of the underlying implementation through both configuration and runtime APIs. Huge thanks to Tom Mrazauskas who contributed this feature!
This allows you to not mock out process.nextTick
which improves compatibility with fake Promise
s, or to enable advanceTimers
which automatically advance timers.
Please see the fakeTimers
configuration for details.
GitHub Actions Reporter
Jest now ships with a reporter to be used on GitHub Actions, which will use annotations to print test errors inline.
You can activate this reporter by passing github-actions
in the reporters
configuration option.
Huge thanks to Bernie Reiter and other contributors for sticking by us and finally landing this feature.
Inline testEnvironmentOptions
You can now pass testEnvironmentOptions
inline in a file, similar to how you can set test environment. This is useful if you want to e.g. change the URL in a single file.
/**
* @jest-environment jsdom
* @jest-environment-options {"url": "https://jestjs.io/"}
*/
test('use jsdom and set the URL in this test file', () => {
expect(window.location.href).toBe('https://jestjs.io/');
});
All Node.js globals
If you are using the new fetch
implementation in Node v18, you might have noticed that this function is not available in Jest. It has been a long-standing issue that we have to manually copy over any globals into the test globals. With Jest 28, this is no longer an issue as we now inspect the global environment Jest itself is running in, and copy over any globals that are missing in the test environment.