Introduction to Node.js, v2 Vanilla Unit Tests
Transcript from the "Vanilla Unit Tests" Lesson
>> All right, so let's keep it moving. And we will go on to everyone's favorite subject. Testing, right? Everyone here loves testing, I would imagine. So we're gonna talk about that. And how, honestly, node JS is like the perfect tool for testing. It's so good with testing that I was trying to remember the last time I've written tests and like ran tests in a browser, that wasn't like an end to end test.
[00:00:28] It was like a unit test and I don't even remember. I know I did it, I won't play, I just don't remember when it was, and that's how long ago it's been since I've been using nodes to write tests. So yeah, like I said, one of the most common use cases for node is writing tests for node JS and front end app.
[00:00:46] So you can do kinda all of it, it's really versatile. First, let's kinda talk about what unit tests. So if you haven't written tests before, the different types of tests, let's talk about unit tests. So unit test is basically it will test like a little chunk of your code, a unit, it could be a little function, it could be some constant variable somewhere.
[00:01:08] It can be pretty much whatever you define as a unit, but it's basically testing it in isolation and not how it works with something else, right? So you won't be testing like how this system works together, how this API call handles everything in the middle-ware stack all the way down to the response.
[00:01:24] That wouldn't be a unit test, that would be like an end to end test. You're testing everything from end to end and how they all relate to each other. You give us an input, you run through a bunch of stuff, and you'll see the output is and what will happen along the way.
[00:01:38] Whereas a unit test is like, I just wanna see how this one thing works by itself without anything else interacting with it. So node JS is really perfect for that. And I'm gonna show you how we can do that. And just like the HTTP and the server section, I'm gonna walk you through how to do this.
[00:01:53] The manual way that's built into node, then I wanna show you how you should actually do it. But by learning it this way, you'll see how it's all working, but sometimes it could kind of feel like magic. So this is really cool module built into node called assert.
[00:02:09] And assert kinda sounds exactly like what it is. It's gonna be used to do assertions against different truthy statements, right? We can assert that this statement must be true and if it's not, it's gonna throw an error, that's basically it. And most of the testing frameworks are built on top of the assert module.
[00:02:28] So we're gonna use that to write some tests. So I'm gonna make a new folder here. I'm gonna call it my, I'm just gonna call it lib. So we got lib here, and make a new file and call it a Mylib.njs and I'll make a new file I'm gonna call this lib.spec.njs.
[00:02:57] So let's do that. So for the lib we can pretty much do anything we just need to write some code that we can test. So I'm just gonna say cost add, it's gonna take some numbers. And it's going to return num1 times num2, right? I know it's add function and multipliers I get it, but we're gonna see how the test fails and we can try to fix it.
[00:03:24] So we got that, in order for me to test this code, this unit, I have to export otherwise I can import it. So I'm gonna export this. And now I can actually import this into some other file, which then I can use and test. So in my other file, which is gonna be the spec file, we're gonna import that method from, not this file, but myLib.njs.
[00:03:54] So one thing, I don't know if I mentioned this, but when you're importing njs files, you do have to explicitly put the extension. If you don't put the extension, I don't think it'll work. Whereas .js files, you don't have to, and/or if you went into the package,json and said hey, I wanna use ECMA script modules type module.
[00:04:16] Then I don't think you have to do the extension. But we do, I'm being explicit that way people don't get confused. I've seen some people get confused with this, so I just learned to just be explicit about it that way it works. Maybe one day when no js file is like, all right, everything is ECMA script modules and with other details, then I'll figure it out.
[00:04:33] But for now, I'm just gonna be explicit about it. Because you can't go wrong, there's no confusion. So we got the add, and then we need to import assert from assert because it's built in. And then we can assert to see if this add function does what it's supposed to do.
[00:04:51] So the first thing I'm gonna do is I'm a console.log, that we're running the add function. And I'm gonna say, make a little break line here, I'm gonna say should, Add two numbers. So I'm gonna just log that. And then I'm going to assert, Actually, I'm gonna try catch this one.
[00:05:15] Let's try catch the assert that add, that takes 1 and 2 should equal 3, because 1 plus 2 equals 3. If that fails, I'm gonna catch the error. And I'm gonna console.log. Fail. And then I'm gonna console.error, the error. But if it passes, we're gonna say success. So here's our little test using the assert library.
[00:05:55] So let's give it a shot, see what happens. So I'm gonna go over to our terminal and back out of this, go to lib. I'm gonna run node. What was it? Lib .spec. Run that, and for some reason, it says success, did I not do that right? I will not say success, hold on.
[00:06:21] MyLib at, [LAUGH] wait, why did I do that? Hold on, I think I might use a certain wrong. I'll go back to my notes, .strictEqual, okay, sorry. Okay, so let's go back into it and do .strictEqual. I'm not used to that change, because I don't really use a search, it's too low level.
[00:06:46] But yeah, strictEqual to see if those two things equal each other. And as you can see, we do get a FAIL here AssertionError. We say, expected value to be strictly equal 2 does not equal 3. And then you can see it logs out the assertion, which has the code, the actual, the expected, and then what operator were used to perform the comparison.
[00:07:09] It's pretty detailed.