Skip to content

Instantly share code, notes, and snippets.

@jeffmo
Last active August 29, 2015 14:01
Show Gist options
  • Save jeffmo/8d33a29fd5f3e58f0ace to your computer and use it in GitHub Desktop.
Save jeffmo/8d33a29fd5f3e58f0ace to your computer and use it in GitHub Desktop.
// SomeEmitter.js
function SomeEmitter() {}
SomeEmitter.prototype.on = function() {};
SomeEmitter.prototype.emit = function() {};
module.exports = SomeEmitter;
// __mocks__/SomeEmitter.js
/**
* This is a manual mock that Jest will look for before it
* tries to automatically generate a mock itself.
*
* For more details:
* http://facebook.github.io/jest/docs/manual-mocks.html
*/
var SomeEmitter = require.generateMock('../SomeEmitter');
SomeEmitter.prototype.on.mockReturnThis();
SomeEmitter.prototype.emit.mockReturnThis();
module.exports = SomeEmitter;
// __tests__/MyThingThatUsesSomeEmitter-test.js
describe('This is a test', function() {
it('is only a test', function() {
var SomeEmitter = require('../SomeEmitter');
var emitter = new SomeEmitter;
emitter.on('foo').emit('bar');
expect(emitter.on).toBeCalledWith('foo');
expect(emitter.emit).toBeCalledWith('bar');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment