Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jasmine.any(Array) breaks when vm.runInNewContext is used #417

Open
chriswininger opened this issue May 6, 2017 · 0 comments
Open

jasmine.any(Array) breaks when vm.runInNewContext is used #417

chriswininger opened this issue May 6, 2017 · 0 comments

Comments

@chriswininger
Copy link

Observer the below setup.
If code which calls a spy is running with vm.runInNewContext (something I often do to stub out modules included through require in the code I am testing), jasmine.any(Array) returns false when passing []. Interestingly new Array() works.

It seems like jasmine.any must be relying on isntanceof Array because [] instanceof Array will return false inside vm.runInNewContext where Array.isArray([]) will function correctly.

Perhaps there is a better way to implement jasmine.any(Array) or some other test I can use?

const vm = require('vm');

describe('jasmine.any(Array)', function() {
  var mockFunc;

  beforeEach(function() {
    mockFunc = jasmine.createSpy();
   });

  it('handles instanceof Array normally', function() {
        // this will pass
        mockFunc([])
        expect(mockFunc).toHaveBeenCalled();
        expect(mockFunc).toHaveBeenCalledWith(jasmine.any(Array));
   });

  it('handles instanceof Array normaly with vm.runInNewContext when new Array is used', function() {
        // this will pass
        vm.runInNewContext('mockFunc(new Array())', { mockFunc: mockFunc, Array: Array });
        expect(mockFunc).toHaveBeenCalled();
        expect(mockFunc).toHaveBeenCalledWith(jasmine.any(Array));
  });

  it('handles instanceof Array normaly with vm.runInNewContext when [] is used', function() {
        // this will fail
        vm.runInNewContext('mockFunc([])', { mockFunc: mockFunc, Array: Array });
        expect(mockFunc).toHaveBeenCalled();
        expect(mockFunc).toHaveBeenCalledWith(jasmine.any(Array));
  });

 });

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

No branches or pull requests

1 participant