Introduction to Node.js Introduction to Node.js

Debugging & Testing Exercise

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

Students are directed to find and debug several issues in the app, and then implement unit tests to ensure that they stay fixed.

Get Unlimited Access Now

Transcript from the "Debugging & Testing Exercise" Lesson

[00:00:00]
>> Scott Moss: All right, cool, cuz we're going into the next exercise, which is gonna be testing and debugging. So you have to debug and track down some issues in a small app, very small app. Use your logging and inspector to find them. Try and fix them, once you do, write some test to make sure it stays fixed.

[00:00:20] It's like real world stuff. This is real life. It's do or die right now. So basically, checkout to the start branch if you haven't already. Check the README on how to execute this program and run the test. There are gonna be 3 bugs. You have to find them and fix them.

[00:00:37] There should only be three. If you find more than three, I didn't mean to put that one in there. [LAUGH] That's extra credit. But there's definitely three bugs, at least three bugs in there. Write some units tests with Jest to make sure those bugs stay fixed. Refactor the code if you have to.

[00:00:53] So definitely do that. So let's go into it. So if we go into Exercises > Testing is the folder you want to look into, you're gonna see something you've never seen before in the API file. We're creating a server. This time it's an API server, not a static server.

[00:01:11] And it's using a framework. Before we used HTTP. This time, we're using something called Express. Express is a framework around HTTP, something I said that you would probably use in the future, you'll never use HTTP again. Well that's because you'll probably use Express. Express is a server for that.

[00:01:24] You don't really need to understand how Express works, because what you're gonna be doing doesn't have a lot to do with Express at all, just like you didn't have to understand Commander. But it's nice just to see how a server is created here in Express. But basically the problems are gonna be inside of users.js.

[00:01:40] There's some issues in here that just aren't adding up. So what this app should do is, if I do a get request to /user/id, I should get back a user with that ID. If I do a delete request to /user/id, it should delete that user and I should not be able to get it.

[00:02:02] So if we run this command to start the server which would be,
>> Scott Moss: node/testing/index.js.
>> Speaker 2: I think you're missing an async.
>> Scott Moss: I'm missing an async. On APIGS.
>> Scott Moss: It's right there. There you go, yep. So there was four bugs. So if you run this, you'll get a localhost:3001.

[00:02:37] And then if you try to do some API calls to it. So I'm using the HTTP client that I have called HTTPie. And I would say 3001. And I'm gonna try to do a get request to users with an ID of like 1. And right now, let's see what happens.

[00:02:54] Right now this thing kind of freaks out.
>> Speaker 3: Is there a different tool that you can use then HTP [INAUDIBLE] because we had issues in the previous course of using that as the demo.
>> Scott Moss: I can just use it in the browser. I can just go right to the browser and do it.

[00:03:11] So let me start the server again.
>> Speaker 3: Or Curl or something, I don't know.
>> Scott Moss: Curl, yeah, Curl is bad, I'm gonna stay away from Curl, but I'll just go with it. I mean, the browser is pretty simple, I'll just go in the browser. I'll just say 3001/users/ some ID like that.

[00:03:32] So you see like it's just spinning, something is happening here. If we go look at the output here you get like some deprecation warnings or something like that. But ultimately nothing is being returned, nothing is happening, so there is some issue is being hung is what people would say.

[00:03:48] The server is hung up on something. So normally that means there's some asynchronous thing that's like not responding correctly. So you gotta fix that. You gotta figure out why it's not going for it, why it's not progressing, and what's actually happening. What would normally happen, if this was fixed, is I'm gonna check out to master here.

[00:04:18]
>> Scott Moss: Yeah, I gotta put the weight back in there, async.
>> Scott Moss: Cool, so if I did that If you go look inside of this users file, you'll see I'm just mapping over an array of 20 and creating users with IDs of the index of 0 to 20. So there are users with the IDs of 0 through 20 in here.

[00:04:38] So long as I ask for a number between 0 and 20, I should get back a user. And I want to be able to do the same thing with delete and have it delete a user. Git check out start. If you will look in the test file here, you're gonna see some tests in here that already are testing things.

[00:04:58] But if we already have that bug, and this test is already here, that means our test is probably not right. So you need to write some better tests that ensure the fixes that you did aren't gonna relapse and break again. All right, so these tests probably just aren't that good like here you see I'm trying to do some stuff but it's just not quite working because we still have bugs.

[00:05:20] So you're gonna have to figure out what test the right to make sure the fixes that you did are actually working. So again, most of the issues are gonna be inside of users.
>> Scott Moss: Around here somewhere, and you're gonna have to use your debugging skills to figure out what the issue is, because I should be getting back a user for an ID.

[00:05:38] I should be able to delete a user.