You can use babel-plugin-rewire (npm install --save-dev babel-plugin-rewire
)
And then in test.js
use the __Rewire__
function on the imported module to replace the function in that module:
// test.js
import sinon from 'sinon'
import cap from 'cap'
describe('cap', () => {
it('should bar', () => {
const barStub = sinon.stub().returns(42);
cap.__Rewire__('bar', barStub); // <-- Magic happens here
cap('some');
expect(barStub.calledOnce).to.be.true;
});
});
Be sure to add rewire
to your babel plugins in .babelrc
:
// .babelrc
{
"presets": [
"es2015"
],
"plugins": [],
"env": {
"test": {
"plugins": [
"rewire"
]
}
}
}
Lastly, as you can see the babel-plugin-rewire
plugin is only enabled in the test environment, so you should call you test runner with the BABEL_ENV
environment variable set to test
(which you're probably doing already):
env BABEL_ENV=test mocha --compilers js:babel-core/register test-example.js
Note: I couldn't get babel-plugin-rewire-exports
to work.
export default
in yourcap.js
file have anything to do with your question? Does it matter if it isdefault
or not? – Centipede