How to Speed up Karma and Jasmine Tests with ChromeHeadless

How to Speed up Karma and Jasmine Tests with ChromeHeadless

Accelerate Your Karma and Jasmine Tests with ChromeHeadless

I'm working with Jasmine and Karma because it is the default toolset for testing in Angular. It works, but one downside is the browser by default for tests runner.

The browser is a bit slow for running, and in the CI, it impacts the time of execution. We can speed up our tests using ChromeHeadless and puppeter with Karma.

Install packages

First, install karma-chrome-launcher and puppeteer.

npm i -D puppeteer karma-chrome-launcher

Configure karma

Edit karma.conf.js add the puppeter path, and set the ChromeHeadless as the default browser.

process.env.CHROME_BIN = require('puppeteer').executablePath()

module.exports = function (config) {
  config.set({
    ....
    browsers: ['ChromeHeadless'],
    ...
  });
};

Karma is ready to use ChromeHeadless from the terminal.

dany@dany:~/Documents/danylab$ ng test
⠙ Generating browser application bundles (phase: building)...19 12 2020 22:48:06.285:WARN [karma]: No captured browser, open http://localhost:9876/
19 12 2020 22:48:06.289:INFO [karma-server]: Karma v5.1.1 server started at http://localhost:9876/
19 12 2020 22:48:06.289:INFO [launcher]: Launching browsers ChromeHeadless with concurrency unlimited
19 12 2020 22:48:06.294:INFO [launcher]: Starting browser ChromeHeadless
✔ Browser application bundle generation complete.
19 12 2020 22:48:09.314:WARN [karma]: No captured browser, open http://localhost:9876/
19 12 2020 22:48:09.406:INFO [Chrome Headless 88.0.4298.0 (Linux x86_64)]: Connected on socket ipYKV7Bm7wf_o-d0AAAA with id 12690576
Chrome Headless 88.0.4298.0 (Linux x86_64): Executed 9 of 9 SUCCESS (0.166 secs / 0.13 secs)
TOTAL: 9 SUCCESS

Done

Hopefully, that will give you a bit of help with speeding up your tests with Jasmine and Karma.

If you enjoyed this post, share it!