Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rickhanlonii
Last active December 22, 2020 14:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickhanlonii/4195b5a132f8c7bc47ba147cdcba1c05 to your computer and use it in GitHub Desktop.
Save rickhanlonii/4195b5a132f8c7bc47ba147cdcba1c05 to your computer and use it in GitHub Desktop.
Mock Test with jest.spyOn and mockRestore
import * as app from "./app";
import * as math from "./math";
test("calls math.add", () => {
const addMock = jest.spyOn(math, "add");
// override the implementation
addMock.mockImplementation(() => "mock");
expect(app.doAdd(1, 2)).toEqual("mock");
// restore the original implementation
addMock.mockRestore();
expect(app.doAdd(1, 2)).toEqual(3);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment