Thursday, 4 May 2017

How to mock a method thats imported inside of another import

I'm testing in a babel-node environment. I want to mock a method thats used inside of the method I'm importing. The challenging part seems to be that the method I want to mock is imported into the file where the method I want to test resides. I've explored proxyquire, babel-plugin-rewire but I can't seem to get them to work on methods imported inside of other imports. From reading through various github issues I get the feeling that this might be a known limitation/frustration. Should this be possible or am I missing something?

No errors are produced when using proxyquireor babel-plugin-rewire. The method just doesn't get mocked out and it returns the methods normal value.

Here's a generic example of the import situation.

// serviceLayer.js

getSomething(){
  return 'something';
}


// actionCreator.js

import { getSomething } from './serviceLayer.js';

requestSomething(){
  getSomething();  <------- This is what I want to mock
}


// actionCreator.test.js

import test from 'tape';
import {requestSomething} from 'actionCreator.js'

test('should return the mock' , (t) => {
  t.equal(requestSomething());
});



via mpls423

No comments:

Post a Comment