[英]Why does Mocha exit before a reporter is done handling a test failure?
I'm writing a test suite using PhantomJS (via selenium-webdriver
) and Mocha. 我正在使用PhantomJS(通过
selenium-webdriver
)和Mocha编写测试套件。 For reporting I need a screenshot every time a test fails and so I've written a custom reporter for Mocha based on spec
reporter: 为了进行报告,每次测试失败时我都需要一个屏幕截图,因此我根据
spec
报告者为Mocha编写了自定义报告者:
module.exports = Screenshot;
var Spec = require('mocha/lib/reporters/spec');
var fs = require('fs');
// custom library generating PhantomJS instances
var phantoms = require("phantoms");
function Screenshot(runner) {
Spec.call(this, runner);
runner.on('fail', function(test, err){
var driver = phantoms.getDriver();
driver.getCurrentUrl().then(function(url) {
console.log(url);
});
console.log('This works');
driver.takeScreenshot().then(function(img) {
console.log("Writing screenshot");
fs.writeFileSync("/tmp/selenium.png", img, 'base64');
return "/tmp/selenium.png";
});
console.log('This also works');
});
}
Screenshot.prototype.__proto__ = Spec.prototype;
Now I have a funny situation when a test fails: Mocha does everything synchronous from the hook callback (eg both logging statements), however neither the getCurrentUrl
promise nor the one for takeScreenshot
gets resolved, which is obviously not what I expected. 现在,当测试失败时,我遇到了一个有趣的情况:Mocha从钩子回调执行所有同步操作(例如,两个日志记录语句),但是
getCurrentUrl
诺言和takeScreenshot
诺言都没有得到解决,这显然不是我期望的。
However, I've found out that if I throw an exception after those promises are defined (eg after driver.takeScreenshot()
), Mocha terminates immediately without either a proper report or an error message (OK so far, even though I'd prefer getting a "Reporter raised an unexpected exception" message), but resolves both WebDriver promises, so that a screenshot is taken and also the current URL is printed just before the exit. 但是,我发现,如果我在定义了这些承诺后抛出异常(例如,在
driver.takeScreenshot()
),则Mocha会立即终止,而没有正确的报告或错误消息(到目前为止,即使我愿意宁愿收到“ Reporter引发了意外的异常”消息),但同时解决了WebDriver的承诺,因此将截取屏幕截图,并在退出前打印当前URL。
I'm playing with a small test suite of a couple of tests only, so I assumed that maybe Mocha considers the hook done and returns to OS before promises have a chance to be resolved. 我仅使用一个包含几个测试的小型测试套件,因此我假设Mocha可能认为挂钩已完成并在答应机会得到解决之前返回OS。 Is there an another explanation?
还有另一种解释吗? Am I at fault here or is it Mocha?
我在这里错吗?还是摩卡咖啡? How do I fix this properly?
如何正确解决此问题?
PS: It's perfectly possible that selenium-webdriver
and its promises framework is at fault here. PS:
selenium-webdriver
及其selenium-webdriver
框架很有可能在这里selenium-webdriver
。 However, I don't want to place blame, but find a solution and I'm happy about any explanation to this phenomenon. 但是,我不想怪罪,而是找到解决办法,我对此种现象的任何解释感到高兴。
Answering my own question: it seems Mocha is currently unable to work with async functionality in its hooks. 回答我自己的问题:看起来Mocha当前无法在其挂钩中使用异步功能。 I've opened an issue on Github about this.
我已经在Github上发布了一个与此有关的问题 。
Update: I've solved the problem and documented in the issue above. 更新:我已经解决了问题,并记录在上面的问题中。 The problem was a wiki entry on Mocha, describing programmatical test run, which employed a straight
process.exit()
instead of async process.on("exit", ...)
. 问题是Mocha上的一个Wiki条目,描述了程序测试运行,它采用了直接的
process.exit()
而不是async process.on("exit", ...)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.