Last active
February 25, 2016 11:49
-
-
Save ctrlplusb/faa6712002c01e48253b to your computer and use it in GitHub Desktop.
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
// This is a basic demonstration of using mockery alongside mocha/chai | |
// for unit testing in node. |
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
// This is the module we will test. | |
import bob from './bob'; | |
function bar() { | |
return bob() | |
} | |
export default bar; |
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
// This is a dependency consumed by our module that we will mock. | |
function bob() { | |
return 'real bob'; | |
} | |
export default bob; |
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
// The test for our module | |
import { expect } from 'chai'; | |
import { configureMockery } from '_common/tests'; | |
describe('When executing with a mocked bob', () => { | |
const bobStub = () => `mocked bob result`; | |
configureMockery((m) => { | |
m.registerMock('./bob-dependency', bobStub); | |
}); | |
it('Then the mock result should be returned', () => { | |
const { default: subject } = require('./bar-module'); | |
const expected = 'mocked bob result'; | |
const actual = subject(); | |
expect(actual).to.equal(expected); | |
}); | |
}); |
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
// This is our test helper | |
import mockery from 'mockery'; | |
export const configureMockery = (conf) => { | |
before(() => { | |
mockery.enable({ | |
useCleanCache: true, | |
warnOnReplace: false, | |
warnOnUnregistered: false | |
}); | |
conf(mockery); | |
}); | |
after(() => { | |
mockery.deregisterAll(); | |
mockery.disable(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment