总是先于一切”挂钩进行测试,无法修复

时间:2019-05-22 05:09:32

标签: javascript node.js express sinon stub

before(function (func: (...args: any[]) => any) {
  app = express();

  // prepare environment
  sandbox = sinon.createSandbox();

  // stub
  sandbox.stub(app, "post").callsFake(() => {
    return Promise.resolve("send a post");
  });

  ingestEventStub = sandbox.stub(IngestController.prototype, "ingestEvent").callsFake(function () {
    return Promise.resolve({
      success: true,
      message: "anymessage"
    });
  });

  eventRoutesV1 = EventRoutesV1(app, express);
  ingestController = new IngestController();
});

beforeEach(function () {
  sandbox.resetHistory();
});

afterEach(function () {
  sandbox.restore();
});


it('should send 200 back and pubsub message when call /event ', function (done) {

  const httpBody = {
    "id": "id",
    "version": "version",
    "creationTime": "creationTime",
    "environment": "environment",
    "name": "name",
    "tenantId": "tenantId",
    "payloadType": "payloadType",
    "payloadVersion": "payloadVersion",
    "payload": {}
  };

  request(app)
    .post('/event/ingestEvent')
    .send(httpBody)
    .end((err, res) => {
      expect(res.body).to.equal({ success: true });
      expect(res.status).to.equal(HTTP_STATUS_CODES.OK);
      done();
    }).then(done, done)
});

错误:

1) "before all" hook for "should send 200 back and pubsub message when call /event "

Error: Timeout of 50000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. 
      at listOnTimeout (internal/timers.js:531:17)
      at processTimers (internal/timers.js:475:7)

我不知道为什么总是出现此错误,并且找不到无法解决的前提

1 个答案:

答案 0 :(得分:0)

传递给before的函数包含一个名为func的参数。

如果传递给before的函数包含参数,则假定该参数为done参数。

由于从未调用过func(被视为done参数),因此测试在before挂钩中超时。

看起来您在done回调中不需要before参数,因此只需删除func,这样就可以避免测试超时:

before(function () {  // <= remove "func"
    app = express();
    ...
});