mocha on test fail无法执行异步函数

时间:2017-03-08 21:51:25

标签: javascript node.js asynchronous promise mocha

我正在使用mocha的runner.on("fail", postProcessFunction)来在测试失败时执行一些后期处理。

请参阅:https://github.com/mochajs/mocha/blob/master/lib/runner.js#L58

我的问题是在postProcessFunction函数中我需要调用一个函数slowFunction来创建一个文件并返回一个promise。承诺需要10秒才能解决,到那时,mocha已经进入下一次测试或完全停止执行。 slowFunction函数未完成执行,文件永远不会被创建。

我已尝试使用promise.then(something)处理承诺并在Q.async中包裹postProcessFunction然后yield slowFunction slowFunction,但我永远不会似乎让它等待缓慢的功能完成后再继续。

注意:我不能使用全局的afterEach,因为测试已经具有套件级别,并且会在全局afterEach之前执行。我需要在失败时立即执行<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="ReverseProxyInboundRule1" stopProcessing="true"> <match url="(.*)" /> <action type="Rewrite" url="http://localhost:8080/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>

1 个答案:

答案 0 :(得分:0)

您可以在afterEach中检测到测试失败,这将允许您使用标准的mocha异步设置。

afterEach('Test failure debug', function(){
  debug('Test result', this.currentTest)
  if (this.currentTest.state === 'failed') {
    return page.source().then(src => debug('Page source', src))
  }
})

在这种特殊情况下,page变量的范围在describe块中,因此它在测试和之前/之后的帮助程序中都可用。