摩卡,nodejs承诺testing无法完成,因为缺乏完成

我试图运行一个承诺的testing,但testing失败,认为它超过超时限制,并build议确保我已经完成的条款。

这是我的testing代码的一部分:

$configurations .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model .then(function () { done(new Error("Expected INVALID_MODEL error but got OK")); }, function (error) { chai.assert.isNotNull(error); chai.expect(error.message).to.be.eq("INVALID_MODEL_ERROR"); chai.expect(error.kind).to.be.eq("ERROR_KIND"); chai.expect(error.path).to.be.eq("ERROR_PATH"); done(); }) .catch(done); }); 

我已经完成了所有的子句,所以我不知道在testing中是否遗漏了某些东西,或者结构是错误的。

摩卡支持testing承诺,只要你答复,就不用done

 const expect = chai.expect it('should error', function(){ return $configurations .updateConfiguration(configurations_driver.NOT_VALID_MODEL) //invalid model .then(()=> { throw new Error("Expected INVALID_MODEL error but got OK")}) .catch(error => { expect(error).to.not.be.null; expect(error.message).to.equal("INVALID_MODEL_ERROR"); expect(error.kind).to.equal("ERROR_KIND"); expect(error.path).to.equal("ERROR_PATH"); }) }) 

还要看看chai-as-promised promise承诺testing更像标准的chai断言/期望。

 chai.should() chai.use(require('chai-as-promised')) it('should error', function(){ return $configurations .updateConfiguration(configurations_driver.NOT_VALID_MODEL) .should.be.rejectedWith(/INVALID_MODEL_ERROR/) }) 

在节点7.6+环境或者你有babel / babel-register的地方,你也可以使用async / await promise处理程序

 it('should error', async function(){ try { await $configurations.updateConfiguration(configurations_driver.NOT_VALID_MODEL) throw new Error("Expected INVALID_MODEL error but got OK")}) } catch (error) { expect(error).to.not.be.null; expect(error.message).to.equal("INVALID_MODEL_ERROR"); expect(error.kind).to.equal("ERROR_KIND"); expect(error.path).to.equal("ERROR_PATH"); } })