Sinon/Mocha test with async ajax calls didn't return promises

时光怂恿深爱的人放手 提交于 2019-12-06 13:13:38

It seems there is an open issue on sinon saying it doesn't work with axios, which seems to be the case here.

Axios is not actually calling your fakeserver. It seems to be trying to request /cart literally, that is why you get a timeout.

That user replaced the fakeServer with another lib called nock he mentions it here

There are other libs that allow you to mock requests.

I usually use supertest npm, Github.

Example from Github README

var request = require('supertest');
var express = require('express');

var app = express();

app.get('/user', function(req, res) {
  res.status(200).json({ name: 'tobi' });
});

request(app)
  .get('/user')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '15')
  .expect(200)
  .end(function(err, res) {
    if (err) throw err;
  });

It looks like the sinon stub only works with axios Request method aliases. That is if you are using axios by passing config

axios(someConfig)

The above method doesn't seem to work.

But with method aliases, it seems to work.

axios.get(url, params, config)
axios.post(url, data, config)

When we do

sandbox.stub(axios, "get")

I think it is stubbing the method get in axios, but in the first case, the get/post/anyother method is not called and stubbing doesn't work. I maybe wrong in this. But it is working for me anyway.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!