如果当前测试失败,如何检查摩卡(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`);
}