Skip to content

Instantly share code, notes, and snippets.

@jcollum
Created October 11, 2018 23:35
Show Gist options
  • Save jcollum/22a323e765216b8aff16443f4c224d18 to your computer and use it in GitHub Desktop.
Save jcollum/22a323e765216b8aff16443f4c224d18 to your computer and use it in GitHub Desktop.
Mock a few functions in a module with many exports (Jest 22)
// file __mocks__/selectors.js
// this file mocks the selectors.js module
const actual = require.requireActual('../selectors')
const mockBase = jest.genMockFromModule('../selectors');
// eslint-disable-next-line security/detect-object-injection,no-return-assign
Object.keys(actual).forEach(key => (mockBase[key] = actual[key]));
mockBase.getCountry = jest.fn();
mockBase.getUser = jest.fn();
module.exports = mockBase;
jest.mock('../../selectors'); // this will be hoisted to top of file if you use babel-jest (22)
import { getOrders, getLanguage, getUser, getCountry } from '../../selectors';
// verify your mocks are mocked and your functions are not
expect(getOrders.mock).toBeFalsy()
expect(getLanguage.mock).toBeFalsy()
expect(getCountry.mock).toBeTruthy()
expect(getUser.mock).toBeTruthy()
// describe and test blocks here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment