Search code examples
javascriptunit-testingmocha.jssinon

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


Solution

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