Introduction to Mocha & Assert

Murtez Alrohani
A free video tutorial from Murtez Alrohani
Software Developer & Instructor
4.4 instructor rating • 1 course • 27,945 students

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 March 2020

  • 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 you testing, you need a testing framework, and the most popular by far is Mulcare, chances are you're going to see it in every company, every project. If you go to Google and search for Moka or go to the website, which is a mock JASTA, or 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 note comes with a built in assertion library, but it's very basic. You can use it for like four 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 that 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. We're going to create a directory called Test and I'll make a new file in this directory. Call it Test Digest's. For now, the first thing you need is a described lock, 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 filename you are testing. So if it's a food, guess, I'm going to put food digest here. The second parameter is a function and we can put everything else here inside the described block. I'm going to create a context and context is just an alias for described, 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 will 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, if a function is supposed to say the user, I'm going to say it should save the user, it should delete a file, it should two numbers and so forth. And for each test you will have an X block. So you'll have one describe for the whole file, a context for each function and in it for each single test. So. Let's begin doing some demos here. First, I'm going to include the default assert. I you can't assert. Equals require assert, then I'm going to say assert. That equal one into. And I go back to my terminal. See my directory, then you run the tests with Moka, 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, a third dip equal, I see name. Joe to name Steve. Let's say 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 it's expected and what the actual value is. So you could 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 checkmark beside it, third item, and it's in a different color and at the bottom, it actually says to 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 Momoka will get into a lot more detail through the rest of this course.