Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mocking es6 imports in javascript unit tests
import {assert} from 'chai';
import sinon from 'sinon';
import mockRequire from 'mock-require';
describe('My module', () => {
let module; // module under test
let mocks;
beforeEach(() => {
mocks = {
service: {
load: sinon.stub().returns('success')
}
};
// mock es6 import (tip: use es5 path)
mockRequire('../../path/to/service', mocks.service);
// require es6 module (tip: use es5 path)
module = require('../../path/to/moudle');
});
afterEach(() => {
// remove all registered mocks
mockRequire.stopAll();
});
describe('Initialisation', () => {
it('should have an init function', () => {
assert.isFunction(module.init);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment