Mocha: silent option (reporter)

Created on 11 Dec 2011  路  23Comments  路  Source: mochajs/mocha

It would be nice to have a --quiet option or --reporter silent or something to indicate no output is wanted unless there are errors. I guess it must be a reporter modifier and not a reporter itself -- we still want the errors in a certain reporter format... Anyway, it's not unusual for commands to have some such option. Often, passing tests can just --shut-up :) --shush?

Most helpful comment

I think any option that can be given from cli is better like --quiet flag

All 23 comments

/dev/null :D

Ha-ha :) Actually, a reporter modifier may not be a bad idea - parsing on hyphen e.g. --reporter quiet-tap. Some of these are funny, others silly (or impossible?) e.g. quietly landing errors progress...

are you ok with /dev/null? I try not to reproduce stuff the shell can easily do for us

Personally, I don't care where it goes, as long as I get told if stuff is not ok... Can dev/null be conditional - example? Maybe send STDOUT to dev/null and still get STDERR of failed tests on-screen?

yeah just do mocha whatever > /dev/null, or if you _really_ want it silent mocha whatever 2>&1 > /dev/null

tried both of them - no difference in behavior. both make it equally shut up (no shell output, just growl). did you mean that this is how it would eventually work or that it already does? errors don't show with this - could mocha send just the passing tests to /dev/null?

> /dev/null will give you just errors:

ok, I see it works when going through make, but not with straight mocha, right?
not everybody would have make test, or even if - use it for development (ad-hoc testing). personally, I also have a cake test with extra options for stuff like combining mocha, vows and other tests (or running something more specific).

works with mocha too, it's just typical stdio redirection, so it works with any program outputting to stdio

here's another example with mocha(1)

ok got it with a final "but" :) this seems to vary with reporters. works as shown with most of them. because of the solarized theme issue, I'm using --reporter tap which I guess is one of the very few that don't support the /dev/null feature.

looks like I have TAP's errors outputting to stdout that's why. I dont use TAP personally so im not sure if outputting to stderr will mess up most consumers but I'll try and take a look

i'd like to disable the reporter and hide all stdout-output from the js api and use the events from the returned Runner instead (or add my own reporter re #323)

(setting {reporter:false} is not sufficient)

How to silent the mocha in lib level, in case of invoke mocha.run() in code.

@tj I have the same question as @cybertk, I am running mocha programmatically, and want to capture the stdio without printing to screen. Is there a way to do this?

@cybertk @IanVS We don't currently inject the function or stream to be used for output by reporters. As a result, all reporters currently use process.stdout.write or console.log. You can get around this by overwriting process.stdout.write prior to invoking mocha.run. Since console.log calls it, that'll handle both cases, but only if no other code is trying to write to stdout at the same time. Example:

var output, originalWrite;
output = [];
originalWrite = process.stdout.write;

process.stdout.write = function(str) {
  output.push(str);
};

mocha.run(function(failures) {
  // Restore original stdout.write
  process.stdout.write = originalWrite;

  // ...
});

@danielstjules awesome, thanks, I think that's just what I needed. I want to run a mocha test inside a mocha test, and so far this is the best way I've come up with to do that. (I'm trying to test a module I wrote for running ESLint as a mocha test.)

looks like this works :

 var mocha = new Mocha({
        reporter: function () {
            //avoid logs
        },
    });

@Cactusbone had the same issue and that worked for me. thanks

I think any option that can be given from cli is better like --quiet flag

When you are using mocha with nyc - you can't control only mocha output. We need --quiet flag.

How would you make this support ES6? I have a cli that checks the current directory for mocha test files and run them. If the test file is in a newer JS version, how do I add support programmatically for those files?

redirecting to /dev/null is not enough. This leads to error output like

Error Response text: 
error Command failed with exit code 1.  

When I would expect the entire error stack trace to be shown, as in

  1 test failing

  1) Client Router
       afterEach Client.clear() works:
     AssertionError: expected [] not to be empty
      at Context.it (dist/routers/client-router.test.js:44:48)
      at processTicksAndRejections (internal/process/task_queues.js:86:5)

(made with Chai)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boneskull picture boneskull  路  76Comments

teckays picture teckays  路  84Comments

sarenji picture sarenji  路  66Comments

domenic picture domenic  路  43Comments

enobufs picture enobufs  路  38Comments