Current section: Unit Testing 6 exercises

Intro to Unit Test

Loading lesson


00:00 All right, we're going to start unit testing. Woo! We're going from the top of the trophy to the bottom part of the trophy. So the very bottom of the trophy is the static testing, like ESLint, Prettier, and TypeScript. But right above that, this little section, unit testing. Unit testing is typically you use

00:18 this for very dedicated functions that have highly complex logic. You typically are going to cover your button components and stuff like that by just testing other things. But yeah, unit tests are for those functions that contain a lot of complex business logic in them.

00:38 And so they can be very useful to have a special test all on its own. So if we had a validate password function like this, which, yeah, it's probably not the best. I would use Zot. Zot is a lot better for validation. But if that's what we had, then this is how you would do that. We're using VTest for doing that.

00:56 And we've got our validate password. We've got a bunch of tests for each one of the different cases. And that's going to cover all of the use cases for our password. This one's really nice because it's a pure function. It takes an input, returns an output, doesn't touch anything with global scope or anything like that. So totally pure function. And those are my favorite things to unit test

01:16 because it's so easy. You can literally do it in a single line for lots of this stuff. Not always is that the case for these level of tests. But sometimes if you really want it to be a true unit test, you have to take all of the dependencies that it has and mock those things out.

01:35 I don't really care too much for these distinctions between unit tests and integration. I did make the testing trophy, so I guess I care a little bit. But what's more important to me is that we're getting confidence out of the test that we're writing. And so in any case, sometimes it is necessary to mock things.

01:53 And we've mocked HTTP requests already, but now we're going to be mocking console.error specifically in this exercise. But sometimes you need to make a fake version of things. And so VTest, the framework that we're going to be using for this, has utilities for mocking functions and things like that.

02:10 So it's going to be pretty cool using some of those. Another thing that's important or useful for you to know is that VTest actually runs all of your test files in parallel in separate processes. So they can all run at the same time. Makes them a lot faster. So that's just useful for you to know that, yeah, you're not going to be able to share global state between those.

02:30 But you can share setup, which we're going to be talking about in this exercise. And then VTest also has this amazing watch mode. And if you press the H key, then you can do a bunch of really cool things. So you can say, hey, I want to just run the one that says returns. So we've got two of those that say return. And I can hit T, returns, and boom.

02:50 Now we're just running those two and we're not running this one. And then we can hit H again, and we can press A or return to rerun all tests. Boom, now we're rerunning all tests. So you've got a bunch of cool things that you can do with watch mode. I recommend that you spend a little time exploring that. And that should be everything that you need to get going for this exercise.

03:10 I hope you have a good time with it.