我正在尝试创建一个带有逻辑的afterEach挂钩,该挂钩只应在前一次测试失败时触发。例如:
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挂钩中检测测试是否失败。是否有某种事件监听器我可以附加到摩卡?也许是这样的:
myTests.on("error", function(){ /* ... */ })
答案 0 :(得分:43)
您可以使用this.currentTest.state
(不确定何时引入):
afterEach(function() {
if (this.currentTest.state === 'failed') {
// ...
}
});
答案 1 :(得分:-4)
您可以执行以下操作
describe('something', function(){
var ok = true;
it('should one', function(){
ok = true;
})
it('should two', function(){
// say the test fails here
ok = false;
})
afterEach(function(){
if (!ok) this.test.error(new Error('something went wrong'));
})
})