3

When calling truffle test some_test.js, I would like to pass Mocha switches.

For example: truffle --no-colors test some_test.js works fine.

However: truffle --reporter json-streams test some_test.js doesn't.

To my understanding, it should get Mocha to generate all the output in JSON format.

Any ideas?

| improve this question | | | | |
3

I can do it by adding this to the Truffle configuration file (truffle.js or truffle-config.js):

mocha: {
    useColors: false,
    reporter: "json"
}

Available reporters are described here.

| improve this answer | | | | |
  • But this is restricted to general configuration. What if a I want to use some mocha settings for my dev environment but others for CI? – jmendiola Aug 5 '19 at 17:22
0

Truffle uses Mocha programatically. So, the Mocha class constructor shows the full options. Truffle v5.1.9 seems to use Mocha v5.2.0 and the constructor code for this version can be found here.

https://github.com/mochajs/mocha/blob/v5.2.0/lib/mocha.js#L83

Which shows

 * @class Mocha
 * @param {Object} options
 */
function Mocha(options) {
  options = options || {};
  this.files = [];
  this.options = options;
  if (options.grep) {
    this.grep(new RegExp(options.grep));
  }
  if (options.fgrep) {
    this.fgrep(options.fgrep);
  }
  this.suite = new exports.Suite('', new exports.Context());
  this.ui(options.ui);
  this.bail(options.bail);
  this.reporter(options.reporter, options.reporterOptions);
  if (typeof options.timeout !== 'undefined' && options.timeout !== null) {
    this.timeout(options.timeout);
  }
  if (typeof options.retries !== 'undefined' && options.retries !== null) {
    this.retries(options.retries);
  }
  this.useColors(options.useColors);
  if (options.enableTimeouts !== null) {
    this.enableTimeouts(options.enableTimeouts);
  }
  if (options.slow) {
    this.slow(options.slow);
  }
}

You can find options like ui, bail, reporter, reporterOptions, timeout, retries, useColors, enableTimeouts and so on.

More detailed meaning of these options can be inferred from the API documentation of Mocha class located at https://mochajs.org/api/mocha.

For example, API documentations for methods such as bail(), retries(), enableTimeouts(), or timeout() show more details on corresponding options.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.