【问题标题】:Mocha passing test when i expect throw, but muy function is not throwing当我期望投掷时,摩卡通过了测试,但是 muy 函数没有投掷
【发布时间】:2021-09-15 08:36:26
【问题描述】:

我有一个函数,我希望它会抛出一个错误,但如果它没有抛出它,mocha 就通过了测试。

async function throwError() {
   throw new Error("foo");
}
async function notThrowError() {
   
}

测试:

describe("bar", function(){
  it("bar-2", async function() {
     await expect(throwError()).throw //passes the test
})
}

describe("bar", function(){
  it("bar-2", async function() {
     await expect(notThrowError()).throw //passes the test, but no error has been thrown 
})
}

【问题讨论】:

标签: javascript node.js mocha.js chai


【解决方案1】:

我应该使用chai as promised

const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
chai.should();

describe("bar", function(){
   it("bar-2", async function() {
      return notThrowError().should.eventually.rejected; //do not pass the test if not throw an error
   })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多