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

275
Vistas
Stubbing Fetch Call - response.json no invocará

Estoy tratando de bloquear una llamada de búsqueda con sinon y sinon-stub-promise . Estoy bastante cerca... pero no estoy seguro de por qué no invoca el método response.json() que creé.

 function toJSON(response) { console.log(response); return response.json(); } function init() { fetch(darkWeatherUrl) .then(toJSON) .then(computeHours) .then(sendAlerts) .catch((e) => { console.log('init error ' + e); }); }

 describe('lifx alert test', ()=> { it('should run fetch', ()=> { var fetch = sinon.stub().returnsPromise(); var body = { "hourly": { data: [ { time: 1493413200, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 297.17 }, { time: 1493416800, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.89 }, { time: 1493420400, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.73 }, { time: 1493424000, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.31 } ] } }; function json() { return body; } var response = {}; response.json = json; fetch.resolves(response); var init = proxy('../index.js', {'node-fetch': fetch}); init(); fetch.should.have.been.called; }); });

 lifex alert test { json: [Function: json] } [ { time: 1493413200, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 297.17 }, { time: 1493416800, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.89 }, { time: 1493420400, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.73 }, { time: 1493424000, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.31 } ] init error TypeError: response.json is not a function â should run fetch 1 passing
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Ver actualización en la parte inferior también

sinon-stub-promise y lo mantuve nativo sinon:

 describe('lifx alert test', ()=> { var fetchStub; beforeEach(() => { fetchStub = sinon.stub(); }); it('should run fetch', ()=> { var body = { "hourly": { data: [ { time: 1493413200, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 297.17 }, { time: 1493416800, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.89 }, { time: 1493420400, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.73 }, { time: 1493424000, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.31 } ] } }; var response = { json: () => { return body } }; fetchStub.returns(Promise.resolve(response)); var init = proxy('../index.js', {'node-fetch': fetchStub}); init(); fetchStub.should.have.been.called; });

ACTUALIZAR

Conseguí que funcionara con sinon-stub-promise con el atributo global de proxyquire:

 describe('lifx alert test', ()=> { it('should run fetch', ()=> { var fetch = sinon.stub().returnsPromise(); var body = { "hourly": { data: [ { time: 1493413200, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 297.17 }, { time: 1493416800, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.89 }, { time: 1493420400, icon: 'clear-day', precipIntensity: 0, precipProbability: 0, ozone: 296.73 }, { time: 1493424000, icon: 'clear-day', precipIntensity: 0, precipProbability: 50, ozone: 296.31 } ] } }; var response = { json: () => { return body } }; fetch['@global'] = true; fetch.resolves(response); proxy('../index.js', {'node-fetch': fetch}); fetch.should.have.callCount(3); }); });
over 4 years ago · Santiago Trujillo 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