Chai + mocha,解决则测试成功,拒绝则失败

Chai + mocha, succeed in test if resolved, fail if rejected

我有一个 returns 承诺的功能。在我使用 chai 的测试文件中,我希望发生以下情况:

const result = sendSurveyDataToAnalytics(userId,eventType,eventTitle)

result.then(() => {
    Logger.info("Succeed in the test if we get here")
}).catch(() => {
    Logger.info("Fail in the test if we get here")
});

代码对此进行了解释。成功了,失败了。用 maybe expect 或 should (already installed chai-as-promised)

做这件事的正确方法是什么

如果您使用 chai-as-promised:

const result = sendSurveyDataToAnalytics(userId, eventType, eventTitle);

result.then(() => {
    Logger.info("Succeed in the test if we get here");
}).catch(() => {
    Logger.info("Fail in the test if we get here");
});

it('resolves as promised', function() {
    return result.should.be.fulfilled;
});

// or:
it('rejects as promised', function() {
    return result.should.be.rejected;
});