How to mock several gets in fetch-mock?

justHelloWorld picture justHelloWorld · Feb 28, 2018 · Viewed 9.9k times · Source

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 can I do this?

Answer

Hatch picture Hatch · Mar 28, 2018

Use overwriteRoutes option

test(`Created correctly`, async () => {
    fetchMock.get(`*`, JSON.stringify(FIRSTGETOBJ));
    fetchMock.get(`*`, JSON.stringify(SECONDGETOBJ), { overwriteRoutes: false });
    fetchMock.get(`*`, JSON.stringify(THIRDGETOBJ), { overwriteRoutes: false });

    //...
}