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?
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With