-
-
Save pointerpin/7794becda21425e4efbe181266b71a53 to your computer and use it in GitHub Desktop.
Mock Implementation
This file contains hidden or 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