javascript - Sinon Stub not working for exported function - Stack Overflow

Sinon doesn't seem to be stubbing a method from an imported file. Is it to do with exporting const

Sinon doesn't seem to be stubbing a method from an imported file. Is it to do with exporting consts?

I see "Received ORIGINAL MESSAGE" in the console.log.

Main.js

import * as otherActions from 'filters/actions/Other.actions';

describe('filter actions', () => {

    it('should log STUBBED MESSAGE', () => {   
      sinon.stub(otherActions, 'logMessage').callsFake(m => console.log('STUBBED Message'));
      const piled = otherActions.doSomethingAndLogMessage(5, 5);
      piled(message => console.log(`RECEIVED ${message}`), () => {});
    });

});

Other.actions.js

export const logMessage = () => console.log("ORIGINAL MESSAGE");

export const doSomethingAndLogMessage = (categoryId, size) => (dispatch, getState) => {
  dispatch(logMessage());
};

Sinon doesn't seem to be stubbing a method from an imported file. Is it to do with exporting consts?

I see "Received ORIGINAL MESSAGE" in the console.log.

Main.js

import * as otherActions from 'filters/actions/Other.actions';

describe('filter actions', () => {

    it('should log STUBBED MESSAGE', () => {   
      sinon.stub(otherActions, 'logMessage').callsFake(m => console.log('STUBBED Message'));
      const piled = otherActions.doSomethingAndLogMessage(5, 5);
      piled(message => console.log(`RECEIVED ${message}`), () => {});
    });

});

Other.actions.js

export const logMessage = () => console.log("ORIGINAL MESSAGE");

export const doSomethingAndLogMessage = (categoryId, size) => (dispatch, getState) => {
  dispatch(logMessage());
};
Share Improve this question asked Jul 23, 2018 at 15:46 HiddenHidden 7,5025 gold badges48 silver badges55 bronze badges 3
  • What version of sinon are you using? – Andrew Eisenberg Commented Jul 23, 2018 at 19:52
  • 1 I'm using version 5.0.10 – Hidden Commented Jul 24, 2018 at 22:22
  • Does this answer your question? How to stub exported function in ES6? – david_adler Commented Jul 26, 2021 at 8:45
Add a ment  | 

1 Answer 1

Reset to default 5

The problem is occurring because you are stubbing the function in the exported module, when referenced in the module context. You are not stubbing when referencing it raw from inside the module. There are many ways to fix this, but I think all will require you to change your production code a bit.

One suggestion is this:

Other.actions.js

export const logger = { message: () => console.log("ORIGINAL MESSAGE") };

Main.js

import * as otherActions from 'filters/actions/Other.actions';

...
sinon.stub(otherActions.logger, 'message')
  .callsFake(m => console.log('STUBBED Message'));

The important thing is that you create the stub in a context that is available to the module under test.

And another general ment is that typically, you don't want to mock or stub functions or methods in the module you are testing. Typically, the unit of unit testing refers to a module. And so, if you find the need to stub out something in the same module you are testing, then I'd suggest that your module boundaries are not correct.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信