Reputation: 541
I'm using mocha programmatically and want to access the result in my program for later computation.
Currently, I have:
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);
});
However, I never get access to the result reported by the json reporter (Only on command line). Is there a way to access the result as json in my program? For example, by writing the result to a file an reading it later?
Upvotes: 1
Views: 1156
Reputation: 151380
The json
reporter stores its results on the test runner in the field testResults
. So you can modify your end
handler to:
.on('end', function() {
resolve(this.testResults);
});
I've tested it and it works. I've figured this by reading the code of Mocha. I've also checked the documentation but I've not seen something pointing to a public API through which to get these results. As far as I know, only the json
reporter does this.
Upvotes: 3