Created
October 11, 2018 23:35
-
-
Save jcollum/22a323e765216b8aff16443f4c224d18 to your computer and use it in GitHub Desktop.
Mock a few functions in a module with many exports (Jest 22)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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