【问题标题】:Why does Jest pass test if function is declared asynchronous?如果函数声明为异步,为什么 Jest 通过测试?
【发布时间】:2020-03-31 18:56:22
【问题描述】:

通过了

test('test_test', async () => {
  try {
    const received = true;
    const expected = false;
    expect(received).toBe(expected);
  } catch (err) {
    return err
  }
});

这应该失败

test('test_test', () => {
  try {
    const received = true;
    const expected = false;
    expect(received).toBe(expected);
  } catch (err) {
    return err
  }
});

我有一个使用 async 和 await 的函数,这就是测试不会失败的原因,所以我试图了解如何让 top 失败。

【问题讨论】:

  • 为什么会有trycatch?如果你删除它们,测试就会失败,async 与否。
  • 我将它与 CircleCi 一起使用,它说在使用异步等待的地方使用 try catchs
  • 你能链接那个建议吗?我更倾向于遵循 Jest 自己的指导:jestjs.io/docs/en/tutorial-async;如果您尝试测试您正在测试的代码正确抛出错误,您只需要捕获错误。
  • 只是按原样运行了一些测试代码,CircleCi 没有抱怨。你是对的,它的尝试。为了清楚起见,这是圈子的消息(node:727) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

标签: javascript asynchronous async-await jestjs


【解决方案1】:

正如@jonrsharpe 建议删除try/catch 它现在按预期运行并且失败了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-20
    • 2019-08-23
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 2018-06-03
    • 2018-11-21
    相关资源
    最近更新 更多