Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running more than 10 tests on karma using jasmine causes: "ERROR: Some of your tests did a full page reload!"

So, this is my first project where I'm using Karma and Jasmine to unit test my angularJS code. Used Yeoman angular generator for the setup.

As soon as I reached 11 tests, I got an error saying "Some of your tests did a full page reload". I'm not doing any tests that would trigger a reload.

Digging deeper I saw the same issue being referenced on Github. https://github.com/jasmine/jasmine/issues/366 -- (FuzzySockets comments)

The problem seems to stem from a line of code in jasmine-core https://github.com/jasmine/jasmine/blob/master/lib/jasmine-core/jasmine.js

To avoid overflow of stack, the maximumSpecCallbackDepth is set to 20. And every time currentSpecCallbackDepth exceeds that, further tests are executed on a new stack by using the setTimout function.

This is the line that seems to cause problems and makes karma throw the error. (I've verified this by invoking the setTimeout method in my own unit test, and it threw the same error).

If change the maximumSpecCallbackDepth to 100, my tests run fine, and no errors are thrown at the end

Has anyone seen this issue and know a fix? I'm using the latest versions of karma(0.13.15) and jasmine(2.4.1).

I haven't really messed around too much with the default grunt or karma config that came with yeoman generated ones, except that I'm using the chrome launcher instead of the default phantomJS, so I don't understand how everyone else is not facing the same issue here.

like image 580
Ronak Karia Avatar asked Oct 30 '22 13:10

Ronak Karia


2 Answers

+1 for this issue. As u said, it caused by maximumSpecCallbackDepth limitation, but I didn't find no fix for this issue so far. You probably could track issue here https://github.com/karma-runner/karma/issues/1101 .

One temporary solution is to reduce nested 'describe' block in your project.

like image 193
Juan Du Avatar answered Nov 09 '22 15:11

Juan Du


I got a similar issue where the angular injections in the global beforeEach stopped working and all tests failed after the 20 limit of maximumSpecCallbackDepth.

During my investigations, I found out that angular-mock doesn't play well with the setTimeout done in jasmine when that limit is reached.

The following code that is given as example everywhere will create a new injector on each test case:

var yourService;
beforeEach(module('app'));
beforeEach(inject(function(_yourService_) {
    yourService = _yourService_;
}));

Instead, you could do the following, which will use a single injector and register your modules only once.

var yourService;
module.sharedInjector();
beforeAll(module('app'));
beforeEach(inject(function(_yourService_) {
    yourService = _yourService_;
}));

Hope this might help others as it took me almost a week to find out that this was the root cause of the issue and not Jasmine itself like some people think on github.

like image 25
Naïm Baki Avatar answered Nov 09 '22 17:11

Naïm Baki