Work with us

Let us know your requirements and we'll get back to you as soon as possible.
Drop files here or click to upload

We care about your privacy and automatically agree to the following NDA. This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Submit

Thank you, !

Thank you very much for submitting your inquiry to Xfive! We'll be in touch with you soon.
Katelyn Gleason
Katelyn Gleason Eligible
Xfive’s team rental model helped me easily acquire a new team member for our project. Marek has been working with us for many months and although he is an external contractor, I feel like he's a part of our family now.
Home Blog Testing Angular Faster with Jest

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

where:

  • 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).

Example:

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 = {},
  };
};

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).

Caveats

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:

Summary

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 LinkedIn

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 from Michal
More from Michal

Comments (30)

Write a comment

Wilgert Velinga

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!

Apr 01, 2017

kpax

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?

Apr 03, 2017

Michal Pierzchala

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!

Apr 04, 2017

Dominic Watson

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.

Apr 04, 2017

Brady Isom

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:

/Users/bradyisom/Development/buzz/src/setupJest.ts:1
({"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?

Apr 04, 2017

Michal Pierzchala

Dominic, Brady, just for the record, this was resolved in jest-preset-angular repo: https://github.com/thymikee/jest-preset-angular/issues/4

Apr 05, 2017

hotelyorba

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

Apr 05, 2017

Michal Pierzchala

hotelyorba, I have an example directory in jest-preset-angular repo:
https://github.com/thymikee/jest-preset-angular/tree/master/example
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()).

Apr 08, 2017

danAnderl

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

Apr 15, 2017

Michal Pierzchala

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!

Apr 18, 2017

Jan

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?

Apr 20, 2017

Michal Pierzchala

Jan, working on it!

Apr 22, 2017

Michal Pierzchala

Hey folks, it's now possible to snapshot your Angular components: https://github.com/thymikee/jest-preset-angular/pull/24
Happy testing! :)

Apr 22, 2017

Eniep Yrekcaz

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: https://github.com/PdUi/angular-aspnet-skeletons/tree/jest-integration/ng-custom
I assume you ran into a similar issue and would greatly appreciate any help you could offer.

Thanks again for the very informative post!!!

May 08, 2017

Michal Pierzchala

Eniep Yrekcaz, checkout the PR I sent you :)

May 10, 2017

Eniep Yrekcaz

You are awesome. I really appreciate it!!!

May 10, 2017

Andrew Krueger

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

May 17, 2017

Michal Pierzchala

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

May 18, 2017

Jon Caris

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

May 25, 2017

Michal Pierzchala

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

May 26, 2017

David Atencia

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

May 27, 2017

YuJM

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

Jun 15, 2017

Carl

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?

Jul 22, 2017

Thomas

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.

Aug 10, 2017

Derek Lin

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

Dec 16, 2017

Marian Zburlea

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.

Dec 20, 2017

Kornel Dva

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.

Jan 25, 2018

Jon

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(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule.withRoutes(routes)],
declarations: [HomeComponent, AppComponent, PageNotFoundComponent]
});

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

fixture = TestBed.createComponent(AppComponent);
router.initialNavigation();
});

it(
'navigate to "/abcdefghij" redirects you to /pagenotfound',
fakeAsync(() => {
router.navigate(['abcdefghij']).then(
() => {
expect(router.url).toBe('/pagenotfound');
},
() => {
fail('Failed to open page');
}
);
})
);

Thanks

https://github.com/zebslc/angular5mat

Apr 26, 2018

Bhargav

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));
expect(service.plContext).not.toBeNull();
expect(global.sessionStorage.getItem).toHaveBeenCalled();

But, I get an error saying:
expect(jest.fn())[.not).toHaveBeenCalled()

jest.fn() value must be a mock function or spy.
Received:
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.

Aug 15, 2018

Ayyaz Zafar

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

Aug 16, 2018

Would you like to add something?

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

Submit

Start your project or scale your team