使用supertest-as-promised和mocha测试promise时出现超时错误

时间:2015-08-28 16:16:50

标签: node.js express sinon chai supertest

所以我试图测试一个函数,如下所示

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)

所以在上面的函数中,它工作正常,虽然我没有所有的代码片段,因为其他代码并不重要。我打印承诺内容的部分按预期工作。所以generateJwt()返回一个promise,我想测试该promise的内容,以确保它与我登录到控制台的内容相同。但它给我一个2000ms的超时错误,如下所示

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

我甚至将超时时间增加到15000ms,仍然是同样的错误。所以我真的不知道发生了什么。我使用了mocha,chai,sinon和supertest-as-promise来测试这个。下面是我的测试套装

# Set up basic comparison features.
criteria timestamp:2sec
# Filter out log files.
filter "-*.log"
# Load first comparison.
load %1 %2
# Compare files with timestamp differences.
select newer.files older.files
# Generate a report of the differences.
folder-report layout:summary options:display-mismatches output-to:%3

欢迎任何建议。感谢

1 个答案:

答案 0 :(得分:0)

尝试使用此代码更改mocha超时值:

this.timeout(40000);