javascript - Test that a function calls another function in an ES6 module with Sinon.js - Stack Overflow

I want to test that a function in an ES6 module calls another function using Sinon.js. Here's the

I want to test that a function in an ES6 module calls another function using Sinon.js. Here's the basic layout of what I'm doing:

foo.js

export function bar() {
 baz();
}

export function baz() {
 ...
}

test.js

import sinon from 'sinon';
import * as Foo from '.../foo';

describe('bar', function() {
  it('should call baz', function() {
    let spy = sinon.spy(Foo, 'baz');
    spy.callCount.should.eql(0);

    Foo.bar();

    spy.calledOnce.should.eql(true);
  });
}); 

But the spy does not pick up the call to baz(). Is there some other way I can set up the module or the test to allow sinon to pick this up? My alternative is to make some basic assertion on something baz does, but I obviously don't want to be doing that.

From what I've seen online I'm wondering if this is even possible with the code laid out as-is or if I need to restructure it to get what I want.

I want to test that a function in an ES6 module calls another function using Sinon.js. Here's the basic layout of what I'm doing:

foo.js

export function bar() {
 baz();
}

export function baz() {
 ...
}

test.js

import sinon from 'sinon';
import * as Foo from '.../foo';

describe('bar', function() {
  it('should call baz', function() {
    let spy = sinon.spy(Foo, 'baz');
    spy.callCount.should.eql(0);

    Foo.bar();

    spy.calledOnce.should.eql(true);
  });
}); 

But the spy does not pick up the call to baz(). Is there some other way I can set up the module or the test to allow sinon to pick this up? My alternative is to make some basic assertion on something baz does, but I obviously don't want to be doing that.

From what I've seen online I'm wondering if this is even possible with the code laid out as-is or if I need to restructure it to get what I want.

Share Improve this question asked Jan 31, 2016 at 7:22 kurtmarcinkkurtmarcink 1452 silver badges7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

You're right in thinking this isn't possible with the way the module is currently structured.

When the code is executed, the baz reference inside function bar is resolved against the local implementation. You can't modify that since outside of the module code there's no access to the internals.

You do have access to exported properties, but you can't mutate these and so you can't affect the module.

One way to change that is using code like this:

let obj = {};
obj.bar = function () {
 this.baz();
}

obj.baz = function() {
 ...
}

export default obj;

Now if you override baz in the imported object you will affect the internals of bar.

Having said that, that feels pretty clunky. Other methods of controlling behaviors exist such as dependency injection.

Also, you should consider whether or not you actually care if baz was called. In standard "black-box testing", you don't care how something is done, you only care what side effects it generated. For that, test if the side effects you expected happened and that nothing else was done.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743731184a4497461.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信