Supercharge your

Angular tests

with Jest

Michal Szczepaniak

?

Problems with jasmine testing

  • initial build times are slow
  • productivity is lower than it should be
  • error messages are vague at best

What is Jest? 1

  • delightful JavaScript Testing Framework
    with a focus on simplicity
  • code coverage out-of-the-box
  • snapshot testing
  • using jsdom under the hood
  • parallel tests

Speed

 

"As an experiment, 100 libs were generated and tested using Karma and Jest. Karma took around 15 minutes while Jest finished in only 4 minutes! The tests run in this experiment are identical between the two test runners which means the difference is due to the differences between the two test runners."

- Nrwl Team (Nx founders) 4

 

Immersive watch mode

DEMO

jest features

1-2-3 Migration

1. Delete not needed packages

2. Install new packages 2

3. Alter package.json

4. Last one (I promise) setupJest.ts

Alternative setup 3

Briebug's Angular-Jest schematics:

  • installs Jest, it's dependencies, and new scripts
  • adds necessary files for Jest to work with Angular
  • removes unnecessary Karma files and configuration

 

 

Additional work for Angular 8

Migrating codebase

  • no need to change assertions
  • differences in mocking

 

jest-codemods

SNAPSHOT TESTING

 

"A good snapshot stops a moment from running away."

- Eudora Welty

 

Snapshot testing 5

  • take any serialisable object, create a snapshot and then compare your changes with the previously taken snapshot
  • snapshots do not have to be created by hand

.toMatchSnapshot()

if the snapshot is not there, jest will create it for you

DEMO

snapshot testing

How about Rxjs and

Observables?

6

Title Text

THANK YOU!

 

Slides available on twitter:

@frontend_mike

Supercharge your Angular tests with jest by @frontend_mike

By msz_technology

Supercharge your Angular tests with jest by @frontend_mike

Anyone knows that you should be testing your app. But in the reality, it is really hard and cumbersome to keep all your tests up to date and develop using TDD methodology. Choosing the proper tools for the job is the first and essential step to achieve this goal. Throughout this session we’ll learn how to use the Jest testing framework with Angular. In the demo, we will compare the testing performance using both Jest and Jasmine within an Angular application and see how the watchers work in both of these frameworks. After that, I will share some migration strategies how to convert the existing Jasmine infrastructure to Jest. Last but not least, we will get to know the coolest feature of Jest – snapshot testing – and use it with Angular!

  • 1,067