Jest spends half its time importing jest-snapshot even when it isn’t used
See original GitHub issue🐛 Bug Report
I did some profiling on a directory of 500 empty tests and discovered that Jest is spending 50% of its time importing jest-snapshot into every test environment. Then I wrote a quick and dirty patch that rips jest-snapshot out of jest-jasmine2 and jest-runtime, and observed that it makes my tests run twice as fast (39.706s → 19.941s)!
Obviously we can’t actually rip jest-snapshot out, but if we could import it lazily, we could get a large speedup on projects with many small test files that don’t use snapshot testing.
To Reproduce
In an empty directory:
mkdir __tests__
for i in {000..499}; do echo "test(\"test $i\", () => {});" > __tests__/test$i.js; done
echo '{ "testEnvironment": "node" }' > jest.config.json
jest --runInBand
(Running tests serially with --runInBand gave me much more stable benchmark results.)
Expected behavior
Jest should be faster! 🙂
Link to repl or repo (highly encouraged)
https://github.com/andersk/500-empty-jest-tests
envinfo
System:
OS: Linux 5.5 NixOS 20.03 (Markhor) 20.03pre212208.8130f3c1c2b (Markhor)
CPU: (12) x64 Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz
Binaries:
Node: 12.15.0 - ~/.nix-profile/bin/node
Yarn: 1.22.0 - ~/.yarn/bin/yarn
npm: 6.13.4 - ~/.nix-profile/bin/npm
npmPackages:
jest: ^25.1.0 => 25.1.0
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Jest snapshot is redundant - Stack Overflow
I am writing snapshot tests using Jest for a node.js and React app and have installed snapshot-tools extension in VS code. Some of...
Read more >What I Learned at Work this Week: Updating a Jest Snapshot
Ease of updating means we can more easily add a mistake to our code. There's a single-line command to automatically update our Jest...
Read more >The case against React snapshot testing - ezCater
One of the recent strategies we've tried out is Jest snapshot testing. ... After spending a little over half a year using it...
Read more >Cypress Screenshots for React Components
A tutorial about replacing Jest Snapshots With Cypress ... It isn't even on the big chart in the 2020 State of JavaScript results....
Read more >jest-snapshot | Yarn - Package Manager
Jest will set process.env.NODE_ENV to 'test' if it's not set to something else. You can use that in your configuration to conditionally setup...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yeah, ignoring the blacklist for
@babel/plugin-transform-modules-commonjssimilarly halves the running time on this benchmark:So now I’m curious, what exactly does this blacklist accomplish in the way of preventing test code from messing with Jest internals? It means that lots of Jest code gets loaded before the test code has a chance to run—but the loaded code still refers to globals that the test code could interfere with, so how does that help?
This seems to still be pretty slow. But it looks like at this point, the toplevel import is okay as long as it’s a
requireOutside(which can be cached across test cases); the issue is that there’s also a regular import. Removing that gets the original repro (with--runInBand) from ~40s to ~25s on my laptop, which is almost as much as ripping out jest-snapshot entirely. If we could do the same for a bunch of other plausibly-stateless packages used by jest itself [1], that can get us down to at least 13s, although I’m not certain how safe that is.[1] I’m testing with
'expect', 'semver', 'picomatch', 'micromatch', 'jest-diff', 'source-map-support', 'jest-matcher-utils', '@jest/expect', 'jest-snapshot'('chalk'is already required outside the VM).