Check out a free preview of the full Introduction to Node.js, v3 course:
The "Types of Tests" Lesson is part of the full, Introduction to Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott discusses the different types of testing in a Node.js application, which include unit testing, integration testing, end-to-end testing, and API testing.

Get Unlimited Access Now

Transcript from the "Types of Tests" Lesson

[00:00:00]
>> We left off on getting pretty much all of our commands done except for that web command where we are doing CRUD on our database using our CLI app. So if you want to take some notes from your terminal while you're in a pinch, you can do it now.

[00:00:13] You just run this CLI and you're good to go. So you don't need the note app anymore, Evernote, Notion, none of that. Just Use the CLI and if it's not good, make it better. That's the sweet thing about being an engineer. You can just make it better. Just go Google NPM thing and enhance it.

[00:00:30] Okay so the next thing I wanna talk about is some testing. I believe I think that's next. Yeah, it is. So talk about testing. So different types of tests. No one wants to talk about testing. But everyone wants tests. Everyone wished they had tests and nobody wants to write it.

[00:00:52] So let's talk about test. I mean, there's basically several types of tests that can be written and for the most part, it really depends on the type of app that you're making. So the first one is unit testing, which is one everyone probably have heard of. If you have a written test, it's basically taking a unit.

[00:01:11] In this case, that could be a function, that could be a module, that could be anything. One small unit of code and testing that individually. If I give it this input I get back this output, right and it has nothing to do with the app as a whole is on an individual level.

[00:01:27] So that is unit testing, integration testing is like, basically testing how different units work together. So I'm gonna take this flow, right, you might have a flow of like a signup flow and like everything that happens on the back end happens. It gets kicked off in this one function.

[00:01:43] And this function calls these functions and that calls these functions. I'm gonna test that whole flow. That's integration tests just like going through to make sure I've tested all these things. Individually they work well, but is it doing the right thing? Just cuz this function called add takes two numbers and adds it in that test pass doesn't mean it's working in the long run because it might be getting the wrong numbers.

[00:02:08] So we're getting the wrong answers, even though it does add those numbers correctly. So we got to test everything as a whole. So we can see that. So that's integration testing the end-to-end testing, which is similar to integration testing, but it's like, literally go to the beginning of your app.

[00:02:22] Which is probably like a user clicking on the thing and then going all the way back through making a request, going all the way through your server, going all the way through your handler, through your database, and then coming back with a response. So it's like end to end, literally doing the same thing.

[00:02:39] So it's not just like testing an API call it could be testing an entire flow from start to scratch. I wanna test the entire Sign up flow from someone clicking this button on the front end till it actually signing in and everything in between. It's an end-to-end test.

[00:02:56] Most end-to-end testing involves a browser because you have to simulate human interaction. In end-to-end testing basically you need a browser for it because you have to simulate user interaction. And clicking, events, scrolling, anything a user can do, you pretty much have to simulate that on end-to-end testing. So there's different types of tools you can use to spin up browsers.

[00:03:20] You might hear the term called a headless browser. Anybody ever heard of that before? Headless browser is the same browser we're looking at right now, except for it doesn't have a UI. So why would I use that? Well, because the code is still gets executed and it still gets ran, which therefore you can test even though you don't need to see it.

[00:03:38] And it's just faster. It's more memory efficient. And it runs in other environments that also don't have a UI like it can run in your terminal because it's headless so you can spin up a browser that doesn't have a visual part to it, but you still get to execute browser code, which means you get to run tests on it.

[00:03:54] So it's just a way to run automated browser tests. So you'll see that. There's tons of companies whose foundations are built on end-to-end testing. There's even companies that allow you to install a Chrome extension and go and click on the things, the flow that the person needs to take, and then record that, and now that's a test.

[00:04:10] So like there's just or you can write scripts. There's just so many ways to go about it these days with end-to-end testing. Another one is kind of like API testing. I mean, this one's very similar to integration testing, but it's basically just like, making sure your API routes work.

[00:04:27] So if you have API routes the difference is, I guess what integration testing is like you're testing to see the functionality with inside of a certain flow or it's like API testing is like you're probably looking at functionality. But you're also looking at like the responses of the APIs, like the headers, like all these things associated with calling an API are correct.

[00:04:48] Whereas the integration test would probably only be testing the logic that gets executed when you hit this API. Or the API testing is like testing that whole from start to finish of that API. Not so much as like an end-to-end, but just that API function. I wanna make sure it responds with the right headers, the right status code, it handles this error and all these things, it triggers this or whatever.

[00:05:11] So that's API testing. And there's probably other made up names or names I don't know about for testing that people do. There's regression testing. There's snapshot testing. There's so many different types of testing. You'll probably never do all of these at once because nobody likes doing it. Until like that one person who's just like, I'm gonna sit down and write tests tries to do it and they realize that it's just really hard.

[00:05:38] So it's unless you work at a company whose testing culture is like really strong, chances are you're not gonna have to do all these tests. But everybody wants some of their super beneficial. Yes.
>> Just making a note that Kent C. Dodds has a JavaScript testing practices and principles course.

[00:05:59] And then there's some more advanced content like Steve Kinney's testing apps with Cypress. We also just released a enterprise UI development course that covers a whole range of testing in the context of a large enterprise code base.
>> There you go. Check out those courses if you wanna learn more about testing.

[00:06:18] Testing is, I mean, that can be someone's job, it's a thing it's a big thing. So it's like when you get it right, it feels good. It does, when you get it set up and you're just like, it's so nice to have it but man said enough, it's just like so I get back to doing other stuff, but it's really good.