Last active
November 4, 2019 22:20
-
-
Save rickhanlonii/dcbdc0f10a82646fba474711e9a13193 to your computer and use it in GitHub Desktop.
Mock Implementation
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
test("mock implementation", () => { | |
const mock = jest.fn(() => "bar"); | |
expect(mock("foo")).toBe("bar"); | |
expect(mock).toHaveBeenCalledWith("foo"); | |
}); | |
test("also mock implementation", () => { | |
const mock = jest.fn().mockImplementation(() => "bar"); | |
expect(mock("foo")).toBe("bar"); | |
expect(mock).toHaveBeenCalledWith("foo"); | |
}); | |
test("mock implementation one time", () => { | |
const mock = jest.fn().mockImplementationOnce(() => "bar"); | |
expect(mock("foo")).toBe("bar"); | |
expect(mock).toHaveBeenCalledWith("foo"); | |
expect(mock("baz")).toBe(undefined); | |
expect(mock).toHaveBeenCalledWith("baz"); | |
}); | |
test("mock return value", () => { | |
const mock = jest.fn(); | |
mock.mockReturnValue("bar"); | |
expect(mock("foo")).toBe("bar"); | |
expect(mock).toHaveBeenCalledWith("foo"); | |
}); | |
test("mock promise resolution", () => { | |
const mock = jest.fn(); | |
mock.mockResolvedValue("bar"); | |
expect(mock("foo")).resolves.toBe("bar"); | |
expect(mock).toHaveBeenCalledWith("foo"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment