jasmine-node icon indicating copy to clipboard operation
jasmine-node copied to clipboard

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

Open chriswininger opened this issue 7 years ago • 0 comments

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));
  });

 });

chriswininger avatar May 06 '17 18:05 chriswininger