Estoy probando usando sinon con axios.
// index.js { .. more code const result = await axios.get("http://save") const sum = result.data.sum }E hice un código de prueba por sinon y supertest para la prueba e2e.
// 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); }); }); });Pero cuando lo pruebo, me da este resultado.
// index.js { .. more code const result = await axios.get("http://save") const sum = result.data.sum console.log(sum) // undefined } Creo que resolví la respuesta. Pero no da ninguna respuesta. Simplemente pasó axios en supertest.
¿Cómo puedo devolver datos correctos en este caso?
Gracias por leerlo.
El valor resuelto debe ser { data: { sum: 12 } } .
P.ej
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); }); }); });resultado de la prueba:
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 | ----------|---------|----------|---------|---------|-------------------