简体   繁体   中英

Access result when running mocha programmatically

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM