首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >抛出mocha异常可以使测试通过,而不应该这样做。

抛出mocha异常可以使测试通过,而不应该这样做。
EN

Stack Overflow用户
提问于 2019-06-26 05:09:32
回答 1查看 400关注 0票数 0

在我们的mocha测试中,有时我们有bug和抛出异常,当这些异常被抛出时,后续的断言不会被调用,我们的测试可能会通过,即使有一些错误。

代码语言:javascript
复制
  // this would pass
  it('adsf', async () => {
    assert.equal(1,1)
    var foo = undefined;
    foo.hi(); // throw exception
    assert.equal(1,2) // assert not called
  });

我们尝试将其包装在try catch中,如下所示

代码语言:javascript
复制
  // this would fail, but not say why
  it('adsf', async () => {
    try {
      assert.equal(1,1)
      // var foo = undefined;
      // foo.hi();
      assert.equal(1,2)
    } catch (err) {
      assert.fail(err) // if fail, such as the 1,2 case above, the line number of the error is not shown
    }
  });

但是catch案例隐藏了一些失败的断言信息。如果任何人有任何建议,将不胜感激。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56762035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档