Skip to content

Instantly share code, notes, and snippets.

@keithstric
Created February 17, 2020 16:04
Show Gist options
  • Save keithstric/83b8e682485747851f9b2747bffb149a to your computer and use it in GitHub Desktop.
Save keithstric/83b8e682485747851f9b2747bffb149a to your computer and use it in GitHub Desktop.
Adventures in Unit Testing 3
const someFunction = require('./index');
const rewire = require('rewire');
let app;
beforeEach(() => {
someFunction();
app = rewire('./index.js');
});
test('it should increment evtCount', () => {
const evtCount = app.__get__('evtCount');
const somePrivateFunc = app.__get__('somePrivateFunction');
somePrivateFunc();
expect(evtCount).toEqual(1);
});
test('it should call somePrivateFunction on the foo event', () => {
const privateFuncMock = jest.fn();
app.__set__('somePrivateFunction', privateFuncMock);
const emitter = app.__get__('customEmitter');
emitter.emit('foo', 'bar');
expect(privateFuncMock).toHaveBeenCalled();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment