So I have something that looks like the following:
fetch(url)
.then((response) => {
// If the Content-Type is not text/html throw an error
const contentType = response.headers.get('content-type');
if (!contentType.includes('text/html')) {
throw new Error('Content-Type was not of type text/html');
}
// Read the response as text.
return response.text();
})
.then((data) => sendResponse(res, data))
.catch(() => sendError(res, errorResponse));
I am able to test the .then((data) => sendResponse(res, data)) just fine with
return fetch(url).then((resp) => {
expect(resp.headers.get('content-type')).toEqual(responseInit.headers['content-type']);
return expect(resp).rejects.toThrow('Content-Type was not of type text/html');
});
So I have something that looks like the following:
I am able to test the
.then((data) => sendResponse(res, data))
just fine withHowever, there aren't really any examples on how to test
.catch
withjest-fetch-mock
. I tried doingand
But now luck.