Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
What is the simplest way to mock a fetch call that is only meant to run in the browser with jest?

If I'm testing a function that makes a fetch call, and all the fetch calls I have in the project are only meant to run in the browser. Do I still need to have some package installed on node and import it to be able to mock that fetch call?

A simple scenario like this throws ReferenceError: fetch is not defined in the Jest test:

// fetcKey.js

export async function fetchKey() {
    try {

        const key = await fetch('http://localhost:3000/key');
        return (await key.text());
    }
    catch(error){
        throw error;
    }
}

//fetchKey.test.js

import fetchKey from './fetchKey'

test('Checks fetchKey return', () => {

    expect(fetchKey()).not.toThrow();

})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should be able to use node-fetch:

import fetch from 'node-fetch';

(Note it's an ECMAScript Module and cannot be imported with require, as mentioned in their documentation).

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda