JEST测试用例应该因try-catch而失败

问题描述 投票:0回答:1

我想知道为什么在使用try-catch块后,下面的测试用例仍通过了,虽然它应该失败:

test("test", () => {
  try {
    expect(true).toBe(false);
  } catch (err) {
    console.log(err);
  }
});

虽然没有try-catch却失败了:

test("test", () => {
      expect(true).toBe(false);
    });
reactjs jestjs try-catch enzyme testcase
1个回答
0
投票
只有抛出错误,测试才会失败。

断言expect(true).toBe(false);将引发错误,但是使用try-catch块将错误吞并,因此测试通过。

© www.soinside.com 2019 - 2024. All rights reserved.