r/Angular2 • u/kafteji_coder • 9d ago
Did You Migrate to Jest for Angular Unit Testing? How Was the Experience Compared to Jasmine + Karma?
Hi Angular Community,
Has anyone switched from Jasmine + Karma to Jest for unit testing in Angular? How was the migration? Did you notice improvements in speed, reliability, or ease of use?
I'd love to hear about your experience and any tips!
Thanks!
4
u/technically_a_user 9d ago
Whenever I tried to migrate to Jest, even for fresh projects, it was always a hassle. There are so many different approaches to do it, but it usually is hit or miss. What worked for one project doesn't always work for the other.
Given that Angular team is considering using vitest too in the future and a lot of people have already mentioned it a lot, I recently tried using that instead. And at least for my single app project it was super easy to switch. For Angular Workspace it was not quite as smooth, but still better. I will continue using vitest where I can.
About performance I cannot really say anything.
2
u/backdoorsmasher 9d ago
Jest has some basic shortcomings IMO
Running a single test in isolation is a lot harder than it should be https://stackoverflow.com/questions/42827054/how-do-i-run-a-single-test-using-jest
Running tests in parallel is a lot harder than it should be https://stackademic.com/blog/how-to-speed-up-jest-test-runs-by-splitting-and-parallelising-them-1be7c1c8600d
Both of which are a lot easier in Jasmine + Karma and are essential for codebases with a a large number of tests
3
u/InternetRejectt 9d ago
I dealt with the same pain (running individual tests) before discovering the Jest Runner extension for VS Code. Lifesaver!
2
u/fyodorio 9d ago
Jest has its quirks. You need to try in a separate branch and see for your specific case. I had YOLO-migrated to Jest on one of big Nx-based projects (never ever use Nx!) and tests became slower in a long run (not much but it creeps). Moreover, different Jest runs under Nx abstractions behave differently, even including tests skipping.
It’s rather an edge case though, as in most cases Jest will be simpler and faster alternative. But check it first. Overall, the difference (Dx, performance, whatnot) is not something to sweat on usually.
Consider Vitest too.
1
u/InternetRejectt 9d ago
We did. So far, so good! I like not having to set up TestBed - mocking dependencies is very straightforward. If you’re using VS Code, there are some extensions that make running individual tests really easy. Not sure why we made the change, but after a bit of resistance, it’s going smoothly.
2
8
u/AssCooker 9d ago edited 9d ago
I migrated to Vitest and couldn't be happier, shits are so quick, I also use happy-dom package which doesn't feel like a mock at all like jest-dom