【问题标题】:Jest does not fail test when function called with invalid parameters使用无效参数调用函数时,Jest 不会失败测试
【发布时间】:2021-01-29 22:40:40
【问题描述】:

我正在使用 Jest 为带有 typescript 的 react 应用程序编写一些单元测试。当我使用无效的函数参数运行测试用例时,测试仍然通过。假设我有以下功能:

function someFunction(a: number, b: number) {
 return a + b;
}

假定错误的测试函数是:

it("should fail", () => someFunction());

我预计测试会由于函数的无效参数而失败。我错过了什么?当然,当我尝试编译应用程序时,它会抛出一个错误,但是当我运行 npm test / yarn test 时,我也想监控我的测试。

【问题讨论】:

    标签: reactjs jestjs


    【解决方案1】:

    它没有失败,因为你没有任何断言。你的测试应该是这样的:

    it("should fail", () => {expect(someFunction(1,2)).toEqual(5)}); //here your test should fail
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-21
      • 2017-10-04
      • 2019-09-21
      • 1970-01-01
      • 2021-09-15
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      相关资源
      最近更新 更多