Loading
Current section: Unit Testing 6 exercises
Problem

Managing Test Output and Error Logging with Console Mocking

Loading exercise

Transcript

00:00 This console error is giving me heartburn. So it's very important to me that we keep our terminal output as clean as possible. When I showed up at a company that will remain unnamed and I ran the tests, the log output was just outrageous.

00:18 There was an enormous amount of logs. And as I was developing tests, it was very, very difficult for me to identify where my errors were and what was causing the errors. It was just a nightmare. So you want to just be very vigilant about making sure that the only logs you get in your test output are logs that are relevant

00:39 for whatever test. So if things went well, then don't log anything other than to say the thing went well. So your code, your application code should not log anything. But we don't wanna change this. This is an important feature, right? If I just console log that out and now it's gone and that's great.

00:59 But it is an important feature to have some form of logs. Now, often in an application, you're going to have some sort of logging service and you'll be calling that instead of doing console.error, console.log. But not always, sometimes it is useful just to use the console and lots of services will just slurp whatever you pass to the console

01:17 over to the logging service as well. And so, and on top of that, sometimes you wanna test an edge case or like make sure that the right thing happens when the wrong thing happens. And that results in a console error coming from something outside of your control like React will console error sometimes as well.

01:36 And so when you're testing those types of cases, you're gonna want to make sure that of a couple of things actually. Firstly, you wanna make sure that the error does get logged. So you wanna assert on that. And then secondly, you wanna make sure that we don't actually end up logging to the console. So you're gonna mock that console error out.

01:55 So that's what we're doing in this step of the exercise. You're gonna mock out console.error so that we don't get this log in the console. And you're gonna use what we call spies. So I wish I had a magnifying glass or something. Yeah, spies, it's gonna be cool. So have a good time with this one. We'll see you when you're done.