How to mock several gets in fetch-mock?
Asked Answered
J

1

17

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?

Journeywork answered 28/2, 2018 at 13:54 Comment(0)
A
20

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 });

    //...
}
Apatite answered 28/3, 2018 at 12:45 Comment(1)
For me ([email protected]), I had to include { repeat: 1 } in each of the mock options - otherwise it would continue to give me the first result.Emerald

© 2022 - 2024 — McMap. All rights reserved.