Why are my AngularJS, Karma / Jasmine tests running so slowly?

AngularjsJasmineKarma Runner

Angularjs Problem Overview


I have some simple karma / jasmine unit-tests that run against an angularjs app. I use the latest version of Chrome and run my tests from within the WebStorm IDE.

Sometimes the test suite runs very quickly (0.24 seconds)

Sometimes exactly the same test suite against exactly the same code runs very slowly (120 seconds)

I have tried every common sense fix. I have scoured the web to try and discover what I am doing wrong.

Why do my tests run so slowly?

Angularjs Solutions


Solution 1 - Angularjs

The answer turns out to be very simple.

I am using Chrome to run the karma server. When you first start the karma server an instance of Chrome is started as a maximised window. So naturally you minimise this so you can see your tests running.

The problem is that Chrome starves any minimised or secondary tabs (switched tabs) of CPU cycles.

Therefore, if you minimise the browser instance running the karma server, or just switch to a different tab, then the karma server is severely starved of CPU and the tests take a long time to complete.

The solution is to keep the karma tab active. The browser window can be hidden behind other windows but the karma tab must be the selected tab and the browser must not be minimised.

Following these simple rules will ensure that your tests always run at full speed.

Solution 2 - Angularjs

Biofractal has answered his own question and you could do that. You could also set up your tests to run in PhantomJS or Chrome Headless which basically allows the unit tests to run only in your command line, thus removing an affects with your actual browsers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbiofractalView Question on Stackoverflow
Solution 1 - AngularjsbiofractalView Answer on Stackoverflow
Solution 2 - AngularjsJimView Answer on Stackoverflow