我正在尝试创建一个 afterEach 钩子(Hook),其逻辑只有在之前的测试失败时才会触发。例如:
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 Hook 中失败。是否有某种事件监听器可以附加到 mocha 上?也许是这样的:
myTests.on("error", function(){ /* ... */ })
最佳答案
您可以使用 this.currentTest.state
(不确定何时引入):
afterEach(function() {
if (this.currentTest.state === 'failed') {
// ...
}
});
关于javascript - 从 Mocha 中的 afterEach Hook 中检测测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24194672/