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

女生的网名这么多〃 提交于 2019-12-22 11:27:38

问题


I'm writing some test for my client side api use karma with Mocha and Sino. But I'm stuck on getting the async process.

import api from '../../../src/api';
import stubData from '../data';
import axios from 'axios';

/* eslint-disable prefer-arrow-callback,func-names */
describe('API test', function () {
  before(function () {
    this.server = sinon.fakeServer.create();
  });
  after(function () {
     this.server.restore();
  });

it('Should return cart with provided token', function (done) {
  this.server.respondWith("GET", "/cart",
        [200, { "Content-Type": "application/json" },
         '[{ "id": 12, "comment": "Hey there" }]']);

 axios.get('/cart')
  .then(function (response) {
    console.log(response);
    done();
  })
  .catch(function (error) {
    console.log(error);
    done();
  });
  this.server.respond();
});

 });

for some reason, I always getting Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. from Mocha. Seems like the then after axios.get() is not executed thus done is not called either.

I did follow what was recommended in Mocha document

describe('User', function() {
  describe('#save()', function() {
    it('should save without error', function(done) {
      var user = new User('Luna');
      user.save(function(err) {
        if (err) done(err);
        else done();
      });
    });
  });
});

Am I doing anything wrong here? Thank you


回答1:


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;
  });



回答2:


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.



来源:https://stackoverflow.com/questions/41516044/sinon-mocha-test-with-async-ajax-calls-didnt-return-promises

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