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 console errors on testing #329

Closed
cyberwombat opened this issue Nov 30, 2017 · 7 comments
Closed

Suppress console errors on testing #329

cyberwombat opened this issue Nov 30, 2017 · 7 comments

Comments

@cyberwombat
Copy link

Could it be possible to not dump errors to console while in test env? It makes a total mess of tests. I have to compose all my handlers with a try/catch to swallow the dump. My error is tested through AVA throws but my screen gets filled with production error dumps.

@cyberwombat
Copy link
Author

Something like:

const sendError = (req, res, errorObj) => {
  const statusCode = errorObj.statusCode || errorObj.status
  const message = statusCode ? errorObj.message : 'Internal Server Error'
  send(res, statusCode || 500, DEV ? errorObj.stack : message)
  if (process.env.NODE_ENV === 'test') return
  if (errorObj instanceof Error) {
    console.error(errorObj.stack)
  } else {
    console.warn('thrown error must be an instance Error')
  }
}

@lfades
Copy link
Member

lfades commented Mar 9, 2018

@leo Can this be a implemented ? Ignoring the logs is great when you're running tests that should throw, if any other test throws the test runner will tell you that so no need to log for errors, the only downside I can see is if the error thrown by the test runner is being cached (by a try/catch - catch()) you will notice anything, but that's just a bad practice that will break the entire test

there was an implementation for it before: #124

@vespertilian
Copy link

Another suggested implementation.

If you provide console as an input defaulted to the native console command, we can override it when testing. I plan just to use my own version of sendError for the time being, however it would be nice if this came out of the box with Micro. Also useful if you want to pass in a custom logger.

Current implemenation

const sendError = (req, res, errorObj) => {
	const statusCode = errorObj.statusCode || errorObj.status;
	const message = statusCode ? errorObj.message : 'Internal Server Error';
	send(res, statusCode || 500, DEV ? errorObj.stack : message);
	if (errorObj instanceof Error) {
		console.error(errorObj.stack);
	} else {
		console.warn('thrown error must be an instance Error');
	}
};

Suggested implementation

const sendError = (req, res, errorObj, _console=console) => {
	const statusCode = errorObj.statusCode || errorObj.status;
	const message = statusCode ? errorObj.message : 'Internal Server Error';
	send(res, statusCode || 500, DEV ? errorObj.stack : message);
	if (errorObj instanceof Error) {
		_console.error(errorObj.stack);
	} else {
		_console.warn('thrown error must be an instance Error');
	}
};

No breaking changes. Happy to pull request this if you will accept it.

@mcchrish
Copy link

mcchrish commented Dec 7, 2018

Any updates on this? I thought #124 fixed it but still gets errors being logged when the default error handler is used.

@timneutkens
Copy link
Member

Check this comment: #349 (comment)

@timneutkens
Copy link
Member

Going to close this in favor of #369

@lucasconstantino
Copy link

@cyberwombat this is a common issue across testing in general. I've build a tool for that some time ago: https://github.com/lucasconstantino/console-suppress

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

6 participants