Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 874
biofractal Avatar asked Oct 30 '13 13:10

biofractal


People also ask

What is the role of jasmine and karma in Angular testing?

Jasmine is a behavior-driven development framework for testing JavaScript code that plays very well with Karma. Similar to Karma, it's also the recommended testing framework within the Angular documentation as it's setup for you with the Angular CLI. Jasmine is also dependency free and doesn't require a DOM.

Does karma use jasmine?

We can run Jasmine tests in a browser ourselves by setting up and loading a HTML file, but more commonly we use a command-line tool called Karma. Karma handles the process of creating HTML files, opening browsers and running tests and returning the results of those tests to the command line.


1 Answers

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.

like image 195
biofractal Avatar answered Oct 11 '22 15:10

biofractal