Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suppress stdout from test cases #290

Closed
defunctzombie opened this issue Feb 25, 2012 · 8 comments
Closed

suppress stdout from test cases #290

defunctzombie opened this issue Feb 25, 2012 · 8 comments

Comments

@defunctzombie
Copy link
Contributor

Something nice to have would be a way to silence the stdout from the tests themselves (not the reporters). That way if I have trace output in my program I can easily suppress it to see some nice test output :)

@tj
Copy link
Contributor

tj commented Feb 25, 2012

not really sure what you mean since there's only one stdout, seems more like something that should be toggleable in your lib

@defunctzombie
Copy link
Contributor Author

Does mocha not capture the stdout of the tests? I figured it did and just passed it along to the console.

@tj
Copy link
Contributor

tj commented Feb 25, 2012

they run in the same process so it's the same stdout

@ktmud
Copy link

ktmud commented Apr 6, 2012

I choose to rewrite console.log when running test:

var util = require('util');
var log = fs.createWriteStream('var/stdout.log');

console.log = console.info = function(t) {
  var out;
  if (t && ~t.indexOf('%')) {
    out = util.format.apply(util, arguments);
    process.stdout.write(out + '\n');
    return;
  } else {
    out = Array.prototype.join.call(arguments, ' ');
  }
  out && log.write(out + '\n');
};

As long as mocha uses process.stdout.write to output test report, this hack should work. But sometimes mocha uses console, too.

I wonder if it's possible to replace all the console.log and console.error in mocha to process.stdout.write / process.stderr.write? After all, sometimes we just can't prevent third party modules to write things with console, whereas process.stdout.write is much more under the radar.

It's reasonable to log something when an app is running. And it is best practice to keep its output pipe into stdout. When debugging, just launch the app, the output will be on the screen. In production, a simple I/O redirection will do. No need for extra code!

While it's necessary to separate test reports from app logs, it is also convenient to keep the report in stdout when test is running.

Maybe, however, multiple reporters is not really that necessary, an option to redirect all the console things and util.debug, util.log to another file would be enough.

@defunctzombie
Copy link
Contributor Author

Anything that wrote with process.stdout.write would still bypass the fake "silencing". In either case, I don't use console.log (and friends) for logging and instead use my own log library. I do what @visionmedia suggested and just have a way to silence log output in the library.

As far as third party libraries go, they should never output to console unless I either asked them to or that is their job (test reporters and such obviously). It is not for them to decide how I do my logging.

@tj
Copy link
Contributor

tj commented Apr 6, 2012

yeah unconditional logging to stdio is kinda rude, unless maybe the lib checks NODE_ENV but even then why not just provide a flag

@MaerF0x0
Copy link

more or less, this is why I like devs using debug(https://github.com/visionmedia/debug) . Then i can tune verbosity to my needs. Or i can just have the --reporter dot .

@biern
Copy link

biern commented May 30, 2020

I've implemented a mocha reporter that captures (and hides) stdout and stderr during tests. It then displays the captured output only of the failed tests alongside their error messages. For me it significantly increases readability of a test report.

Internally it just mocks process.stdout so it should work with most third party libraries out there.

Maybe you'll find it useful: https://github.com/biern/mocha-dd-reporter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants