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

327
Vistas
Stub of Sinon using axios returns undefined

I'm testing using sinon with axios.

// index.js
{
    .. more code
    const result = await axios.get("http://save")
    const sum = result.data.sum
}

And I made a test code by sinon and supertest for e2e test.

// index.test.js
describe('ADMINS GET API, METHOD: GET', () => {
  it('/admins', async () => {
    sandbox
      .stub(axios, 'get')
      .withArgs('http://save')
      .resolves({sum: 12});

    await supertest(app)
      .get('/admins')
      .expect(200)
      .then(async response => {
        expect(response.body.code).toBe(200);
      });
  });
});

But when I test it, it gives me this result.

 // index.js
    {
        .. more code
        const result = await axios.get("http://save")
        const sum = result.data.sum 
        console.log(sum) // undefined
    }

I think I resolved response. But it doesnt' give any response. It just pass axios on supertest.
How can I return correct data in this case?
Thank you for reading it.

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

0

The resolved value should be { data: { sum: 12 } }.

E.g.

index.js:

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/admins', async (req, res) => {
  const result = await axios.get('http://save');
  const sum = result.data.sum;
  res.json({ code: 200, sum });
});

module.exports = { app };

index.test.js:

const supertest = require('supertest');
const axios = require('axios');
const sinon = require('sinon');
const { app } = require('./index');

describe('ADMINS GET API, METHOD: GET', () => {
  it('/admins', async () => {
    const sandbox = sinon.createSandbox();
    sandbox
      .stub(axios, 'get')
      .withArgs('http://save')
      .resolves({ data: { sum: 12 } });

    await supertest(app)
      .get('/admins')
      .expect(200)
      .then(async (response) => {
        sinon.assert.match(response.body.code, 200);
        sinon.assert.match(response.body.sum, 12);
      });
  });
});

test result:

  ADMINS GET API, METHOD: GET
    ✓ /admins


  1 passing (22ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |     100 |      100 |     100 |     100 |                   
 index.js |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
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