Isolating existing code for testing
Video: Isolating existing code for testingWhen you're approaching an existing application to unit test it for the first time, you may find certain pieces are tied together in such a way that you can't write effective tests. In this video, we'll look at our existing express application and find ways to decouple some of the code. Let's go to the exercise files first. Go to chapter six, video two and copy the start folder to the desktop. Now let's go there and install the dependencies in terminal. I'm going to type cd, and then drag the path in, then I'm going to do npm install and press Return.
Viewers: in countries Watching now:
- Why use Node.js?
- Installing Node.js
- Understanding the event loop
- Initializing Node.js projects
- Creating modules with getters and setters
- Starting Express applications
- Testing your code
- Working with sessions and databases
- Building command-line tools
- Emitting events and attaching listeners
- Controlling readable streams
Isolating existing code for testing
When you're approaching an existing application to unit test it for the first time, you may find certain pieces are tied together in such a way that you can't write effective tests. In this video, we'll look at our existing express application and find ways to decouple some of the code. Let's go to the exercise files first. Go to chapter six, video two and copy the start folder to the desktop. Now let's go there and install the dependencies in terminal. I'm going to type cd, and then drag the path in, then I'm going to do npm install and press Return.
Now that the dependencies have been installed, let's have a look at the code. I'm going to drag the start folder into sublime text. So first let's take a look at routes, index.js. We have a problem here on line six. These route handling functions are specifically tied to the set of data, that's here on line six. We also have another problem. Let's go to app.js, and at the bottom of the page, we are creating an HTTP server and starting it up. So we're defining the application throughout the rest of the file, and then we're also starting a server in the same file.
So that means whenever we pull in this application, we're always going to get a server starting up at the same time. To effectively test this application, we're going to need to separate out the data and the server. The reason for this is that the data is subject to change. We may remove a flight down the road. If we build a unit test around a flight and then remove it from the database, the test is going to break. Another problem we have, is that the application is running on a specific port number. We should be able to change the port number at any time without breaking our unit tests.
So let's begin to break apart this application and make it more suitable towards unit testing. The first thing we're going to do is create a new entry point for this application. I'm going to call it server.js. So the first thing I'm going to do, is move over all of the HTTP handling functionality into this file. So first, go to app.js, and then cut line eight. We're going to use that HTTP module again, but we're going to use it now over in server.js. Now scroll down to the bottom of the file, and cut everything from line 36 to 38.
This is where we're creating the server and we want to do this in server.js rather than app.js. So now, we need to get this app object, we need to tie the application to this server.js file. Let's require app. So, I'm going to do app and then set that to require and then I'm going to do dot slash for the current directory app. So that's going to load up app.js and store it in our app object. And finally, let's go back to app.js.
We need to export the application from this file. So I'm going to type in module exports, and set that to app. So before we move on, let's make sure that the server is still working. Go back to terminal, and then type in node, space, and now you're going to type server. This is going to load up server.js. And it works. The express server is now listening on port 3000. I'm going to press Control C to stop the server, and now let's go back and separate out the data.
So at the moment, we have the data tied here on line six, to all of our route handling functions. This is a problem, because if we want to use say a, database source in the future, or we want to use any other data source, we have to modify this file directly. What we ultimately like to do is, get the data here in server.js, pass it through the application, and then have the application pass it to the route handling functions. So let's do that now. I'm going to cut line six and go over to server.js.
I'm going to add a comma here at the end of this line. And then I'm going to add a comma at the end of the other line as well. And finally I've moved up a level in the directory structure, so now I'm just going to do dot slash data. So this will get the data here in index.js. So now I'm going to pass these flights into the application, and then I'm going to have the application pass those flights into the route handling functions. So to do that, I'm going to call app with flights. Now let's modify app.js so that it actually receives those flights.
What I'm going to do here, is I'm going to change what we're exporting. I'm going to take module exports from the bottom, and then place it here on line ten, and we're going to export a function. That function is going to take the flights as the single argument. And now I'm going to move all of this code into that function. I'm also going to move these three modules into the function as well. Then I'm going to return the application. And finally, I'm going to pass the flights into the route handling functions.
So now let's go to index.js in the routes folder. Again, we're going to wrap this file in a module exports function. It's also going to take flights as its single argument. And now, I'm going to move all the existing code into that function. Now, instead of calling exports, what I really want to do is return an object. Since we're already using module exports to return a function, we can't really use exports here to assign different functions to different properties. So, to handle that, I'm going to create a new variable.
And I'm going to call it functions. And I'm going to set it to a blank object's literal. Then I'm going to change each and every one of these exports to functions. And then finally, I'm going to return functions from this function. So now, we've decoupled the data. We're requiring the data in server.js, and this data could come from any source in the future. We're passing it through the application, and then we're passing it on to the route handling functions.
So let's start the server again and make sure it's still working. I'm going to press Up on the keyboard to get node server. And now the server is still listening on port 3000. Let's go to one of the routes just to make sure. I'm going to go to local host at port 3000, and then / flight / 33. And now it's pulling up the information for flight 33. The data and the server have now been decoupled from the application. When we go to write the unit tests, we can write a small file in place of server.js.
Through this file, we'll be able to configure the application in such a way suitable for unit testing. In the next video, we'll write this small file, and get some tools together to start unit testing.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
- Q: How can I install the new version of the Express command-line tool?
- A: Newer versions of the Express command-line tool can be installed using the command "npm install -g express-generator." But before doing this, you should first remove any older version of the module, using the command "npm uninstall -g express."
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.