Skip to content

Instantly share code, notes, and snippets.

@toni-sharpe
Created July 5, 2019 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toni-sharpe/6bfbea45be08fd1ddc890f9b7bbc4ecc to your computer and use it in GitHub Desktop.
Save toni-sharpe/6bfbea45be08fd1ddc890f9b7bbc4ecc to your computer and use it in GitHub Desktop.
Jest Mock Module Example
// 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