Introduction to Mocha & Assert

Murtez Alrohani
A free video tutorial from Murtez Alrohani
Software Developer & Instructor
4.5 instructor rating • 1 course • 27,471 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 testing you need a testing framework. And the most popular by far is more 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 their website which is a moka Esther 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 no that comes with a built in a certain library but it's very basic. You can use it for look for very simple products but to be honest it just doesn't have enough power to test a real application. If you go to Google 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 of a certain 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 Moka with NPM install moka. Mine is G which makes it a global item since this is going to be used on my system for everything all installed globally. Willing to create a directory called test and make an you file in this directory. Call it digests. 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 food allergy it's going to put food digests 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 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 for 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 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 add two numbers and so forth. And for each test you will have it block. So you'll have one describe 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 as a can't assert require assert. Then I'm going to say assert that equal one into and then go back to my terminal see my directory. Then you run the test with Moka in 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 the equal say 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'll show you where it's the what is expected and what the actual value is. So you could look at the air and fix it. I'm going to change this back and rerun my test. No 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. The third item and it's in a different color 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 Moka will get into a lot more detail through the rest of this course.