Created
July 5, 2019 07:09
-
-
Save toni-sharpe/6bfbea45be08fd1ddc890f9b7bbc4ecc to your computer and use it in GitHub Desktop.
Jest Mock Module Example
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
// Path to the module to mock | |
// Then function which is called when the module is accessed | |
jest.mock("../mocked-module", () => ({ | |
// Allows for multiple exports and a default to be mocked out individually | |
__esModule: true, | |
// Immediately returning the args like this is a substitute for inspecting the calledWith | |
// on a mock (Jest mock does not allow access to surrounding variables like a closure would) | |
default: jest.fn(args => args), | |
API_METHOD: { POST: "POST" } | |
})); | |
// More simple example just returns a named export from a module | |
jest.mock('../mocked-module', () => ({ | |
upsert: (payload: TSType) => payload.changed | |
})) | |
// Even simpler example just gets around an imported module causing an error outside the unit | |
// you're testing | |
jest.mock('../mocked-module', jest.fn()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment