以编程方式访问 运行 mocha 时的结果
Access result when running mocha programmatically
我正在以编程方式使用 mocha,并希望在我的程序中访问结果以供以后计算。
目前,我有:
var mocha = new Mocha({
reporter: 'json'
});
files.forEach(file => {
var path = process.cwd() + '/' + file;
mocha.addFile(path);
});
mocha.run()
.on('start', function() {
// do something
})
// ...
.on('end', function() {
// I want to resolve the promise with the result generated by mocha:
resolve(result);
});
但是,我永远无法访问 json 报告者报告的结果(仅在命令行上)。
有没有办法在我的程序中以 json 的形式访问结果?例如,将结果写入文件并稍后读取?
json
报告器将其结果存储在字段 testResults
的测试运行器上。因此,您可以将 end
处理程序修改为:
.on('end', function() {
resolve(this.testResults);
});
我已经测试过它并且有效。我通过阅读摩卡的代码来理解这一点。我还检查了文档,但没有看到指向 public API 的内容,通过它可以获得这些结果。据我所知,只有json
记者这样做。
我正在以编程方式使用 mocha,并希望在我的程序中访问结果以供以后计算。
目前,我有:
var mocha = new Mocha({
reporter: 'json'
});
files.forEach(file => {
var path = process.cwd() + '/' + file;
mocha.addFile(path);
});
mocha.run()
.on('start', function() {
// do something
})
// ...
.on('end', function() {
// I want to resolve the promise with the result generated by mocha:
resolve(result);
});
但是,我永远无法访问 json 报告者报告的结果(仅在命令行上)。 有没有办法在我的程序中以 json 的形式访问结果?例如,将结果写入文件并稍后读取?
json
报告器将其结果存储在字段 testResults
的测试运行器上。因此,您可以将 end
处理程序修改为:
.on('end', function() {
resolve(this.testResults);
});
我已经测试过它并且有效。我通过阅读摩卡的代码来理解这一点。我还检查了文档,但没有看到指向 public API 的内容,通过它可以获得这些结果。据我所知,只有json
记者这样做。