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

643
Views
Why do I get reference error for fetch when I do unit tests using Jest?

I'm new to unit tests using Jest but basically I have a file client.js looking like this:

function add(a, b) {
    return a + b;
}
const fetcher = () => {

    return fetch('http://localhost:3000/').then(res => res.json);
}


module.exports = {add, fetcher};

The client.test.js is as follows:



const client = require('../client');

test('Testing addition', () => {
    expect(client.add(1, 2)).toBe(3);
});

test('Yet another addition', () => {
    expect(client.add(3, 10)).toBe(13);
});



test('Testing a promise', () => {
    return client.fetcher().then(data => {
        expect(data).toBe(1824);
    })
});

When I try to run tests it throws an error ReferenceError: fetch is not defined. How do I fix this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Either:

  • Define a fetch function somewhere (e.g. by loading the node-fetch module that is available on NPM)
  • Use the --experimental-fetch flag when you run Node (and make sure it is version 17.5 or newer
about 4 years ago · Juan Pablo Isaza Report
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!