jefflau / jest-fetch-mock

Jest mock for fetch
MIT License
883 stars 116 forks source link

How to mock with cookies? #118

Open Bessonov opened 5 years ago

Bessonov commented 5 years ago

Thank you for this great library!

I want to test a scenario, where a higher order utility function get in second request a :cookie: . To set headers seems not be handled by jest-fetch-mock:

fetchMock.mockResponseOnce(
    'body',
    {
        status: 200,
        headers: {
            'set-cookie': 'my=cookie; Path=/;'
        }
    }
)

If I use react-cookie, then I can set the cookie, but for all requests, not second one.

Is there any way to workaround without touch the higher order utility function?

hacknlove commented 2 years ago

I am having the same issue

Have you managed to solve it?

hacknlove commented 2 years ago

This is working for me

        fetchMock.mockResponse(() => Promise.resolve({
            body: JSON.stringify({ ok: true }),
            headers: [
                ['set-cookie', 'some cookie'],
                ['set-cookie', 'more cookies']
            ]                
        }));