Arrow Down Arrow Right Arrow Down Arrow Right Arrow Arrow Down Arrow Left Arrow Right Articles Case Study Close Facebook GitHub Google+ Menu Information Link LinkedIn x five Interview Location Code Snippet Twitter Tick Timer Users Card Discount Magnifier Time Quote Task

This website uses cookies to ensure you get the best experience on our website. Privacy Policy


Testing Angular faster with Jest

by Michal Pierzchala on March 31, 2017
Published in Open Source 30 Comments
Testing Angular faster with Jest

It struck me how painful experience it was to test an Angular app. And then I realized it can be painless with Jest.

Note: This article assumes you are using an Angular CLI v1.0 generated project from Angular v4.0.0. But it too works on Angular v2.x with CLI in beta.

Despite Angular being a comprehensive framework with lots of default abstractions: e.g. custom JIT and AOT compilers, deep RxJS and Zone.js integration, it’s still possible to work outside of its toolbox. Interestingly, it’s possible to change the default test runner. This post will tell you why, and how you can do it.

If you ever felt blue, tired, hopeless while running your Angular app slow test suite with Karma in your favourite browser (and I’m not talking about injecting every possible component into your test file), keep reading.

What if I tell you, that you can:

  • run your Angular tests without a browser
  • run test suite several times faster
  • rerun instantly only tests related to latest code changes?

Testing Angular with Jest

Meet Jest 🃏

Jest is a painless JavaScript testing platform. If this doesn’t tell you much, it’s probably because you had to spend all your free time on running slow Karma tests 😅. Let me introduce it briefly:

  • Jest is a testing platform, widely adapted by many large companies and swiftly adopted by the React community.
  • Sits on top of Jasmine, so the API is nearly identical.
  • Has all of it’s API documented, along with guides, examples and helpful community on forums like Reactiflux and Stack Overflow.
  • Focuses on Developer Experience (speed and ease of use is the first priority.)
  • Provides meaningful error messages.
  • Runs on Continuous Integration servers without extra tooling (abstracting the DOM with jsdom library.)
  • Provides code coverage out of the box.
  • Integrates with Babel and TypeScript seamlessly.

And what’s most important is that it provides a smart, immersive watch mode. The watch mode runs only tests affected by git file changes – it also runs failed tests first and is able to bail on first error so the feedback loop is ~10x faster than with Karma, depending on test suite size.

Testing Angular with Jest - Watch Mode

To the point – I want my Angular tests faster now!

We need to install necessary dependencies (I’m using yarn, but if you fancy npm, no problems):

$ yarn add --dev jest jest-preset-angular @types/jest


  • jest – Jest testing platform
  • jest-preset-angular – configuration preset with common settings setup for you
  • @types/jest – Jest typings

Note: if you're using npm v2 (which doesn't flat out dependencies) you'll also need to install jest-zone-patch.

You’ll need to add this entry to package.json:

"jest": {
  "preset": "jest-preset-angular",
  "setupTestFrameworkScriptFile": "<rootDir>/src/setupJest.ts"

This is most likely all configuration you'll need. I've extracted common configuration options into jest-preset-angular package and it should just work™ for most cases.

Note: setups differ and you may need to tailor the config specifically to your app needs. You should then find README of the preset helpful. It's also worth noting that every option set by preset may be simply overwritten.

Next step – create setupJest.ts. This little guy you can write in TS, also making sure to place it into /src as well. It looks like this:

import 'jest-preset-angular';
import './jestGlobalMocks';

You can see that we’re importing jestGlobalMocks.ts file with patches to our window object (jsdom doesn’t have it all implemented so we need to patch it sometimes).


const mock = () => {
  let storage = {};
  return {
    getItem: key => key in storage ? storage[key] : null,
    setItem: (key, value) => storage[key] = value || '',
    removeItem: key => delete storage[key],
    clear: () => storage = {},

Object.defineProperty(window, 'localStorage', {value: mock()});
Object.defineProperty(window, 'sessionStorage', {value: mock()});
Object.defineProperty(window, 'getComputedStyle', {
  value: () => ['-webkit-appearance']

Mocking localStorage is optional, but without mocking getComputedStyle your test won’t run, as Angular checks in which browser it executes. We need to fake it.

You’re now ready to add this to your npm scripts:

"test": "jest",
"test:watch": "jest --watch",

...and change the way you test Angular apps forever.

Oh, one more thing. Forget about installing PhantomJS on your CI:

"test:ci": "jest --runInBand",

It's worth noting that CI servers usually run your code on single core, so parallelization may slow your tests down. If you're experiencing such behavior, use --runInBand flag to tell Jest explicitly that you want to run tests one-by-one (just like Karma or Mocha).


Of course, there are some. But surprisingly not many.

Migration from Jasmine

We’ll need to migrate some of Jasmine calls to Jest. Most of the API is similar but there are slight differences.

Below are listed required changes to be made in your codebase.

  • jasmine.createSpyObj('name', ['key']) --> jest.fn({key: jest.fn()})
  • remove @types/jasmine module (duplicate TS declarations from @types/jest)

After porting jasmine.createSpyObj() you can come back later to migrate rest of the functions, which are optional at the time of writing (this may change in the future):

  • jasmine.createSpy('name') --> jest.fn()
  • and.returnValue() --> mockReturnValue()
  • spyOn(...).and.callFake(() => {}) --> jest.spyOn(...).mockImplementation(() => {})
  • Asymmetric matchers: jasmine.any, jasmine.objectContaining, etc. --> expect.any, expect.objectContaining

Farewell browser, our tests now run in jsdom

You can also bump into APIs available in browsers but not in jsdom, like htmlElement.closest or window.localStorage (which we just mocked in jestGlobalMocks.ts).

Zone.js messy error stack trace

I couldn't force Zone to output shorter and more meaningful error stack traces. As a workaround you can add Error.stackTraceLimit = 2; to your setupJest.ts or whatever number suites you. I find it useful for most cases.

IDE integrations

Be sure to also check these awesome extensions to ease testing experience even more:


I was truly amazed that it’s actually possible to integrate a 3rd party testing tool not running in browser, like Jest, into Angular. Couple of days ago I still thought that it will be impossible to do so within reasonable amount of time and it’s just not worth the effort, but turned out it was so worth it.

Our app's whole test suite of 35 files with ~100 tests executed 2.5x faster than with Karma. We can also use snapshot testing. What’s most important though, we now have powerful watch mode, rerunning tests instantly. And all of this without the need of compiling the app or running a dev server.

So if you care about your testing experience, do yourself a favor and run Angular tests with Jest. You’ll love it.

And don't forget to star it on GitHub!


If you happen to find problems with the setup presented or spot a bug, please file an issue in jest-preset-angular GitHub repository.

About the author

Michal Pierzchala

Michal Pierzchala has been a member of Xfive since 2013. Part of the Jest core team. Passionate about modern web technologies, enthusiast of traveling, sailing and future space exploration.

More articles from Michal


Wilgert Velinga April 1, 2017

Jest looks like a big improvement over Karma. But actually something exists that is even better into terms of feedback speed and quality: wallabyjs. Try it out!

kpax April 3, 2017

I can't seem to figure out how to get webpack to run through it's loaders first, like angular2-template-loader to do what your preprocessor is doing. Is there a way to get jest to kick off a webpack bundle first?

Michal Pierzchala April 4, 2017

kpax Running your code through Jest and Webpack are two different things. One doesn’t know about the other. Here you’ll find a Webpack tutorial on official Jest docs. You’ll probably need to adjust moduleNameMapper to suite your needs. You can also submit an issue on jest-preset-angular and we’ll see what can be done. Good luck!

Dominic Watson April 4, 2017

Feel like I'm being dumb and missing something, but is this for ejected CLIs?
Using on a project out of the box I get: `SyntaxError: Unexpected token import` as it runs through each of the tests.

Brady Isom April 4, 2017

I was excited about trying to add Jest to my Angular 4.x/AngularCli project. Thank you for the post. However, In following the instructions, I am seeing an error when Jest tries to load the setupJest.ts file:

({"Object.":function(module,exports,require,__dirname,__filename,global,jest){require('ts-jest').install();import 'jest-preset-angular';
SyntaxError: Unexpected token import

at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
at handle (node_modules/worker-farm/lib/child/index.js:41:8)
at process. (node_modules/worker-farm/lib/child/index.js:47:3)
at emitTwo (events.js:106:13)

Any ideas on what is wrong here?

Michal Pierzchala April 5, 2017

Dominic, Brady, just for the record, this was resolved in jest-preset-angular repo:

hotelyorba April 5, 2017

Do you have a github repo where we can see some examples of how to test angular components and services using Jest?

Michal Pierzchala April 8, 2017

hotelyorba, I have an example directory in jest-preset-angular repo:
But in general you can test components, services, reducers, etc the same way as with Jasmine, provided that you're not using unsupported methods (like jasmine.createSpyObj()).

danAnderl April 15, 2017

Hello there,
very nice article. i manage to execute 208 tests in around 30 secs in the best case. which is quite good, but i want it even faster. the problem seems to be in some initial step, as only the first tests take long (6 - 10 secs -> each worker ) and the following up are really quick.

-> watch mode 2 specs changed:

PASS src/app/features/geofencing/geofencing.component.spec.ts (5.593s)

PASS src/app/features/geofencing/geo-definition/geo-definition.component.spec.ts (9.75s)

Test Suites: 2 passed, 2 total
Tests: 16 passed, 16 total
Snapshots: 0 total
Time: 10.709s, estimated 11s

-> watchAll -w=8

Test Suites: 5 failed, 41 passed, 46 total
Tests: 202 passed, 202 total
Snapshots: 0 total
Time: 31.418s

do you have any idea to investigate what takes so long initially? i am using uptodate versions of ng (4.0.1) and ng-cli (1.0.0)

Cheers Andi

Michal Pierzchala April 18, 2017

Hey danAnderl!
The following runs are faster, because the cache is "warm". On the first run Jest scans all necessary files and transform them from TypeScript to JS. Transforming takes some time, so the results (JS files) are cached – this way the files are only transformed through tsc (or any other transformer, e.g. babel) only once, and after they're changed.
Glad it works for you!

Jan April 20, 2017

Thanks for the great article and project ... I still have a question concerning Snapshot testing ... is there an equivalent to the react-renderer i can use in Angular projects?

Michal Pierzchala April 22, 2017

Jan, working on it!

Michal Pierzchala April 22, 2017

Hey folks, it's now possible to snapshot your Angular components:
Happy testing! :)

Eniep Yrekcaz May 8, 2017

Thank you so much for the post. Your approach and the Jest framework look very interesting. I have my own Angular* skeleton project that I've been working on. I tried to integrate your approach, but am running into some issues. Namely, I keep getting an error trying to run the tests relating to zone-node.js. If you would be willing to look, the repository is on GitHub:
I assume you ran into a similar issue and would greatly appreciate any help you could offer.

Thanks again for the very informative post!!!

Michal Pierzchala May 10, 2017

Eniep Yrekcaz, checkout the PR I sent you :)

Eniep Yrekcaz May 10, 2017

You are awesome. I really appreciate it!!!

Andrew Krueger May 17, 2017

Michal Pierzchala, what's the benefit of using Jest with Webpack rather than running it normally?

Michal Pierzchala May 18, 2017

Andrew Krueger, I'm sorry, but I don't know what are you referring to. Please be more specific.

Jon Caris May 25, 2017

when trying to configure jest to run with coverage I get an error :
Failed to write coverage reports:
ERROR: RangeError: Maximum call stack size exceeded
STACK: RangeError: Maximum call stack size exceeded

Michal Pierzchala May 26, 2017

Jon Caris, that doesn't tell much. But if you can provide a repository with the reproduction of the problem, please post it on Jest issue tracker

David Atencia May 27, 2017

Awesome! I have been able to integrate Jest into an Ionic2 project with a little change following the instructions in this article. Really easy to set-up.

Many thanks for the article

YuJM June 15, 2017

i using material2
i could not test ( dependency material2 components files )
how do i do?

Carl July 22, 2017

This article is awesome and the angular jest preset is great. I'm having one little issue that I hope you can help with. When I change one file, jest runs all of my tests rather than the tests relating to the file that changed, do you know why this is?

Thomas August 10, 2017

Nice article. Any suggestion why the paths in the tsconfig seem not to be resolved?
My tests are crashing when using absolute imports based on the paths defined there.

Derek Lin December 16, 2017

Do you have an example of how to set up jest in a multi-app environment in angular-cli?

Marian Zburlea December 20, 2017

Bad day:
FAIL src/app/app.component.spec.ts
● Test suite failed to run

TypeError: Cannot read property 'clearImmediate' of undefined

at new FakeTimers (node_modules/jest-util/build/fake_timers.js:106:29)
at new JSDOMEnvironment (node_modules/jest-preset-angular/testEnvironment.js:27:23)

Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 0.56s, estimated 1s
Ran all test suites.
npm ERR! Test failed. See above for more details.

Kornel Dva January 25, 2018

Well, this works fine for simple setups. As soon as you have some 3rd-party modules, the "painless" becomes "nightmare". Jest babel ignores, by default, everything in node_modules and you can either hack it to stop ignoring specific modules, or you have to mock all the stuff in the modules, otherwise you get errors like "SyntaxError: Unexpected token export". Good luck with the mocking.

Jon April 26, 2018

Does anyone know of any tutorials or links about testing Angular routing with Jest? I have a really simple sample project with e2e and test coverage plus reporting for a CI/CD. Currently all the tests work except routing to an unknown route.

const routes = AppRoutingModule.applicationRoutes;

beforeEach(() => {
imports: [RouterTestingModule.withRoutes(routes)],
declarations: [HomeComponent, AppComponent, PageNotFoundComponent]

router = TestBed.get(Router);
location = TestBed.get(Location);

fixture = TestBed.createComponent(AppComponent);

'navigate to "/abcdefghij" redirects you to /pagenotfound',
fakeAsync(() => {
() => {
() => {
fail('Failed to open page');


Bhargav August 15, 2018

After adding your jestGlobalMocks.ts to setupJest.ts and importing the setupJest.ts like below:
setupTestFrameworkScriptFile: "/src/setup-jest.ts",
I am expecting sessionStorage.getItem to have been called like below:
global.sessionStorage.setItem('appInitData', JSON.stringify(plContext));

But, I get an error saying:

jest.fn() value must be a mock function or spy.
function: [Function getItem]

Can you please help? I feel like I am close to getting it resolved. I am not using any jest.mock() or jest.spyOn() for sessionStorage since I believe it's already mocked in the jestGlobalMocks.ts. Correct me if I am wrong. Thanks in advance.

Ayyaz Zafar August 16, 2018

It is not working with angular 6.
I am getting this error:
import 'jest-preset-angular';
SyntaxError: Unexpected token import

Would you like to add something?

All fields are required. Your email address will not be published.

More from the blog

Work with us