Image for post
Image for post

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:

selleo

Experienced Ruby On Rails, Elixir, Node.js,

Tomasz Bak

Written by

✅ Design Successful Product at https://selleo.com ✅ Build Engineering Team at https://www.softkraft.co 🚀

selleo

selleo

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

Tomasz Bak

Written by

✅ Design Successful Product at https://selleo.com ✅ Build Engineering Team at https://www.softkraft.co 🚀

selleo

selleo

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

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store