Created
September 14, 2021 17:06
-
-
Save sabesansathananthan/94b252a964f50abe5aba79d8df17e2a7 to your computer and use it in GitHub Desktop.
mock network request in Jest
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
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