mercredi 19 août 2020

I'm getting following error in Jest ReferenceError: Response is not defined

First I had a typescript issue that my mocked data doesn't match to Response type. Then I tried to create mock data with Response constructor and got that error.

I have the following code

const HttpRequest = async function (
    url: string,
    options: RequestInit,
): Promise<Response> {
    try {
        return await fetch(url, options);
    } catch (error) {
        return null;
    }
};

export default HttpRequest;

And following test:

const res = new Response();
window.fetch = jest.fn(
    () => new Promise((resolve) => {
        return resolve(res);
    })
);

describe('HttpService', () => {
    it('fetchWithFeedback', async () => {
        const data = await HttpRequest('/api', { method: 'GET' });
        expect(data).toEqual(res);
    });
});

Aucun commentaire:

Enregistrer un commentaire