Faster Angular tests with setupTestBed

Tomasz Bak
Nov 9, 2017 · 1 min read

by Angular Developers @ Selleo

Michal Pierzchala’s jest-preset-angular makes Angular tests faster with Jest. Here is how you can make them even faster with setupTestBed :-)

Following official guide you define TestBed.configureTestingModule inside beforeEach and get the testing module reseted on each it block in the file. This is expensive, especially if you have many dependencies.

You can avoid it with beforeAll and afterAll using Brad Chen setupTestBed helper function. In our case we got tests run 3.5x times faster. People reported 8x and more speed up.

You achieve the best results if you combine it with Shallow component tests with NO_ERRORS_SCHEMA

Here is how you can setup it with jest-preset-angular. Import setupTestBed.ts in jest.setup.ts

and create index.d.ts

Now you can replaceTestBed.configureTestingModule with setupTestBed and move it out of beforeEach block, like this:


Experienced Ruby On Rails, Elixir, Node.js, Ember.js, React & React Native developers ready to join your team or build your next project.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade