Course info
May 4, 2017
2h 28m

Building good and comprehensive tests for your JavaScript applications helps enable stable and robust applications. Good tests also serve as valuable documentation for what your applications are actually supposed to be doing. In this course, Testing JavaScript for Node.js with Mocha, you'll dive deep into Mocha, one of the most popular and robust testing frameworks for JavaScript. First, you'll start by learning the ins and outs of testing with Mocha. Next, you'll explore how to implement a BDD style of assertion language with Chai. Finally, you'll cover the right way to mock objects and functions with Sinon, and you'll develop an understanding of how to track your code coverage with Istanbul. By the end of this course, you'll be able to build a robust test suite against your node.js applications.

About the author
About the author

Jonathan is a Pluralsight Author, Technology Advisor, and Business Leader. As a member of the Chief Digital Advisory team at World Wide Technology, Jonathan is able to leverage his unique experiences and skills to drive digital transformation for his clients. As a dedicated developer community leader, Jonathan serves on the board of directors for the Kansas City Developers Conference, is a Microsoft MVP, and is a regular speaker and keynote presenter at conferences around the globe.

More from the author
RESTful Web Services with Node.js and Express
2h 2m
Jan 24, 2019
More courses by Jonathan Mills
Section Introduction Transcripts
Section Introduction Transcripts

Course Overview
Hi everyone. My name's Johnathan Mills, and welcome to my course, JavaScript Testing for Node. js with Mocha. I'm a consultant dealing with JavaScript, and one of the things I noticed with JavaScript developers is everyone says, we should be testing our code, but not as many people are actually doing it. And I think part of the reason for that is that getting tests set up and going is not an easy thing to do, plus there's a lot of moving parts, and it's just one more thing that we all have to learn. And keeping up with the JavaScript ecosystem is not exactly something that's easy to do. But in this course, we're going to simplify that process. So we're going to break it down to the minimum parts needed to get testing going. So we're going to start with a deep dive into Mocha, and we're going to make sure we understand how that works and what's going on in there. And then we'll add in assertion and mocking frameworks to make our testing easier. And then we'll pull that all together and test a real application, so that we understand how things work all together. By the end of this course, you'll be able to set up tests for your own application. But before beginning the course, you're probably going to need to be familiar with Node. js and JavaScript, otherwise it's not going to make a whole lot of sense to you. I hope you'll join me in this journey to learn all about Mocha with this course, at Pluralsight.

Testing with Mocha
Now that we've got a basic understanding of what tests are and kind of what we're looking for, we're going to take a whole module here and just dig into the mechanics of Mocha. Now Mocha really is just a test runner. There's several test runners out there, there's Mocha, there's Jasmine, there's Jest, and there's even a couple others, and all Mocha is is just a mechanism by which tests are executed. The tests itself are not Mocha, they're your tests that are just written in JavaScript. And so what we're going to do in this module is dig into the mechanics a little bit of Mocha, and how it works, and what the pieces are, and how you make you it do different things, kind as you go. And we're going to start with describes and its, and so we're describing something and then we're dictating what it does, and that's how Mocha breaks up its sections of tests. And so we'll talk about that. We'll talk about the lifecycle of a Mocha test. We'll talk about before, beforeEach, after, afterEach, and all these lifecycle hooks that you have, to help you have more control over what's created when as you run your tests. And we're going to end with a conversation about how to manage what tests get executed. You don't always want to run all of your tests. So let's talk a little bit about how to have only some of your tests execute, or just skip over some tests altogether. And so that's what we're going to do as we go through this module.

BDD Style Assertions
Alright, so we've got a good understanding of Mocha and how Mocha works and what all the pieces are, we're not quite to the point where we're ready to just wholesale test an application, but we're getting much closer than we were just a minute ago. One thing I want to talk about in this module is our assertion style. We've been using assert., and that's kind of clunky, and we don't really like the way that that feels. We've got a very nice clean feel for our describes and our it shoulds, and all that kind of stuff, but assert is kind of clunky, and so I want to walk through using Chai to help clean up our assertions a little bit, and make them a little bit easier to read and just easier to follow so it flows a little bit better. Now Chai has two options for assertions and for BDD style assertions. They've got expect and should, and I don't care which one you use. And in fact, I'm going to kind of show you both side by side so you can see how that works and what that looks like, so that you can decide on your own which one you like better. And we're going to end with a talk about objects. So right now we've only been asserting whether or not something is true or false, and that doesn't really get us too far. We're going to kind of dig into assertions a little bit more so we can check on both values and objects to see how things work.

Spys, Stubs, and Mocks
Up until now we've kept things pretty easy and pretty straightforward and unfortunately that's not the real world. In the real world things become very complicated, very, very quickly. And so what we're going to do in this module is start talking about how we mock objects. And what I mean when I say that is, we'll start passing objects around. And we need to keep track of functions on those objects and make sure that things are called when we expect them to. And when we're unit testing we don't necessarily want to test the other objet that we're passing in, we just want to isolate the thing we're dealing with. And so mocking objects is required to do some of that. Luckily for us we have this cool tool called Sinon that's going to handle all of this for us. And it becomes a pretty straightforward thing, not as straightforward as we might like, but we'll talk about it for just a little bit and we'll get down to the bottom of it, and how all of this stuff works. Let's take a minute, get everything installed, and paint the picture for what we're going to do, and then we'll get into mocking objects with Sinon.

Testing Real Things
Alright, we've come quite a ways in this course, and we've talked about a lot of different things, we've talked about Mocha and Sinon, and all of those types of things, but everything up until now has been fairly contrived. And I'd like to take a break from contrived examples and start talking about reality, and talk about things that we'll actually run into every day as we're working. And testing is not always as straightforward as it's been made out, up until now. Sometimes we're dealing with modules that get imported, that I might not have access to, or databases and HTTP calls can be complicated. Databases because we're pulling in and we're using packages that are just imported, and HTTP calls dealing with streams, and we haven't talked about streams and how to mock streams yet. So we might have to deal with that. So let's take a look at some realistic scenarios. They're not quite full blown because I still don't want to get overblown with everything, but let's take a look at some real scenarios that deal with some real things, in a little bit of a controlled environment, so we can understand how you put all of these tools together in practice.