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

biofractal picture biofractal · Oct 30, 2013 · Viewed 8.5k times · Source

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?

Answer

biofractal picture biofractal · Oct 30, 2013

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.