如果当前测试失败,如何检查摩卡(tdd)的拆解方法? [英] How to check in the teardown method of mocha (tdd) if the current test failed?
本文介绍了如果当前测试失败,如何检查摩卡(tdd)的拆解方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何在Mocha的afterEach()
方法中检查测试是否失败:此处说明:
I know how to check if a test failed in the afterEach()
method of mocha: That's explained here: detecting test failures from within afterEach hooks in Mocha
但是使用suite
和test
(tdd)而不是describe
和it
的人呢?
But what about the people using suite
and test
(tdd) instead of describe
and it
??
如何在此处检查当前测试是否失败?相同的代码将不起作用,因为state
将是未定义的:
How can I check if the current test failed here? The same code won't work because state
would be undefined:
teardown(async () => {
// check if failed:
if (this.currentTest.state === 'failed') {
console.log("fail");
}
});
推荐答案
似乎与tdd(使用suite
和test
)的工作方式略有不同.
It seems that it works a little bit different with tdd (using suite
and test
).
使用this.ctx.currentTest
而不是this.currentTest
为我工作.
示例:
if (this.ctx.currentTest.state === 'failed') {
console.log(`'${this.ctx.currentTest.title}' failed`);
}
这篇关于如果当前测试失败,如何检查摩卡(tdd)的拆解方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文