Viewers: in countries Watching now:
Testing code is much easier when unit testing frameworks are used. In addition to frameworks, there are also unit testing tools available that can be used with any frame work. In this video, we'll install some of these tools, then write some unit tests. First go to the exercise files, and then open up chapter six and video four. Copy the start folder the desktop, then go to Terminal. I'm going to change directory to the start folder. And now I'm going to type npm install.
So now that I've installed the existing modules, I'm also going to install three more modules that I'm going to use for unit testing. They're called Mocha, Should, and Supertest. Now these modules are only intended for the unit testing of our application, and they're not intended to be a permanent part of the normal operation of the application. So when we install these modules, we're going to tell npm that we want to place them in a specific part of our package.json file. To do that, we're going to use the flag called save dev.
So, now I'm going to install these three modules with save dev. So, to do that, I'm going to type dash dash save and then dash dev. I'll do that for Mocha, and then I'm also going to do it for Should and Supertest. So I'm going to press the up arrow on my keyboard, and then just change Mocha to Should, and then I'm going to do the same for Supertest. Now before we move on, let's take a look at that package.json file. I'm dragging the start folder into sublime text, and now I'm opening up package.json.
You'll notice that on line 12, we have a new property called devDependencies. And it's an object that includes Mocha, Should, and Supertest. This way our package.json file is identifying these three modules as ones we're using for reunit testing. Let's take a closer look at these modules. First there's Mocha. Mocha is a unit testing framework, that I'm fond of. It allows you to find a sweet of tests you want to run, then provides a done function you can call with each individual test is done.
And finally, since we're testing a web application, I'm also going to use Supertest. The Supertest module lets you simulate HTTP requests to your express application. What's nice about it is that you don't have to fire up the application on the specific server port. Supertest just tests the routes you've defined in your application. So let's go back to the code. I'm going to open up the Start folder in Sublime Text. And then I'm going to go to test and flightspec.js. For now, I'm going to require Should and Supertest.
We're going to use them later. And now let's write some unit tests. To start a suite of unit tests in Mocha, you use the describe function. The first argument you pass into describe is a label for this unit test suite I'm going to call this flights. The second argument you pass into describe is an anonymous function. Within this function, is where you write your individual unit tests. So let's write a couple of tests now. To write a test, you call the it function.
The first argument you pass is a label describing what it should do. So I'm going to say it should pass. And then the second argument you pass in is a function. That function is provided with one argument called done. So I'm going to call that done function right away. So this unit test is going to pass, because we're not really even testing anything, we're just calling done and it's going to pass. Now let's write another unit test. In this unit test, I'm going to throw an exception.
So even though I'm calling done at the end, this throw key word should throw an exception which should break the unit test. So now that we've written a couple of unit tests, let's run them. So there are two ways that we can run these unit tests. The first way is we can modify package.json and in the script section, we can add a test property. And then in that test property, we'll tell npm how to unit test this module. An easier way to do it right away though is to install Mocha as a global module, and then run the Mocha command from Terminal.
So let's do that now. I'm going to install Mocha as a global module by typing npm install -g Mocha. This fails though, because I'm not an administrator on this computer, and the Node modules folder is protected. So to fix this, I'm going to type sudo at the beginning. I've pressed up on the keyboard to get the last command, and now I'm just going to preface this command with pseudo. So it asks for my password, and now it installs Mocha. So let's run these unit tests.
I'm at the root of my application, and I have a folder named Test, so if I type Mocha here, it will run the unit tests. So it's saying I have one passing test, and one failing test. The failing test is because we're throwing that error. So if I comment out the throw key word, and I run the unit test again by pressing up on the keyboard, and pressing Return, I now have two passing unit tests. A unit testing framework makes it easy to write and organize your unit tests. Unit testing frameworks, like Mocha, are flexible, in that you can use any other tools you'd like along with them.
In the next video, we'll use the Supertest module to simulate HTTP requests.
Find answers to the most frequently asked questions about Node.js Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.