Udemy

Introduction to Mocha & Assert

A free video tutorial from Murtez Alrohani
Software Developer, Consultant & Local Nerd
Rating: 4.5 out of 5Instructor rating
1 course
31,513 students
Introduction to Mocha & Assert

Learn more from the full course

Node.js Unit Testing In-Depth

​A focused, in-depth look into Node.js unit testing, from absolute beginner to very advanced.

02:17:16 of on-demand video • Updated August 2022

Students will be well versed in how to unit test applications, they will be ahead of most other developers in that regard.
English [Auto]
In order to start unit testing, you need a testing framework. And the most popular by far is mocha. Chances are you're going to see it in every company, every project. If you go to Google and search for Mocha or go to their website, which is Mocha Js.org, you can take a look at some of the documentation. It's well backed and fairly fairly well done framework. So that's the first item we're going to use. The next item we need is an assertion. Library and Node comes with a built in assertion library, but it's very basic. You can use it for like for very simple projects. But to be honest, it's just it doesn't have enough power to test a real application. If you go to Google and search for Node assert, it will be the first option and feel free to take a look at the documentation If this is something you like, you know you can use it. It's there. You don't have to do anything extra. But as I said, it's just like it really is awkward to work with and doesn't have enough options to make a good assertion. Library I'm going to create a new project using the default settings. Just to show you the basic structure. Then I'm going to install mocha with NPM install mocha minus G, which makes it a global item Since this is going to be used on my system for everything I'll install globally. I'm going to create a directory called Test and I'll make a new file in this directory. Call it Test.js for now. The first thing you need is a describe block. And this is where all your tests go. It takes two parameters. First one being a string. And generally, this is going to be the file name you are testing. So if it's a JS, I'm going to put Vuejs here. The second parameter is a function and we can put everything else here inside the describe block. I'm going to create a context and its context is just an alias for describe, but you can use it to distinguish now by convention, this is going to be the function to be tested. So if I have a method called foo, I would put foo in here. And again, the second parameter is a function. And inside here we're going to write our tests and the tests go inside of it blocks. I know it sounds weird, but basically you say it and then it takes a string parameter and then another function. And you would phrase this like you would say a sentence. So if if a function is supposed to save a user, I'm going to say it should save the user, it should delete a file, it should add two numbers and so forth. And for each test you will have an IT block. So you'll have one described for the whole file, a context for each function and then it for each single test. So. Let's begin doing some demos here. First, I'm going to include the default assert. I'll say const assert equals require, assert. Then I'm going to say assert dot equal 1 in 2. I'm going to go back to my terminal, see my directory. Then you run the test with Mocha. Then the file name. So we have a failing test because one doesn't equal two, obviously. Let's go back. And change this to one. Rerun our test. Now it's passing. Let's create another test. So it should do something else. Assert DB equal, let's say name. Joe to name Steve. Let's save this. Run the test again. Now it shows you that these objects don't match. And down here, it will show you what it's the what is expected and what the actual value is. So you can look at the error and fix it. I'm going to change this back. And rerun my test. Now we have two passing tests. Next, we're going to look at a pending test. And basically that means a test that has nothing. So I'm going to say this is a pending test. Let's go back and rerun. Now you see, the third one doesn't have a check mark beside it. The third item and it's in a different color. And at the bottom it actually says two passing, one pending. So. Sometimes it helps to create a test for a function that that you're not ready to test or that may not exist yet. And this is a reminder for you to go back and finish this in the future. And this is the very basic structure of how you use Mocha. We'll get into a lot more detail through the rest of this course.