Created
September 15, 2021 15:00
-
-
Save sabesansathananthan/af27076a14719b45e3e6192f1641aebd 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 * as request from "./demo/wrap-request"; | |
jest.mock("./demo/wrap-request", () => { | |
let hook = () => ({ result: 0 }); | |
return { | |
setHook: cb => (hook = cb), | |
request: (...args) => { | |
return new Promise(resolve => { | |
resolve(hook(...args)); | |
}); | |
}, | |
}; | |
}); | |
describe("Simple mock", () => { | |
it("test success", () => { | |
request.setHook(() => ({ result: 0 })); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: 0, msg: "success" }); | |
}); | |
}); | |
it("test need login", () => { | |
request.setHook(() => ({ result: -100 })); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: -100, msg: "need login" }); | |
}); | |
}); | |
it("test something wrong", () => { | |
request.setHook(() => ({ result: 1111111 })); | |
return counter(1, 2).then(res => { | |
expect(res).toStrictEqual({ result: -999, msg: "fail" }); | |
}); | |
}); | |
it("test param transform", () => { | |
return new Promise(done => { | |
request.setHook(({ data }) => { | |
expect(data).toStrictEqual({ id: 1, operate: 1 }); | |
done(); | |
return { result: 0 }; | |
}); | |
counter(1, 1000); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment