Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

325
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!