0

so i am trying to test a function as below

function generateJwt(){
    var deferred = Q.defer();
deferred.resolve({
            message: 'user created',
            token: signedJwt,
            userId: user.userId
          });
deferred.promise.then(function success(result) {
    console.log(result)
  }, function failed(result) {
    console.log(result)
  });
  return deferred.promise;

 }

so in the function above, it works properly, although i dont have all the pieces of code since the others are not that important. The part where i print the contents of the promise works as expected. So the generateJwt() returns a promise and i want to test the contents of that promise, to make sure it is same as what i am logging to the console. But it gives me a 2000ms timeout error as below

Auth Service Unit Tests:  #POST - generatePingJwt should return a promise with message user exists with user scope Admin:
 Error: timeout of 2000ms exceeded
  at null.<anonymous> (/Users/z001hm0/Documents/api_portal/developer-portal/node_modules/grunt-mocha-test/node_modules/mocha/lib/runnable.js:139:19)
  at Timer.listOnTimeout (timers.js:110:15)

i even increased the timeout to 15000ms, still same error. So i dont really know whats happening. I used mocha, chai, sinon, and supertest-as-promised to test this. below is my testcase suit

var chai = require('chai');
var requestPromise = require("supertest-as-promised");
var should = require('chai').should();

it("should return a promise with message user exists with user scope TM", function (done) {

  sinon.stub(dbModels.User, 'findOne').resolves({
    scope: 'TM',
    userId: 30001,
    save: save
  });
  save.resolves(true);

  requestPromise(app)
    .post(baseRoute + 'ping')
    .then(function (res) {
      console.log('promise result is ');
      console.log(res);
      done();
    });
});

Please any suggestions are welcomed. Thanks

user3137376
  • 1,527
  • 2
  • 19
  • 29

1 Answers1

0

Try to use this code to change the mocha timeout value:

this.timeout(40000);
Kyll
  • 7,036
  • 7
  • 41
  • 64
jackfu
  • 1