jest: Jest is 3x slower on all windows machines (Windows 10 and lower)
š Bug Report
Jest is slow on windows machines.
To Reproduce
Anyone with a windows desktop machine.
Expected behavior
It should be lightning fast.
Run npx envinfo --preset jest
Paste the results here:
System:
OS: Windows 10
CPU: (4) x64 Intel(R) Core(TM) i5-7400 CPU @ 3.00GHz
Binaries:
npm: 6.2.0 - C:\Program Files\nodejs\npm.CMD
Iāve done a ton of reading around and it seems like 100% of all windows users are being affected by delaying in running tests with jest, while it is blazingly fast for all mac users.
Has there been any research or attempts to find what is causing this? Currently Iām copy and pasting all of my components and unit testing them in codesandbox, (It instantly runs tests blazingly fast) then copy and pasting them back into my project, which isnāt the most ideal way to do it but I love the API that jest offers.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 118
- Comments: 106 (19 by maintainers)
Hello, Iām experiencing same issue in here. I have a windows machine and WSL. I copied my project files to WSL to test this behavior. Here are the runs of same two basic tests: Windows 10(Version 2004): WSL 2(Ubuntu 20.04):
The tests are very simple:
The project is created with CRA, so there is no configuration to botch the settings, I added nothing in terms of Jest. The same tests run blazingly fast on mocha, almost instantly. Iām trying to set up a testing environment for our project, and I would really like to use Jest, but it seems as I add more and more tests, the test suite will be slower and slower it seems. Each tests seems to be adding 0.8 seconds, which is ridiculous, since they do nothing. Something is messing with test execution on windows, and I donāt know what. The problem was worse, a single test would take around 15 seconds, but when I added --runInBand and , the situation seemed to improve a little bit, but I think it is still not enough, considering mocha watch mode is instant.
Yarn is version 1.22.5, all other npm dependencies(like react and react-scripts) are latest.
I disabled the anti-virus and windows defender to see if it has any effect, but it doesnāt. Also, I disabled indexing for my project folder, to no avail neither.
Edit: I tried pressing enter, and the tests were as fast as on WSL:
Now Iām really confused š
But this seems still very slow, since it seems each test takes 0.3 seconds, which is a lot, considering they do nothing. I expect this suite to be completed under 0.1 seconds.
Edit 2: When I added 100 tests to my test suite, it took around 44 seconds to run them, even if I press the enter to run them:
Same test suites takes around 9-10 seconds on WSL:
Iām on a brand new MacBook Pro. As I have students on both MacOS and Windows 10, I decided to add two more partitions to my drive; one for Windows 10 and one for shared storage using Tuxera NTFS.
I ran into this speed issue today preparing a JavaScript lesson that incorporates unit tests. Iām actually running Jest from MacOS but the code and tests are located on the shared NTFS partition. Even with all suites marked as
describe.skip
, Jest takes more than 10 seconds to complete, both in single-run and watch modes.8 suites 42 tests
I swapped
jest
formocha
andchai
and runs came back down to about 1 second single and 10 milliseconds watch mode.Basically you didnāt read my last post. You wanted to promote
mocha/chai
ā¦ we all know about thisā¦ We are trying to make the regression of jest fixed. Either you help to do this [from my post]...can you try with micromatch 4...
or keep these useless comments out of the thread. Sorry to be direct but at some point there is no other way to say it.On a more positive note, Iād just like to say I owe this bug a great debt of gratitude. It was the final straw that made me decide to switch to Linux for my main development workflow and Iāve never been happier. I canāt say Iād never go back because sometimes I have to work on legacy projects, but with ASP.NET core being cross-platform, the reasons for booting back into windows are becoming fewer all the time.
It is slow for both single and multiple files, with or without watch mode. Pretty much every time it runs any test there is a 3+ second delay on initializing the tests, and it is slow running the tests one by one by 0.3 or 0.4 or 0.5 seconds each while other test runners like mocha/chai would usually just run each as if it feels like 0.05 seconds each.
I use jest in codesandbox and they seem to run jest instantly on initialization/running tests, I watched my coworkers run jest on their mac machines and they run it instantly like normally. Itās just windows machines as far as I know. I use a windows machine at work and jest is having the slow problem there, and I also use a windows machine at home and the problem continues here.
I used --runInBand but it seemed to have slightly slowed down the unit tests even further by an additional 0.2 seconds each, based on feeling.
I have to disagree even if I know you tried to be funny. No other runners suffer from this issue. So even if the issue would come from NTFS behavior itās still a Jest bug.
That being said, it might be due to difference in file handles. If Jest holds on to file handles too long it would slow down execution a lot.
Please @timrobinson33 letās stay on topic. There is no reason
jest
should be 68 times slower than any other environment on Windows, considering Node works just fine on any platform. Also might I add, I have not experienced this problem with any other test runner.I would wish so much that someone would at least acknowledge this is a problem. As it is now, my windows desktop machine which has plenty of power (read: much more than my co-workers macbook) is approximately 69 times slower than the macbook when executing tests! This is practically forcing me not to touch the frontend code since it is so inefficient for me to work on those due to Jest tests running so slow. We might have to move away from Jest if this doesnāt get fixed. Everything else is lightning fast but Jest tests are superduper slow. The time is spent on something else than actually executing the tests, when the actual test logic is executed those go really fast.
has anyone tried to migrate to vitest and had much faster tests on windows then?
EDIT: Well I have just tried it and itās super fast! And it even has nice vscode plugin. I was struggling with jest for 2 days. But vitest looks like a good solution for my troubles. Migration was easy, as itās reusing the jest API. I only had to create a ridiculously small config file and run it.
Since 2019 still we donāt have fix for this š¦
so, we all know that issue is stupid NTFS š
This is crazy!
Simply installed jest globally and now the exactly same project runs in 0.9s instead of 52(!!!) seconds!
npm remove jest
in the project, thennpm install -g jest
Of course Iād like to integrate jest as dev dependency into the project again. Any idea why that happens?
@cino893, not a solution š try to find fix not a workaround
Related: #6783
Is it slow starting up, or in watch mode as well? If just during startup, you can try to install
watchman
, that should help (https://facebook.github.io/watchman/docs/install.html)Hi, just thought Iād add some additional information to this discussion. Jest is super slow also when executed inside a Docker container which has the source code and tests shared via volume from the host system (Windows).
Iām pretty certain this slowdown is due to the differences in how Windows handles file handles compared to unix systems. In unix, if a process has a file handle open that does not block other processes from reading that file. In Windows, a process holding a file handle owns that file as long as it releases the handle. I would look into Jest code for file reading logic and especially when and how file handles are released. A well behaving program should anyway release file handles immediately after it has done itās job. Test runner like Jest should have no reason to hold file handle for a long time anyway.
Just mentioning that this will indeed improve test runner performance with WSL when cloning into home folder, but itās not as fast as on native Linux (installed on bare metal, no VM or anything like that) and probably Mac. I ended up switching from Windows 10 to Linux in my client project.
If thatās allowed in your project, I highly recommend, besides Linux is much faster in most cases and every day usage compared to Windows.
So help fix it, this is an open source project. If you canāt contribute code, help adding more details about the root causes.
I also want to mention that @josh-barkerās solution also seems to work in my case. So hereās the present thinking: it seems that as long as Jest is installed under
/home
and not under/mnt
, the performance is fixed.So to summarize, the 2 main workarounds would basically be:
npm install -g jest
and use that instead of a local installation under/mnt
./home
, so that when you runnpm install jest
, itāll be installed under/home
as well.Once again, for reference Iām using Windows 10 Home with WSL v2 and Ubuntu 20.04 LTS.
This most definitely sounds like a virus scanner issue to me. Meaning, your project directory is in the scope of virus scanning which slows down jest to a crawl but your global npm directory is not. This is just a guess though.
Iāve moved to mocha/chai since then but iām surprised this issue is still being worked on.
I tried with both versions at that time of the post.
I just created a new project with one test with simple array push tests and it took more than 10 seconds from start to completion. The project is using react/typescript but the test file is not a react component file but a normal file like a .js. Gif below for visual reference if it makes it better to visualize what the issue might be:
I noticed that the first time I run the test it shows that the test is estimated to be 9 seconds. Once it completes, it randomly retries the tests a second time to completion.
When I took that gif picture above (which was the second time this time), the time estimated cut down a little and it didnāt perform a second retry. Not sure if that is the expected jest behavior.
Here is a gif of me running micromatch 4 with yarn in a separate project:
Using windows 10 and my computer specs are: AMD FXā¢-8320 Eight-Core Processor 3.50GHz 16GB ram x64 SSD
@Southclaws issue is slow NTFS so Windows is the issue
@Glinkis can you try to hit enter after the first run is it still showin 1.6sec? (trigger a rerun)
@SimeonRolev iāll take a look to the example you posted and see what kind of number i get with the same steps (patternā¦) update: try1: i tried it i as u and got 6sec when i tried your steps -> drop to 3sec on rerun the same test (hit enter) try2: upgraded jest to 26.4 in Kent repo -> 3 sec first run near same for rerun try3: I took
index.spec.js
file from my benchmark test repo. and i dropped it to Kent repo. (deleting all other test files) -> surprise 2.8sec (SAME JEST 26.4.2, SAME COMPUTER, POWERSHELL, NODE_VERSION etcā¦ as my test yesterday posted here)I donāt understand this try3 still => ~3sec on rerun in Kent repo for my file but in my repo it drops to 0.300s on rerun (need someone to debug this) edit: Kent should be the one to check this š
Running the benchmark on my windows machine takes about 1.6s. I am not using WSL. I am using AVG antivirus, but have added exceptions for both the repository and node executable. My harddrive is an SSD.
Node version is
v12.16.1
Updating the test instantly triggers the file watcher, but the actual time it takes to run that update is around 1s-2.4s.
As expected, no difference to your test setup performance. Runs actually bit faster than on your computer. Changed your test setup to contain 100 test files under
__tests__
and those too run fine. Since our app usesreact-scripts
I also added that to your example to check if that might cause the actual issue but no difference in performance.HOWEVER then I tried to run those tests in WSL2 bash (against NTFS filesystem) and boom, execution time nearly 10x to raw powershell. Slower I/O speed is to be expected on WSL2 against NTFS but considering how simple this setup is (just 100 test files with single test on each one) it really shouldnāt affect that much. For reference, I executed this on WSL2 bash:
Which shows it takes practically no time at all to read the file system from WSL2. For reference similar command in Powershell:
Which shows the performance is on the same ballpark.
So, based on this I would say the issue might be caused how Jest uses I/O and that is somehow affecting the performance dramatically on WSL2. When it comes to the project that is causing me issues it is not a simple task to not require bash due to issues in code and tests (not done by me!). Considering the fact that WSL2 is gaining popularity I would say this is a real issue that should be looked into.
Hope this helps.
:edit:
Just out of curiosity I executed our test suite with
--no-watch
to see if watching the file system over WSL2 somehow affects things. The answer is no, it really doesnāt affect that much.Iām also having this issue on Windows. The test itself runs in <1 second but the overall setup takes ~15 secondes to execute. Iāve tried it with v24 and v26, itās actually a bit slower on v26
I did not have luck with any of the following (it did not improve the execution time) :
--runInBand
maxWorkers
.ts .js .spec.ts .spec.js .tsx
exclusions toVirus and Threat Protection
, as suggested by @Cellule and @alessioalexThe delay is still slow even with watchman installed. Iāve run a profiling test with ndb running ājest --verboseā, here are the results:
Screenshot of the first 1.7 seconds:
Screenshot of 1.8 secs to 2.7 secs
A .json file and a .heapsnapshot file saved from the profile tab in ndb after recording:
profiling.zip
Does
Windows 10 and lower
in the title mean that itās not an issue on Windows 11 or has it just not been tested?I find this to be a breaking deal issue on my Windows 10 VM that runs on 4 Xeon 6148 cores, 40 tests take ~90 seconds there
But my Windows 11 12700k machine simply slashes through 5555 tests in 7 seconds. And I have the exact same results on both SATA SSD and HDD.
I get that thereās a huge CPU difference but it seems a bit too much to write off as just that.
I tested with v26.4.2 in my benchmark jest github repo. https://github.com/nasreddineskandrani/benchmark_jest node version in my computer: v12.13.0
pretty fine when i update the simple test (see screenshot)! For me jest is now correct for a simple test. If you have a problem at your job. You need to try to isolate the problem since by default itās fine.
@empperi can you try my benchmark repo. example with the v26 folder and tell me how long it takes to run this test in your machine? if itās not 0.166ms or around it you have an issue your side.
So Iāve confirmed today that Windows Defender makes a huge difference. I used to have a bunch of exclusion, but when I received my newer faster laptop, I couldnāt for the life of me figure out why jest took >10 minutes to run a single file.
Then I remembered exclusions. I canāt tell exactly which exclusions make a difference, but I got the runner to go down to <15sec instead of >10mins
I found a gist with relevant exclusions (albeit forceful) https://gist.github.com/darvell/edbc758b11ea4dcd7226b7c9f1821196 I also added file extensions
.ts .js .spec.ts .spec.js .tsx
@pfftdammitchris I have compared pretty complex test suites on Mac + Windows and I see some differences, mainly from a cold cache Windows is notably slower, but once itās hot I get similar performance between the two.
HOWEVERā¦
One thing to be extremely wary of on Windows in particular are intrusive kernel-level programs like Antiviruses/File-watchers like Carbon Black (or other real-time file watching software). If you have something like this running, you can see HUGE performance hits when running Jest. I am talking about it taking tens of minutes to run tests.
I worked for a company last year where the same test suite took ~30 seconds on a Macbook Pro & 20 minutes on a Windows laptop with these file-watching programs running.
I have no idea why, but I would hazard a guess itās something to do with file-handles & how Jest uses some of the node.js filesystem APIs.
I thought enough attention had been given to micromatchās maintainers so that this mustāve been ironed out already. Running(thus writing) jest tests on windows is a very unpleasant experience at the moment.
@pachumon the fix is not present in 24.8.0 as far as i understood
you need to set one dependency of jest to a specific version to remove the performance issue (theoretically) the fix will be by default present in jest 25 => read here to know how a dev find out this https://github.com/facebook/jest/issues/7963#issuecomment-483985345
to set the dependency (micromatch) to the version where the fix was done => you can check here i did an example in a little project https://github.com/nasreddineskandrani/benchmark_jest/blob/master/jest_24_with_micromatch4/package.json
Add to your
package.json
: (must useyarn
notnpm
)Hope this helps! and waiting for feedback
Microsoft Windows [10.0.22621.2428] node v18.16.0 jest 29.7.0
Temporary workaround for WSL2 Users: Install jest globally too (in addition to installing it as a dev dependency) and then use its binaryās absolute path in the
package.json
file instead of onlyjest
, e.g.:"test": "/usr/bin/jest",
. This will make it use the global jest binary instead of the one in the node_modules which for some reasons works 10 times faster.Just chiming in to mention that @JPustkuchenās workaround is also working for me i.e. installing Jest globally:
npm install -g jest
. Speedup was significant: went from ~36 seconds to ~3 seconds in wall clock time.@josh-barker that should be because
/mnt
is NTFS until/home
is extI realise this isnāt addressing the actual performance issue, but Iāve found a work around that suits me.
Iāve setup a devcontainer in VS Code - https://code.visualstudio.com/docs/languages/typescript and the performance is much better. I put my source in in my home directory -
/home/<user>/<app>
Itās important that the code is not in
/mnt/<drive letter>/some/path
, as the performance is much slower there.Pressing enter makes the test run in around 200ms.
I wanted to test if itās the environment being the issue.
I was messing around with this repo: https://github.com/kentcdodds/react-testing-library-course/tree/tjs
npm test
and all the tests start running in watch mode.Iād be surprised if Kent C. Dodds has a messed up environment in his paid course, but if he does, you can debug it there probably :? In his videos, it runs just fine, I think he uses a Mac.
I have to note something very strange that I canāt reproduce again - I had some consecutive test reruns that for one of the files (tdd-02ā¦js) executed for about 0.1s, and for the file next to it (tdd-01ā¦js)- about 3s. The code is almost the same and uses the same dependencies. So, I copied the code from the fast-running file and pasted it to the slow-running one and vice versa. The results were the same - the slow-running file remained slow and the fast-running file remained fast, with the actual codes swapped. This is getting crazy. Now both files run slow again (3-6s).
FYI: https://github.com/kulshekhar/ts-jest/pull/1549 will be in alpha version of ts-jest (possibly today). Anyone who is using ts-jest please help to test the alpha version and give us some feedbacks for https://github.com/kulshekhar/ts-jest/issues/1115
Pure JavaScript here. I have this issue so not related to TypeScript.
Hi any news on this?
As a workaround you can use --runInBand if you start multiple tests. It will still took long to start the first test but the next tests will be fast.
My project took 21.803s for executing all tests. Now with --runInBand it takes only 7.412s
@nasreddineskandrani You got me. Iāve ordered a new macbook so will be out of open-source action until it arrives. I refuse to actually work on my crappy Windows box in my spare time š
It seems like the issue has moved in to the node/C++ realm, which is a little (extremely) outside my comfort zone - but I will do some digging!
you can still connect at home to do some open source š and check it p.s. a dota game takes more time to queue now you can check gitter in this time š this is where it is now: nodejs/node#28946
I am finishing a docker stuff i am doing for my personnal websites -> after (in a week) iāll come back on this.
@TomMahle Iāll try my side to have a repo github with the problem you describe.
dom
mode for the tests?Thanks for the suggestion about
micromatch
@nasreddineskandrani, but like @TomMahle above, pinning it to^4.0.0
didnāt seem to improve performance for me either. š¢I did find out one funky thing, though, which might help in diagnosing this problem.
I have the ability to run jest either on my native windows system, in a docker container with the main app directory mounted from my windows filesystem. Running in non-watch mode seems to have nearly identical behavior in both systems, which maybe suggests, as @thebearingedge implied, that the core problem has something to do with the NTFS filesystem, since my docker container is ultimately running everything except the filesystem in a linux VM.
However, on watch mode, things are slightly different: native windows always works slowly as expected, but the docker container only runs tests slowly on the first run. Once I tell it to run any test suite for the second time (e.g. by pressing <kbd>p</kbd> and entering a pattern), it runs the tests in well under one second (doing the same in native windows takes 3-4 seconds). The only downside of using docker is that the file change events donāt seem to propagate from my windows volume to docker, so I have to manually press <kbd>Enter</kbd> to re-run the test rather than having jest do it automatically, but I guess thatās not relevant to the issue at hand.
My test run time has also ballooned from ~2.5 minutes on 23.6.0 to ~15 minutes on 24.7.1 and 24.8.0. Our CI server is running windows and has seen a similarly large increase in build time with this upgrade. Iāve tried the micromatch dependency resolution override as mentioned above by @nasreddineskandrani to no avail. Please let me know if thereās anything I can do to assist with diagnosing this.
@bburns read above issue
@kensherman can you try with micromatch 4 in your yarn resolutions. to see if itās better in windows please? https://github.com/facebook/jest/issues/7963#issuecomment-483985345
Hello all ! I cumulate the slowness of jest 24 and windows 10 (800s for 400 tests!). The faster way I found to speed up all of this is to use wsl instead of powershell or cmd. Now my tests takes āonlyā 189s.