fetch-mock

Unable to mock node-fetch using fetch-mock

点点圈 提交于 2021-02-11 05:33:24
问题 I am trying to do unit testing for a simple function which sends a get request, receives a response and then returns a promise object with the success or the failure message. Following is the function: module.exports.hello = async (event, context) => { return new Promise((resolve, reject) => { fetch("https://httpstat.us/429", { headers: { 'Content-Type': 'application/json' } }).then(response => { console.log(response); if (response.status == 200) { return response; } else { throw Error

How to mock several gets in fetch-mock?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 18:05:00
问题 I'm testing my react components and I want to mock several get operations. What I want to do is something like: test(`Created correctly`, async () => { fetchMock.get(`*`, JSON.stringify(FIRSTGETOBJ)); fetchMock.get(`*`, JSON.stringify(SECONDGETOBJ)); fetchMock.get(`*`, JSON.stringify(THIRDGETOBJ)); //... } The url for each get is the same, but the payload changes. However, using the code above I will get: Error: Adding route with same name as existing route. See `overwriteRoutes` option. How