Skip to main content

Unit Testing with Mocha

Image result for mocha unit test

Unit testing is the best way to uncover hidden bugs in your system. JavaScript Community has produced several testing frameworks that will help us with the task of writing Unit Tests. Some of them are Jasmin, Mocha, Jaster, Quint. These are all JavaScript Testing frameworks, that you can use to test JavaScript Code. Today we are going to focus on the Mocha testing framework. Let's go and install Mocha globally that we can use on any of our projects.

$ npm install  -g mocha

To run a Mocha test all you need is to type mocha. But you need to have directory named test. Before running mocha first make sure you have created a directory call test.

$ mocha

Now Mocha is ready to use. Now let's go and create our first test. Create a folder MochaTesting anywhere in your folder. This is the folder we are going to work. Inside that folder create a folder called test. Inside the test, folder create a file call my-test.js

We are going to use Test Driven Development. That means we are going to watch the test fail and then write our code to make our test pass. I am going to use a mocha function called describe(). In the describe function we are going to set up a suite of tests. I am going to describe our printName() function. We are going to test a function called print name. The second argument for the describe function is a callback function. This is where we are going to write all our tests for printName(). 

describe("printName()",function(){
  it("should print the last name first")

});

Now run the test by typing mocha in the MochaTesting directory.


Mocha found our my-test.js file inside our test folder and it created a suit for print name function. We can use it() statement to go and stub our tests.So now go and write the printName() test. In order to write this test first, we need an insertion engine. Mocha gives us a suit for describing, running and building tests but it doesn't give us a way to check values. So we need to use chai. To install chai locally in your project type the following command.

$npm install chai --save-dev

var expect = require("chai").expect;
var tools = require("../script");

describe("printName()",function(){
  it("should print the last name first",function(){
    var result = tools.printName({first:"Dhanuka",last:"Perera"});

    expect(result).to.equal("Perera,Dhanuka");

  });
});


After install, we going need to require in the file. And also we need the include the where our printName() function lives. Still, we haven't created the printName function. But ideally if I create a printName function this is how it should work with it. I should be able to send a object that repesent person. This person should have first name and last name. If I invoke this fucntion printName() I will expect it to return a string with my name. So this is where is going to use expect() fucntion. Now our test is ready. If we run our test now we are getting an error becuse still we haven't implemnt the printName() fucntion. So now lets go create a file call script.js insdide MochaTesting folder.

Inside the script.js file create a printName() fucnton

module.exports = {
   printName(){
  
   }
}

If we run our test, our test will get fail, becuse it don't returing anything.


Now lets go back agin to printName() fucntion and write the code that we need to pass.

module.exports = {
   printName(person){
     return `${person.last},${person.first}`;
   }
}

Now lets go and run our test again.

\
And we getting pass the test and results as expected.

Comments

Popular posts from this blog

Getting Started with Git & GittHub

Creating a Git Project Git is a version control system that keeps track of the files in a project. In order to use git, we start by identifying a folder as a git repository. we do it by running init command in the folder. Before running the git command go to your project folder and create two files called file1.txt and file2.txt and add some sample text to it and save the file. Now open up the git command line in the project folder and type the following command. An easy way to is by right clicking anywhere inside your project folder and click git bash here. $ git init Checking Status Now we created a git repository. Now we can check the status of our repository by typing git status in the git terminal. $ git status You can see there are two files in red which called as untracked files. This is because we still didn't add any file to our git repository. Adding files to Git repository To add files to your git repository by simply typing git add file1.t

Introduction to MEAN Stack

MEAN stands for Mongo, Express, Angular and Node. In the world of modern software, we typically deal with frontend and backend. MEAN Stack includes both technologies for both sides frontend and backend. Mongo Mongo is a cross-platform document-oriented database. Meaning is not like more traditional SQL database. Which is where NoSQL term comes from, with that means Mongo DB Store JSON-like documents with dynamic schemas making it better and faster for some applications.Companies with large scale deployments of Mongo DB includes Adobe, eBay, LinkedIn and much more. There is no database we can call as the best. It depends on the type of the system. Mongo DB is one in many options. Mongo DB is good for developers who is familiar with JavaScript and JSON and also easy to start learning. Express Express or ExpresJS is Node JS web application framework. It's the server framework for NodeJS. It allows quick development of robust web APIs by providing the thin and performa