javascript - 如果当前测试失败,如何检查mocha(tdd)的拆解方法?

标签 javascript node.js testing mocha.js teardown

我知道如何在 mocha 的 afterEach() 方法中检查测试是否失败:这里有解释:detecting test failures from within afterEach hooks in Mocha

但是那些使用 suitetest (tdd) 而不是 describeit 的人呢?

如何在此处检查当前测试是否失败?相同的代码将无法工作,因为 state 将是未定义的:

  teardown(async () => {
    // check if failed:
    if (this.currentTest.state === 'failed') {
      console.log("fail");
    }
  });

最佳答案

似乎它与 tdd 的工作方式有点不同(使用 suitetest)。

访问 this.ctx.currentTest 而不是 this.currentTest 对我有用。

示例:

if (this.ctx.currentTest.state === 'failed') {
  console.log(`'${this.ctx.currentTest.title}' failed`);
}

关于javascript - 如果当前测试失败,如何检查mocha(tdd)的拆解方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58661128/

相关文章:

node.js - 如何使用 mongoose 和 MongoDB 在我的网站中实现搜索引擎

javascript - 自定义初始化函数未在 TestCafe 中执行

Spring 、JUnit 和 EasyMock : EntityNotFoundException

android - 使用自定义 InstrumentationTestRunner 添加 Android JUnit 测试

javascript - iframe 中的打印按钮

javascript - Facebook JS SDK 中的 Oauth 错误

node.js - 如何扩展使用其他服务的 Node.js 服务?

javascript - 有没有办法检查JS中的字符串是否是单个表情符号?

javascript - wp_add_inline_script 没有向我的页面添加任何内容,不明白为什么

javascript - 试图映射到React