Soy nuevo en las pruebas unitarias usando Jest, pero básicamente tengo un archivo client.js con este aspecto:
function add(a, b) { return a + b; } const fetcher = () => { return fetch('http://localhost:3000/').then(res => res.json); } module.exports = {add, fetcher}; El client.test.js es el siguiente:
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); }) }); Cuando intento ejecutar pruebas, arroja un error ReferenceError: fetch is not defined . ¿Cómo puedo solucionar esto?
O:
fetch en algún lugar (por ejemplo, cargando el módulo de recuperación de nodos que está disponible en NPM)--experimental-fetch cuando ejecute Node (y asegúrese de que sea la versión 17.5 o posterior )