Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

1.6.2 - mocking $window in unit tests throws undefined is not an object (evaluating '$window.angular.callbacks') #15685

Closed
mattlewis92 opened this issue Feb 7, 2017 · 5 comments

Comments

@mattlewis92
Copy link
Contributor

mattlewis92 commented Feb 7, 2017

Note: for support questions, please use one of these channels: https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#question. This repository's issues are reserved for feature requests and bug reports.

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
1.6.2 only - if you mock the $window object in tests angular throws the error

undefined is not an object (evaluating '$window.angular.callbacks')

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://plnkr.co or similar (template: http://plnkr.co/edit/tpl:yBpEi4).

Mock out the $window like so:

let $window;

beforeEach(angular.mock.module('myModule', $provide => {
    $window = {
      innerHeight: 100
    };
    $provide.constant('$window', $window);
}));

Any tests that you try to run after this throw that error. This was working OK in < 1.6.2.

What is the expected behavior?
It should be possible to mock out the $window object in tests.

What is the motivation / use case for changing the behavior?
$window exists so that you can mock it.

Which versions of AngularJS, and which browser / OS are affected by this issue? Did this work in previous versions of AngularJS? Please also test with the latest stable and snapshot (https://code.angularjs.org/snapshot/) versions.
1.6.2 only

Other information (e.g. stacktraces, related issues, suggestions how to fix)
I guess it was caused by this commit: 0377c6f

@gkalpak
Copy link
Member

gkalpak commented Feb 7, 2017

Can you provide a reproduction. We haven't changed anything wrt to mocking $window, but it is possible that previously (accidentally) silenced are now surfaced.

@mattlewis92
Copy link
Contributor Author

Sure thing, I'm not too sure how I'd do this in a plunker, is a runnable repo ok?

mattlewis92 pushed a commit to mattlewis92/angular.js that referenced this issue Feb 7, 2017
@gkalpak
Copy link
Member

gkalpak commented Feb 7, 2017

It's OK. I have a minimal reproduction (based on an early prototype from @tommck).

describe('My Angular App', function() {
  beforeEach(module(function($provide) {
    $provide.value('$window', {});
  }));

  it('should redirect to the configured URL on post-logout', inject(function($http) {
    expect(true).toBeTruthy();
  }));
});

Not sure what causes it yet. Looking into it.

@mattlewis92
Copy link
Contributor Author

@gkalpak It was easier for me to just to send a PR that fixes it 😄 #15686

@gkalpak
Copy link
Member

gkalpak commented Feb 7, 2017

I think it is this commit. Previously, angular-mocks would mok out $httpBackend completely. Now, it decorates it (in order to support some specific usecase were ngMock and ngMockE2E are used together), which means that the "real" $httpBackend is instantiated and then the mock version.

Since the real $httpBackend relies on $jsonpCallbacks, which in turn relies on $window.angular, you get an error when $window.angular is not defined.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants