Last active
July 13, 2017 22:27
-
-
Save fedecarg/10b4e345bd46e0e2cca262e1f3d268a3 to your computer and use it in GitHub Desktop.
Mocking es6 imports in javascript unit tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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