Last active
December 22, 2020 14:24
-
-
Save rickhanlonii/4195b5a132f8c7bc47ba147cdcba1c05 to your computer and use it in GitHub Desktop.
Mock Test with jest.spyOn and mockRestore
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
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