Skip to content

Instantly share code, notes, and snippets.

@sabesansathananthan
Created September 14, 2021 17:06
Show Gist options
  • Save sabesansathananthan/94b252a964f50abe5aba79d8df17e2a7 to your computer and use it in GitHub Desktop.
Save sabesansathananthan/94b252a964f50abe5aba79d8df17e2a7 to your computer and use it in GitHub Desktop.
mock network request in Jest
import { counter } from "./demo";
import { request } from "./demo/wrap-request";
jest.mock("./demo/wrap-request");
describe("Simple mock", () => {
it("test success", () => {
request.mockResolvedValue({ result: 0 });
return counter(1, 2).then(res => {
expect(res).toStrictEqual({ result: 0, msg: "success" });
});
});
it("test need login", () => {
request.mockResolvedValue({ result: -100 });
return counter(1, 2).then(res => {
expect(res).toStrictEqual({ result: -100, msg: "need login" });
});
});
it("test something wrong", () => {
request.mockResolvedValue({ result: 1111111 });
return counter(1, 2).then(res => {
expect(res).toStrictEqual({ result: -999, msg: "fail" });
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment