从after内部检测测试失败Mocha中的每个钩子 [英] detecting test failures from within afterEach hooks in Mocha

查看:38
本文介绍了从after内部检测测试失败Mocha中的每个钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个afterEach挂钩,其逻辑仅在先前的测试失败时才触发.例如:

I'm trying to create an afterEach hook with logic that should only fire if the previous test failed. For example:

it("some_test1", function(){
  // something that could fail
})

it("some_test2", function(){
  // something that could fail
})

afterEach(function(){
  if (some_test_failed) {
    // do something to respond to the failing test
  } else {
    // do nothing and continue to next test
  }
})

但是,我不知道从afterEach挂钩中检测测试是否失败的方法.我可以将某些事件侦听器附加到摩卡吗?也许是这样的:

However, I have no known way of detecting if a test failed from within the afterEach hook. Is there some sort of event listener I can attach to mocha? Maybe something like this:

myTests.on("error", function(){ /* ... */ })

推荐答案

您可以使用this.currentTest.state(不确定何时引入):

You can use this.currentTest.state (not sure when this was introduced):

afterEach(function() {
  if (this.currentTest.state === 'failed') {
    // ...
  }
});

这篇关于从after内部检测测试失败Mocha中的每个钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆