Chai http承诺永远不会失败

时间:2017-10-16 16:07:59

标签: mocha chai chai-http

我正在使用Chai http和承诺。以下测试应该失败,但它在没有调用then函数的情况下通过。如果我添加done参数以等待异步函数完成,则失败(正确)。我做错了吗?

it('Returns the correct amount of events', function() {
    chai.request(app)
        .get('/api/events/count')
        .then(function(res) {
            throw new Error('why no throw?');
            expect(res).to.have.status(200);
            expect(res).to.be.json;
        })
        .catch(function(err) {
            throw err;
        });
});

1 个答案:

答案 0 :(得分:1)

当您忘记退货时,请保证您的测试是常绿的。因此,您只需要返回promise即可使其工作:

it('Returns the correct amount of events', function() {
  return chai.request(app)
    .get('/api/events/count')
    .then(function(res) {
        throw new Error('why no throw?');
        expect(res).to.have.status(200);
        expect(res).to.be.json;
    })
    .catch(function(err) {
        return Promise.reject(err);
    });
});
相关问题