Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!