How to check in the teardown method of mocha (tdd) if the current test failed?

淺唱寂寞╮ 提交于 2020-01-25 08:22:08

问题


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

But what about the people using suite and test (tdd) instead of describe and it??

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

回答1:


It seems that it works a little bit different with tdd (using suite and test).

Accessing this.ctx.currentTest instead of this.currentTest worked for me.

Example:

if (this.ctx.currentTest.state === 'failed') {
  console.log(`'${this.ctx.currentTest.title}' failed`);
}


来源:https://stackoverflow.com/questions/58661128/how-to-check-in-the-teardown-method-of-mocha-tdd-if-the-current-test-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!