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 console.log
output from tests that succeed
#1998
Comments
@justinmchase Only via plugins, if any exist. No place for this in mocha's core, imo. Proxying |
Thanks for the reply! That sounds totally reasonable. Do you have an example of hooking into the mocha fail event that I could reference? I am having trouble figuring out where to begin. |
This is an old issue, but I found this StackOverflow answer useful. Inspecting // On the root suite - so outside any describe() block
afterEach(function() {
if (this.currentTest.state === 'failed') {
// a test just failed
}
if (this.currentTest.state !== 'passed') {
// a test, before(), or beforeEach() hook just failed
}
}); |
There's been a pretty long thread about this in the past. A possible workaround was proposed here: #1582 (comment) |
there should be a flag --errors-only, which would mean the only Mocha output would be for failing tests, but not sure if it exists. |
The solution I've come up with since is to use the var debug = require('debug')
var log = debug('myapp')
log('useful debugging output') Then when run: $ mocha # no console output from app or $ DEBUG=myapp mocha # prints out console output from app Combine this with the |
@justinmchase Not sure to quite understand. |
For a lot of apps, console logging is only for developer debugging, like for a webserver maybe. In those cases just using the debug library works great. If your app is itself an actual console application, which intentionally emits console output as a feature then you'll probably need another layer of abstraction. One other thing i have done since this issue was originally opened, which I thought worked well is to separate the logic of the application from the console front end part of the application. Sort of treat it like your code may have multiple front ends and then treat your tests as a 2nd front-end. Or in other words, do not actually do any console logging inside of your application but instead emit events which the front end can handle. In the case of the console front-end, it will result in writes to standard out. In the case of unit tests it will result in passing/failing assertions. For example: // console.js
import { App } from './app'
const app = new App()
app.on('started', () => console.log('application started...')
app.on('done', () => console.log('application done.')
app.run() // app.spec.js
import { App } from './app'
import sinon from 'sinon'
import { expect } from 'chai'
it('runs successfully', (done) => {
const started = sinon.stub()
const app = new App()
app.on('started', started)
app.on('done', () => {
expect(started.called).to.be.true
done()
})
app.start()
}) The point is that the code in |
my only advice would be to use a logging module like bunyan instead of console.log and filter out the output that is above level error
|
@justinmchase I really like this idea of a console front end... It looks kind of obvious when you think of it. Your solution is much cleaner... Well, my logging activity is tested now. |
I can't believe this is not a "thing", I am new to using TDD and one of the first things that annoys me is on |
I agree. During development I will often Like @sladec I'm confused as to how this is not a common problem with a built-in solution, but that is the case with many issues I've hit upon adopting Mocha and TDD. In any case another workaround if you use Webpack might be to suppress |
Again, @ViggoV I think what you should do is use the debug library instead of For example: import debug from 'debug'
const log = debug('server');
export function server () {
// ...
log('server started...')
} Now when you run this locally or in a test nothing will be written to stdout by default. To enable this debug output to be written to stdout you would enable it with an environment variable. For example: $ npm test # <> is emitted
$ DEBUG=server npm test # <server started...> is emitted |
Unfortunately For example, oas3-tools 2.0.2 contains page-long dumps from here: As I can't pause our processes for months until a fix for that would make it to the official packages, (and this is just one such package among several,) all I can do now is to suppress these messages on the test-suite level. Now, I'll need one of the hacks/workarounds, which will make the testcases more complex: As I don't intend to copy it into each and every testcase file, I'll need to compartmentalize it into some module and refer that from the testcase files, and so on. I think I'm not alone with this issue, and now each of us is reinventing the same wheels, reimplementing the same workarounds, so if there is popularity survey for having that as a built-in framework feaure, you definitely have my vote for it. Meanwhile, here is my loghack module, and a small loghack test for it. (It should handle nested invocations as well, though I haven't tested that yet.) |
I wrote my own module to solve the same exact issue with minimal effort so I'll leave a link if anyone is interested. https://www.npmjs.com/package/mocha-suppress-logs Here's some example code that will hide logs of tests that succeed and show only the ones of tests that failed: const suppressLogs = require('mocha-suppress-logs');
describe('Something', () => {
suppressLogs();
it('should do something', () => {
// test code
});
}); You can also do so globally over the entire test suite. |
I do not want to see debug output from tests that succeed but would like to see it for tests that fail. Is there a way to achieve this currently?
I have seen some recommendations to only conditionally log based on environment variables in responses to similar questions but I would like to see the debug information if the test fails which is slightly different. It seems like that would be a mocha level or reporter level feature, not something the code under test could control.
The text was updated successfully, but these errors were encountered: