'Jest Fetch reference error: UnhandledPromiseRejection when running a test suite not containing fetch
I have a couple of files below: When I run jest I get this error:
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "ReferenceError: fetch is not defined".]
ApiHandler.test.ts
import { formatDateOptions } from "/ApiHandler";
describe("formatDateOptions()", () => {
it("Describe bla bla", () => {
const inputDate = {Tables[{a:1}]}
const expected = [ {key:1,text:"a",}]
const result = formatDateOptions(inputDate);
expect(result).toEqual(expected);
});
});
ApiHandler.ts
export const formatDateOptions = (data) => {
const dates = [];
data.Tables[0].Rows.map((x) => dates.push({ key: x[0], text: x[0] }));
return dates;
};
Even though fetch is not being used here, I am getting the above error.For more info, fetch is being used totally unrelated before formatDateOptions is being called Sample where fetch is being used
ApiHandler.ts (same file)
export const setDatePickerOptions = async()=> {
try {
const [dates, platformSignal] = await Promise.all([
dateFilters(platform),
platformSignal(platform),
]);
try {
const [datesJson, platformSignalJson] = await Promise.all([
dates.json(),
platformSignal.json(),
]);
const responseData = formatDateOptions(datesJson);
} catch (e) {
console.error(e);
}
} catch (e) {
console.error(e);
}
};
export const dateFilters = async(platform)=> {
try {
const dates = await fetchWrapper(platform);
return dates;
} catch (error) {
console.log(error);
}
};
export async function fetchWrapper(platform){
try {
const response = await fetch(url, {
method: "post",
headers,
body: JSON.stringify({
csl: platform
}),
});
return response;
} catch (e) {
console.log(e);
}
}
I even imported node-fetch in APihandler.ts but still that is not working. Please help me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|