Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

655
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda