This course has been updated! We now recommend you take the Introduction to Node.js, v2 course.
Transcript from the "Types of Tests" Lesson
>> Scott Moss: And then we're gonna talk about the types of tests. Basically everything can be tested with node. There's literally no code you cannot write that cannot be tested. You can test your packers.json if you want. If you can write it you can test it. There's just no way you cannot test it.
[00:00:14] And the different types are unit. So you have unit tests which are you're testing the small blocks of code. For instance, if I were to unit test, actually we wrote unit test here. So for instance, we wrote unit test if the length of the user is the length of the post, we wanted to see if api.getUserById what the ID would be.
[00:00:33] So we're testing individual pieces of code and not how they work together, right. We're not testing I wanna test this whole route to see if it does all of this stuff. No, I just want to test this one function that does this one thing, it says individual units and not how they work in a whole.
[00:00:48] Unit tests can get you pretty far, so definitely stick with a unit test. They're the easiest to do. They force you to write clean code, and yeah, they're pretty easy to set up. So start with those. Integration tests are tests where you're testing a lot of things integrated with each other.
[00:01:06] So I wanna test that this API call does this. That API call might do a thousand different things. It might do this database query, this database query, talk to Stripe, talk to Airbnb, go to analytics, and then it's done. And you wanna test that one API call. So it's like an integration test.
[00:01:22] So you would do that to test things that work together. That's also very helpful cuz you wanna make sure things do their job when they're connected. But you also should individually test all those different units separately inside of that route to make sure that they work independently and that they're doing their job.
[00:01:36] And the argument might be said, if you'd write some good unit tests, maybe you don't need integration tests. I don't know, it really depends on your system. But I know if you write really good unit tests, you're gonna go far. Just write some good unit tests. End-to-end testing is like that, but a step further.
[00:01:52] So it's like starting from a client, I wanna go all the way to the server and back and test that. So for this you would need some type of web thing, especially if it's a web app. So you would need some type of browser, whether it's a PhantomJS or hitless Chrome or whatever, because you need to actually load up a browser and type some stuff in a field, hit a button, and see what happens.
[00:02:14] That's a full end to end test. There's a user argument where is like, this is not needed. And yeah, I would say for the most part I never do end-to-end test, but I've also worked with a lot of big companies and they absolutely have the end-to-end test. So it really depends on your application.
[00:02:28] But this is a lot of work to do, and there's actually a lot of companies popping up now that solve just this one problem. So yeah, don't try to fix that one yourself. UI testing is almost like end-to-end testing but it's just for the UI. I'm going to take some, I'm gonna see if this CSS changed, I wanna do some, basically just anything UI testing, I wanna see if this interaction does what it's supposed to do.
[00:02:50] So either UI testing, whether it's on the browser or whether it's on node, you you do some type of UI testing. Snapshot testing is really cool. So you take snapshots of a component versus another component once it changed, and you make sure that there is no difference. And that snapshot can be an actual visual snapshot or it can just be a snapshot of the dom to make sure the dom is the same.
[00:03:09] So however you do it, node can handle it. We do the dom, so if we change it, if a component dom spits out with these divs and these classes in this order and then somebody did a PR and officer is dom is spitting out like this. Then you broke something cuz we did a snapshot test and they don't match.
[00:03:25] So that's what a snapshot test is. And then you have performance testing which is exactly what it sounds like, testing performance of different things, load balancing, stuff like that, and many more. I'm not gonna continue going. Everybody's probably bored about all these tests that I'm talking about, but yeah, there's just way more testing.
[00:03:42] I just wanted to talk about the ones that I used before and seen other people use, but there's just so many more different types of tests, especially when you talk about different frameworks.