January 2024

Migrating from Jasmine to Jest

- entry level, jest, testing; 1 minute reading time

If you want to migrate Jasmine tests to Jest, jest-codemods helps you a lot. I used it in a project with more than 200 tests, and it slashed the effort by several days:

npx jest-codemods

The tool is going to ask you a few questions. Here are the correct answers for a standard Angular project:

? Which parser do you want to use? ! TypeScript ? Which test library would you like to migrate from? ! Jasmine: globals ? Are you using the global object for assertions (i.e., without requiring them)? ! Yes, and I'm not afraid of false positive transformations ? Will you be using Jest on Node.js as your test runner? ! Yes, use the globals provided by Jest (recommended)

The tool doesn't fix every incompatiblity. Luckily, most issues are pretty obvious. Richard Wotzlaw has written a deep dive on migrating to Jest, including list of replacements.

 
January 2024

How to set up Jest for an Angular 17 monorepo using @angular-builders/jest

- entry level, jest, testing, angular; 6 minutes reading time

Using a Jest builder is an attractive option for using Jest in an Angular project. @angular-builders/jest:run is a production-ready solution. The name is a bit cumbersome, so let's call it the "Just Jeb" plugin because the author has published it on his website JustJeb.com.

In a few years, we'll probably prefer Angular's native builder, but it's not fit for production yet. I'm covering it here.

  more...
January 2024

How to set up Jest for an Angular 17 monorepo manually

- entry level, jest, testing, angular; 7 minutes reading time

The nice thing about Jest is it's easy to get started. However, most tutorials tell you about setting up Jest in a default Angular project. I was interested in using Jest in a monorepo, so I had to research.

As far as I can see, there are five roads you can go:

  • Starting with Angular 18 (or even later?), you can use the native support Angular offers. I'm covering this in another article, but as of Angular 17, I don't think it's ready for use in production. Experiment with it and tell the Angular team about it because that's how open source progresses, but don't use it in production.
  • You can configure Jest manually. That's what this article is about.
  • You can use the Briebug schematics to set up Jest in your Angular project or workspace.
  • You can use a plugin like the "Just Jeb" builder (aka @angular-builders/jest. I'm covering this here. You may also want to read Jeb's article.
  • You can use nx. That's almost certainly a good option, but until now, I have always shied away from using nx. I don't want to add another dependency until I need it. That's why I can't tell you much about it.
  more...