Skip to content

Instantly share code, notes, and snippets.

@ctrlplusb
Last active February 25, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ctrlplusb/faa6712002c01e48253b to your computer and use it in GitHub Desktop.
Save ctrlplusb/faa6712002c01e48253b to your computer and use it in GitHub Desktop.
// This is a basic demonstration of using mockery alongside mocha/chai
// for unit testing in node.
// This is the module we will test.
import bob from './bob';
function bar() {
return bob()
}
export default bar;
// This is a dependency consumed by our module that we will mock.
function bob() {
return 'real bob';
}
export default bob;
// 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 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