网页
综合
更新时间 2025-06-04 00:00:00
Blog · Jest
自动蜘蛛
jestjs.io
正文约 5999 字
Today we are happy to announce the release of Jest 30. This release features a substantial number of changes, fixes, and improvements. While it is one of the largest major releases of Jest ever, we admit that three years for a major release is too long. In the future, we are aiming to make more frequent major releases to keep Jest great for the next decade.
If you want to skip all the news and just get going, run npm install jest@^30.0.0 and follow the migration guide: Upgrading from Jest 29 to 30.
Jest 30 is noticeably faster, uses less memory, and comes with tons of new features. First, let’s take a look at the breaking changes:
These are just some of the highlights. The full list of breaking changes can be found in the CHANGELOG and the Jest 30 migration guide.
Jest 30 delivers real-world performance gains thanks to many optimizations, especially related to module resolution, memory usage, and test isolation. By relying on the new unrs-resolver, module resolution in Jest became more feature-rich, standards-compliant, and faster. Thanks to @JounQin for the migration. Depending on your project, you may see significantly faster test runs and reduced memory consumption. For example, one large TypeScript app with a client and server observed 37% faster test runs and 77% lower memory usage in one part of their codebase:
Jest is fast, but due to Jest's test isolation, slow user code often exacerbates performance issues and leads to slow test runs. When tests leave behind open handles like unclosed timers or connections to other services, it can cause Jest to hang or slow down. Jest 30 has gotten better at detecting and reporting these issues, which helps you identify and fix slow or problematic tests more easily. For example, tests at Happo were sped up by 50% from 14 minutes down to 9 minutes by cleaning up open handles and upgrading to Jest 30.
If you are using files that consolidate the exports of multiple modules into a single file (i.e. "barrel files"), we recommend using tools such as babel-jest-boost, babel-plugin-transform-barrels or no-barrel-file to avoid loading large swaths of application code for each test file. This can lead to performance improvements of up to 100x.
Jest achieves test isolation between files by running each test in a separate VM context, giving each file a fresh global environment. However, if your code does not clean up globals after each test file, it can lead to memory leaks across Jest and slow down your test runs. Jest 30 introduces a new feature that notifies you about globals that are not correctly cleaned up after a test run.
In the future, Jest will automatically clean up globals after each test run. If you don't get any warnings about uncleaned globals with Jest 30, you can already set the globals cleanup mode to "on" now to enable this feature fully, and benefit from major memory savings and performance improvements:
The default in Jest is globalsCleanup: 'soft'. To disable this feature you can set it to off. If you need to protect specific global objects from being cleaned up -- for example, shared utilities or caches -- you can mark them as protected using jest-util:
Thanks to @eyalroth for implementing this feature!
Support for import.meta.* and file:// was added when using native ESM with Jest. In addition, you can now write your Jest config files in TypeScript, and .mts and .cts files are natively supported without requiring extra configuration. If you are using Node’s native TypeScript type stripping feature, we no longer load the TypeScript transformer to strip types, leading to faster test runs.
You can now use JavaScript’s new explicit resource management syntax (using) with Jest spies. If your environment supports it, writing using jest.spyOn(obj, 'method') will automatically restore the spy when the block ends, so you don’t have to manually clean up.
Jest 30 introduces a new asymmetric matcher, expect.arrayOf, which lets you validate every element of an array against a condition or type. For instance, you can expect an array of numbers ensuring all items are numbers:
If you use data-driven tests with test.each, you can now include a special placeholder %$ in your test titles to inject the number of the test case. For example:
will replace %$ with the test’s sequence number.
@sinonjs/fake-timers was upgraded to v13, adding jest.advanceTimersToNextFrame(). This new function allows you to advance all pending requestAnimationFrame callbacks to the next frame boundary, making it easier to test animations or code that relies on requestAnimationFrame without having to guess millisecond durations.
Jest 30 enhances jest.retryTimes() with new options that give you control over how retries are handled. You can specify a delay or immediately retry a failed test instead of waiting until the entire test suite finishes:
Jest 30 adds new experimental jest.unstable_unmockModule() API for finer control when unmocking modules (especially when using native ESM).
A new onGenerateMock method was added. It registers a callback function that is invoked whenever Jest generates a mock for a module. This callback allows you to modify a mock before it is returned to your test environment:
Jest’s matcher utilities now support defining a static SERIALIZABLE_PROPERTIES on custom objects. This allows you to control which properties of a custom object are included in snapshots and error messages, making the output more focused and relevant.
Test files listed in setupFilesAfterEnv can now export an async function or use top-level await similar to setupFiles.
Check out the full CHANGELOG for all changes, improvements and new features.
jsdom has made changes to become more spec compliant. This might break some use cases, most notably mocking window.location in tests. Jest now ships with @jest/environment-jsdom-abstract to make it easier for you to compose your own custom test environment based on jsdom. If you are just looking to patch jsdom, you can apply this jsdom patch
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。