mochajs / mocha

☕️ simple, flexible, fun javascript test framework for node.js & the browser
https://mochajs.org
MIT License
22.59k stars 3.01k forks source link

🐛 Bug: Inconsistent Reporter Use of Console and Stdout #2943

Open shellscape opened 7 years ago

shellscape commented 7 years ago

The title of this issue is the crux. It appears that the default spec reporter uses console.log rather than the built-in process.stdout that other reporters use. And there doesn't seem to be a pattern to which reporters use which output method.

Some reporters like spec (https://github.com/mochajs/mocha/blob/master/lib/reporters/spec.js#L40) are using console.log, some like dot(https://github.com/mochajs/mocha/blob/503627c756cd4feef76772aafadccd0da255756f/lib/reporters/dot.js#L31) use stdout.

My use case involves utilizing headless chrome to run mocha tests. I'm providing a proxy to window.mocha.process.stdout to use a message bus that I've injected, that pipes raw messages back to the node app which catches them via the Chrome DevTools Protocol. It might sound complex, but it's rather simple. Without knowing which actor (user code or mocha) is calling console.log, I can't effectively show console messages in the terminal with any distinction between actors.

The "simple" fix would be to standardize all reports on one output method - preferably mocha.process.stdout.

ScottFreeCode commented 7 years ago

We would need to verify that none of the uses of console.log in reporters aren't relying on any formatting behavior of console.log that isn't present in the direct process.stdout methods, or else recreate the effect. Off the top of my head, I want to say that console.log has four formatting behaviors:

It's possible some of those are shared by stdout methods, but we'll want to double-check.

boneskull commented 7 years ago

this sounds workable to me if someone wants to send a PR. ideally, a reporter should have utility method(s) available on its instance to deal with this, e.g. this.print() for dot and this.println() for spec, as to not have to reach into mocha.process.

boneskull commented 7 years ago

this is also ideally semver-patch

plroebuck commented 6 years ago

JSONStream reporter is similarly afflicted (#3521).