从摩卡中的afterEach中检测testing失败

我想创build一个afterEach挂钩与逻辑,应该只会触发如果以前的testing失败。 例如:

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钩子内的testing是否失败。 是否有某种我可以附加到摩卡的事件监听器? 也许这样的事情:

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

你可以使用this.currentTest.state (不知道这是什么时候引入的):

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

你可以像下面这样做

 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')); }) })