Hide console.error when mocking error response in jest?

I'm writing a few client-side unit tests and want to assert what happens when an error is returned from the procedure. My tests pass ok, but I end up with a wall of console.error's when I do so. I don't want to mock the return value of console.error so that it never fires, as there may be some valid errors that are thrown that I want to catch, especially when running my tests on my CI/CD. Is there any other way to prevent the console.error from logging when a procedure throws an error? I've tried updating my loggerLink in my config during testing so that it is disabled, like so;
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === 'test' ? false : process.env.NODE_ENV === 'development' || (opts.direction === 'down' && opts.
result instanceof Error),
}),
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === 'test' ? false : process.env.NODE_ENV === 'development' || (opts.direction === 'down' && opts.
result instanceof Error),
}),
While this does clear the likes of the following console.error's
console.error
<< mutation #2 user.delete {
console.error
<< mutation #2 user.delete {
I will still get the likes of
console.error
TRPCClientError: An error occured when attempting to delete your user account. Please try again.
console.error
TRPCClientError: An error occured when attempting to delete your user account. Please try again.
Any help would be greatly appreciated!
1 Reply
paul
paul2y ago
I haven't done this, but I've had the same problem and would be interested in finding out what others have done Spitballing here... - I could imagine maybe writing your own it.silent function that runs the test you pass it, but stubs out error printing and restores it after? - Override toString as a noop just for the error classes that you expect to be thrown? - Patch jest.fn, mockImplementation, and mockRejectedValue to intercept the error and re-throw it with some added property that will tell your logger/console to ignore it?
Want results from more Discord servers?
Add your server