Ian Zhao
Ian Zhao

Reputation: 1173

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

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

Upvotes: 2

Views: 3422

Answers (3)

vkswhy
vkswhy

Reputation: 88

The answer suggested by @bring2dip is correct, but in case you don't have option other than using

axios(config)

then you can use

axios.request(config)

and in test.ts you can use

sandbox.stub(axios, "request")

and the result will be the same.

Upvotes: 0

bring2dip
bring2dip

Reputation: 885

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.

Upvotes: 1

BrunoLM
BrunoLM

Reputation: 100322

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

Upvotes: 3

Related Questions