Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Bring up unit testing in a room full of software developers, and you're likely to get a range of opinions on how to do it. Regardless of those opinions, Nodes community offers unit testing tools to suit just about anyone. In this video, we'll take a look at a few of the reasons why you should unit test your code, as well as some of the testing features specific to Node. While there are a wide range of unit testing methodologies out there. There are two broad approaches I'll bring up. The first approach, is to write unit tests first.
This is the ideal approach. It's ideal, because you can catch bugs right away. Often, writing a unit test will reveal problems with your approach to a problem, before you get too far with writing the actual code. I find that writing tests first works best when you're writing individual pieces of reusable code. However, there's a pitfall with this. It's not always practical to write tests first. Sometimes, you have to write code that's inherently not testable. While this shouldn't be an excuse to abandon unit testing altogether.
Sometimes, untestable code is unavoidable. The other approach is to write your application, and then unit test it. This approach helps you find bugs in the code you've already written. When you write unit tests after writing application, it helps you define how the application is supposed to behave, and that can help you catch breakages later on. However, the biggest pitfall with this approach is that you don't get the benefit of fixing bugs before you write your code. That's something that writing tests first will help you with.
Now I have a confession to make. For the longest time I held out on unit testing my code. The tools in the coding environment I was in were difficult to install let alone use. Even worse, the people advocating testing seemed to always make it an all or nothing proposition. Unit testing became this unapproachable ivory tower. So I just didn't bother with it. However, my opinion changed when I approached node. Due to the frequent changes earlier on in the project, unit testing was encouraged earlier on in the community.
Consequently, a good number of the modules on NPM include unit tests you can run for yourself and learn from. Let's take a look at some finished unit tests. I'm here on the command line, in a final project. Now I'm going to run some unit tests that I've already written in this project. I'm going to use the mocha command to run these unit tests. Mocha is a unit testing framework that I've downloaded from MPM. To run it I'm going to type mocha and then I'm going to press Return. I've written three unit tests and all of them are passing.
Let's take a closer look at the project. In the package.json file there's a new section called devDependencies. These are all modules that are helping me unit test my code. Regardless of how you approach unit testing a node, all the tools you need are available on NPM. When you set up your project, always include the unit testing tools in the devDependencies section. In our next video, we'll take a look at our existing application and prepare it for unit testing.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92464 Viewers
82 Video lessons · 104136 Viewers
71 Video lessons · 75596 Viewers
56 Video lessons · 106972 Viewers
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.
Your file was successfully uploaded.