6

Moving Angular CLI to Jest and Web Test Runner

 1 year ago
source link: https://blog.angular.io/moving-angular-cli-to-jest-and-web-test-runner-ef85ef69ceca
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Moving Angular CLI to Jest and Web Test Runner

On the Angular team, we believe testing is critical to build highly complex and scalable applications effectively. Testing takes many forms, and “unit tests” focus on testing the smallest units of a codebase. For Angular applications, this typically includes tests of individual components or services.

We regularly survey Angular developers to make sure we are always providing the best experience, and recently we have noticed consistent frustration with Angular unit testing:

Thinking about your overall experience, how satisfied or dissatisfied are you with the following parts of the Angular development experience?

A table breaking down Angular satisfaction by team size and feature area. The “testing” row is highlighted, where “small teams” of 1 to 10 engineers rated testing at 17% satisfaction, while “large teams” of 11 or more engineers rated testing at 19% satisfaction.

Developer survey results about Angular satisfaction broken down by team size and feature area.

We always take community feedback seriously and a low score like this for testing is a strong signal that we had some work to do. Digging deeper, developers often complain that Angular unit testing is slow, heavyweight, and difficult to integrate with continuous integration (CI) systems. There are lots of takeaways from this data and we’ll have more to share outside of testing soon.

Angular has historically taken a fairly opinionated approach on unit testing, with Karma being the only directly supported unit test runner. While Karma is a great option for many teams, it leverages a real browser, which can make unit tests heavier and slower than they otherwise should be. Using real browsers also complicates CI systems which need to download and manage browser versions.

At the same time, we have seen significant interest in the community around Jest, a unit test runner based on executing directly in Node without using a real browser. Several existing community projects have successfully brought Jest to Angular applications and proved the approach can work at scale for many applications.

As a result, we are excited to announce that we are adding initial experimental support for Jest in Angular v16. We have seen significant momentum in the Jest ecosystem and are excited to help bring those benefits to the Angular community. Please note that this is still experimental and not quite ready for use in business-critical applications, but you can give it a try by first updating to v16 once it is released and then modifying your `angular.json`:

{
"projects": {
"my-app": {
"architect": {
"test": {
"builder": "@angular-devkit/build-angular:jest",
"options": {
"tsConfig": "tsconfig.spec.json",
"polyfills": ["zone.js", "zone.js/testing"]
}
}
}
}
}
}

We have a lot of work left to do to get Jest as well integrated as Karma is today, so stayed tuned for the production launch!

Browser Testing

While Jest’s Node-based testing approach has a lot of attractive improvements, on the Angular team we still find considerable value in browser-based unit testing. Karma helped pave the way for browser-based unit testing since its initial release over 10 years ago. In that time, the web ecosystem has advanced by leaps and bounds, and some compelling alternatives have popped up. Since Karma is also now deprecated, this is a good time to update our browser testing infrastructure as well.

We have been particularly impressed with Web Test Runner, a browser-based unit test runner. It is developed by Modern Web, an organization dedicated to improving web tools to work with modern browsers and technologies instead of against them. We think Web Test Runner would be a great addition to the Angular tooling ecosystem. So we are also adding support for Web Test Runner in a future Angular release. We believe Web Test Runner can meet the needs of existing Angular browser-based unit tests and provide a solid foundation for web unit tests going forward.

Of course, backwards compatibility and ecosystem stability are key pillars of Angular and we are committed to making a migration off Karma as seamless as possible. To that end, we will integrate Web Test Runner with Jasmine to provide a testing experience extremely similar to the current Karma setup. We will also provide `ng update` support for migrating existing Karma tests to Web Test Runner.

We greatly appreciate all the incredible work that went into Karma over the years and everyone who contributed to this awesome project. Angular would not be where it is today without Karma’s support and we are immensely grateful to the community.

Looking forward, we believe Jest and Web Test Runner will have a significant impact on the Angular ecosystem and will improve the unit testing experience for everyone. There is still a lot of work to be done to launch both of these runners successfully, and land even more testing improvements in the future. Stay tuned for future updates.

Happy testing!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK