Skip to content

Instantly share code, notes, and snippets.

@cladley
Last active August 19, 2020 14:17
Show Gist options
  • Save cladley/b9d2138f4d249f1eadf7854b708f0d16 to your computer and use it in GitHub Desktop.
Save cladley/b9d2138f4d249f1eadf7854b708f0d16 to your computer and use it in GitHub Desktop.
Jest Mocking examples
// To mock a function
// utils has a function called getWinner that takes two strings
// as arguments and return one of them as the winner. It
// is used internally by thumbWar. Problem here is that the original
// function gets clobbered by our mock, so we have to manually
// reassign the original back after the test has run.
// This is called monkey patching and only works with commonjs modules
const utils = require('./utils');
const thumbWar = require('./thumb-war');
test('returns winner', () => {
const originalGetWinner = utils.getWinner;
utils.getWinner = jest.fn((p1, p2) => p1);
const winner = thumbWar('Ken', 'Colin');
expect(utils.getWinner.mock.calls).toEqual([
['Ken', 'Colin']
]);
// Clean up
utils.getWinner = originalGetWinner;
});
////////////////////////////////////////////////////////
// To mock a function a better way is to use jest.spyOn
// which keeps track of the original implementation.
// This is called monkey patching and only works with commonjs modules
const utils = require('./utils');
const thumbWar = require('./thumb-war');
test('returns winner', () => {
jest.spyOn(utils, 'getWinner')
utils.getWinner.mockImplementation((p1, p2) => p1)
const winner = thumbWar('Ken', 'Colin');
expect(utils.getWinner.mock.calls).toEqual([
['Ken', 'Colin']
]);
// cleanup
utils.getWinner.mockRestore();
});
///////////////////////////////////////////////////////////
// But we can mock out an entire module with jest.mock
const thumbWar = require('../thumb-war')
jest.mock('../utils', () => {
return {
getWinner: jest.fn((p1, p2) => p1)
}
})
test('returns winner', () => {
const winner = thumbWar('Kent C. Dodds', 'Ken Wheeler')
expect(winner).toBe('Kent C. Dodds')
expect(utilsMock.getWinner.mock.calls).toEqual([
['Kent C. Dodds', 'Ken Wheeler'],
['Kent C. Dodds', 'Ken Wheeler']
])
// cleanup
utilsMock.getWinner.mockReset()
})
///////////////////////////////////////////////////
// If you want to mock part of a module but have rest of
// exports not set
//////////////////////////////
const OPERATIONS = {
SEND_EMAIL: 'SEND_EMAIL',
SEND_PUSH_NOTIFICATION: 'SEND_PUSH_NOTIFICATION'
};
function createEmailNotification(to, subject, content) {
}
function createPushNotification(to, title, content) {
}
async function sendNotification(action) {
// send something to an API
}
module.exports = {
OPERATIONS,
createEmailNotification,
createPushNotification,
sendNotification
};
// Here we are only mocking sendNotification and leaving the rest of
// the exports in the module un touched.
jest.mock('./notifications', () => ({
...jest.requireActual('./notifications'),
sendNotification: jest.fn(async () => {}),
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment