tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

json-mocha-reporter.js (1761B)


      1 const mocha = require('mocha');
      2 module.exports = JSONExtra;
      3 
      4 const constants = mocha.Runner.constants;
      5 
      6 /*
      7 
      8 This is a copy of
      9 https://github.com/mochajs/mocha/blob/master/lib/reporters/json-stream.js
     10 with more event hooks. mocha does not support extending reporters or using
     11 multiple reporters so a custom reporter is needed and it must be local
     12 to the project.
     13 
     14 */
     15 
     16 function JSONExtra(runner, options) {
     17  mocha.reporters.Base.call(this, runner, options);
     18  mocha.reporters.JSON.call(this, runner, options);
     19  const self = this;
     20 
     21  runner.once(constants.EVENT_RUN_BEGIN, function () {
     22    writeEvent(['start', {total: runner.total}]);
     23  });
     24 
     25  runner.on(constants.EVENT_TEST_PASS, function (test) {
     26    writeEvent(['pass', clean(test)]);
     27  });
     28 
     29  runner.on(constants.EVENT_TEST_FAIL, function (test, err) {
     30    test = clean(test);
     31    test.err = err.message;
     32    test.stack = err.stack || null;
     33    writeEvent(['fail', test]);
     34  });
     35 
     36  runner.once(constants.EVENT_RUN_END, function () {
     37    writeEvent(['end', self.stats]);
     38  });
     39 
     40  runner.on(constants.EVENT_TEST_BEGIN, function (test) {
     41    writeEvent(['test-start', clean(test)]);
     42  });
     43 
     44  runner.on(constants.EVENT_TEST_PENDING, function (test) {
     45    writeEvent(['pending', clean(test)]);
     46  });
     47 }
     48 
     49 function writeEvent(event) {
     50  process.stdout.write(JSON.stringify(event) + '\n');
     51 }
     52 
     53 /**
     54 * Returns an object literal representation of `test`
     55 * free of cyclic properties, etc.
     56 *
     57 * @private
     58 * @param {Object} test - Instance used as data source.
     59 * @return {Object} object containing pared-down test instance data
     60 */
     61 function clean(test) {
     62  return {
     63    title: test.title,
     64    fullTitle: test.fullTitle(),
     65    file: test.file,
     66    duration: test.duration,
     67    currentRetry: test.currentRetry(),
     68  };
     69 }