60

I'm running into an issue where when I run my tests on Jasmine, I get this error below. The problem is, it seems to happen when I try to execute a certain amount of tests. It doesn't seem to be tied to a particular test, as if I comment out some, the tests pass. If I uncomment some tests, the error appears. If I comment out ones that were uncommented before, they all pass again. (ie if I have red, green, blue and orange test and it fails, I comment out orange and blue it passes, then I uncomment blue and orange it fails again, but if I comment out red and green it passes again).

Chrome 41.0.2272 (Mac OS X 10.10.1) ERROR Some of your tests did a full page reload! Chrome 41.0.2272 (Mac OS X 10.10.1): Executed 16 of 29 (1 FAILED) ERROR (0.108 secs / 0.092 secs)

I'm stumped as to what is going on. The more tests I add, that's when this becomes an issue. Has anyone encountered this before? I have no idea what could be causing it, as nothing in any of my tests do any kind of redirection, and they all pass universally on another persons machine.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
Siraris
  • 1,088
  • 3
  • 13
  • 21
  • Can you add some of your tests to the question to get a feel for the issue. – Nicholas Murray Apr 01 '15 at 14:43
  • As the error message states, your tests are causing a full page load. You should be trying to figure out why the page load is happening. The tests must be navigating to a new location. – Andrew Eisenberg Apr 06 '15 at 04:07
  • Does your app use ui-router, by any chance? – Manube May 25 '15 at 07:53
  • I see you're using Chrome. Does the same thing happen in Firefox or PhantomJS? – Michal Charemza May 26 '15 at 12:20
  • @Manube No I am not. And Michal It happens in Firefox as well, yes. – Siraris May 26 '15 at 14:28
  • I too have the same issue. If you have got the solution, could you please let me know. Its been more than a week, but still having the same issue. – Rajeshwar Jul 14 '15 at 04:49
  • Having the same issue. No solutions anywhere. I have 8 describe blocks in one test, I get the full page reload, if I comment out any of the 8 describes (reducing the count to 7) it works. – ThinkingInBits Oct 21 '15 at 19:43
  • @MichalCharemza for me it's the case - it happens on chrome but on firefox tests are going through well. Do you know what might be a reason? – topr Jun 27 '16 at 10:47

14 Answers14

49

In my case the problem was that in my source code I had code directly setting the href on the location object, like window.location.href = 'somewhere';

In my specs I set up a onbeforeunload listener that just returns a string instead of allowing the redirect to take place:

beforeAll(() => {
  window.onbeforeunload = () => 'Oh no!';
});
telliks
  • 1,216
  • 10
  • 13
26

I suppose you are using window.location somewhere in your targeted code. In order to pass it just create a spy for the window.onbeforeunload

Example:

window.onbeforeunload = jasmine.createSpy();

Or even better use $window instead, and this will not happen.

Adrian Diaconu
  • 261
  • 3
  • 5
20

Make sure that your tests are properly isolating all modules under test with mocks/spies. The behavior you are seeing says to me that your tests are not truly running in isolation - they are changing some state somewhere that will trigger a reload.

johnmcase
  • 1,769
  • 2
  • 16
  • 27
  • 2
    I actually found the place after offering the bounty. And yes, you are right on the money. In my case the reload was triggered after a certain amount of tests for some reason - because of a pending http-httprequest to an external system that timed out exactly between test 13 and 14... ;) – Christian Dalager May 26 '15 at 21:02
4

There are many ways this error can happen.

If your component has a form element, this might be the cause.

Whenever a button on the form is clicked, this error can happen, even tho your component contains no navigation logic.

Ryan
  • 550
  • 4
  • 9
3

I recently encountered this error with Karma 0.13.12. I upgraded to Karma 0.13.14 and my tests work again. The problem for me (and probably also for @mqklin) was related to https://github.com/karma-runner/karma/issues/1656 and https://github.com/jasmine/jasmine/issues/945.

mer10z_tech
  • 697
  • 7
  • 12
2

What worked for me was upgrading Karma from 1.4.0 to 1.4.1 and changing the maximumSpecCallbackDepth in my jasmine.js file from 20 to 100.

keimjohn
  • 139
  • 1
  • 5
1

creating a spy on the function which has the window.location / reload fixed the issue for me

Sebastian
  • 11
  • 1
1

Hope you have used window.location = "some url" in your code; Faced similar problem, and solved by using the below changes.

Replaced window.location in the code with,

window.location.assign("some url");

Do the below in unit test:

spyOn(window.location, "assign").and.callFake(() => {
    // Dummy assign call - so that your actual call will be faked and the reload will not happen. 
});
Easwaramoorthy Kanagaraj
  • 3,925
  • 8
  • 36
  • 62
  • 4
    seems as nice suggestion but I got 'assign is not declared writable or has no setter' – ada Jul 12 '18 at 11:38
0

Try to reduce amount of describe sections or completely remove them. I don't know why, but it works for me.

mqklin
  • 1,928
  • 5
  • 21
  • 39
0

You also need to make sure that modules are not being loaded twice. In my case, I had an AngularJS module file -e.g., auth.controller.js which contents were already bundled in a core.js file. Once I excluded the bundled files on karma, the error disappeared.

rebelliard
  • 9,592
  • 6
  • 47
  • 80
0

I was using setTimeout(() => window.location.replace('/'), 10); I used below code in my unit test and it worked for me.

spyOn(global, 'setTimeout');
0

In case it was ng-submit callback, which doesn't call "event.preventDefault()" and the browser reloads page. Mocking $location doesn't help in that situation.

Daniil Iaitskov
  • 5,525
  • 8
  • 39
  • 49
0

According to angularjs documentation you should inject the $window module to be able to solve the testability issue you get. If you really want to do a full page refresh while routing, which will reload the whole application. But anyway...

So for example in component

.controller('ExampleController', ['$scope', '$window', function($scope, $window**) 
 {

      $scope.doRerouteWithPageReload = function() {
       return this.$window.location.href = "/myUrl";
 };

And then in your test-file you import $window to the test-controller your way, then where you assign spies you can do something like this:

$window = { location: {href: jasmine.createSpy() };

And then the actual test is something like this:

expect($window.location.href).toBe("/myUrl");

Angularjs documentation for reading more about $window.

Michael R
  • 75
  • 12
-1

It will solve this Karma redirect error!

var html = '<script type="text/javascript">';
html += 'window.location = "' + urlToRedirect +'"';
html += '</script>';
$( '.wrapper' ).append( html );