Created
September 15, 2021 18:50
-
-
Save sabesansathananthan/bb008ef37497187d87b549aaf66d0f68 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.mockImplementation(() => Promise.resolve({ result: 0 })); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: 0, msg: "success" }); | |
}); | |
}); | |
it("test need login", () => { | |
request.mockImplementation(() => Promise.resolve({ result: -100 })); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: -100, msg: "need login" }); | |
}); | |
}); | |
it("test something wrong", () => { | |
request.mockImplementation(() => Promise.resolve({ result: 1111111 })); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: -999, msg: "fail" }); | |
}); | |
}); | |
it("test param transform", () => { | |
return new Promise(done => { | |
request.mockImplementation(({ data }) => { | |
expect(data).toStrictEqual({ id: 1, operate: 1 }); | |
done(); | |
return Promise.resolve({ result: 0 }); | |
}); | |
counter(1, 1000); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment