Mocha is a testing framework which is used for writing unit tests for Node Js application. Testing is one of the most important parts of any application development. There are many packages available for implementing testing in a node js application. One of the most common frameworks is Mocha. So, here I am going to show you how you can get started with testing a node js application.
So, let’s start by creating a simple node js application.
Step 1: Start a command prompt and create a new directory: mkdir nodejsapp
Step 2: Navigate to the folder: cd nodejsapp
Step 3: Run the command in command line: node init which creates a package.json file with default settings.
Step 4: Use Command Line to run: npm install mocha –save-dev
Step 5: Copy and paste the below code in your application:
mocha.js
module.exports.add = (x,y) =>{ return x+y-1; }
Step 6: Create a folder and create a file inside the folder with name as app.test.js
mocha.test.js
var mochaexample = require('./mochaexample'); it('should be sum of numbers',() => { var res = mochaexample.add(2,3); if(res !== 5){ throw new Error(`Result should be 5 but it is ${res}.`); } console.log("Test case passed.") })
Step 7: The final step is to add the mocha code into the script test section:
mocha ***/*.test.js
Once you have added the code into your project you can use the below command to start testing the application:
npm test
Below is a very simple way which we can use to auto restart our test cases. For achieving this we need to have nodemon package installed globally on our PC. Below is the npm command that can be used to install the nodemon package:
npm install nodemon
After installing the nodemon npm package replace the script for test with the script given below:
"scripts": { "test": "mocha ***/*.test.js", "test-watch": "ndeomon --exec \"npm test\"" }
Now to auto restart the tests you can use the below command:
npm run test-watch
Expect is an npm package that can be used with Mocha for writing the unit test cases in NodeJS. Here I am going to demonstrate how to expect can be used with Mocha for unit testing in the NodeJS application.
First of all you need to install the expect framework using the below command:
npm install --save-dev expect
After installing the package using the npm, you can replace the code as below
require('./mochaexample'); it('should be sum of numbers',() => { var res = mochaexample.add(2,3); //if(res !== 5){ // throw new Error(`Result should be 5 but it is ${res}.`); //} expect(res).toBe(5).toBeA('number'); })
One of the most common questions asked in the interviews is what is the difference between a singleton pattern and a static class. Given below are some of the points that show us the difference between the two:
Loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Static Class cannot have a constructor.
You cannot create the instance of a static class.
We cannot pass the static class to a method.
We cannot inherit a Static class in C#.
Singleton instance is created for the first time when the user requested.
Singleton class can have a constructor.
You can create one instance of the object and reuse it.
You can create the object of singleton class and pass it to a method.
Singleton class does not say any restriction of Inheritance.
We can dispose the objects of a singleton class but not of static class.
One of the most common questions that are being asked in Interview question is the difference between an Abstract Class and an Interface. So, let’s start by understanding what is an abstract class and an interface. So, an abstract class is a class that cannot be instantiated. So what exactly is the purpose or use of the Abstract class? The purpose of the abstract class is to be used as a base class. Lets say that we want to have some consistency in our appliction, then we can create an abstract class with abstract methords which will force the team members to implement the abstract class with same name. Also, it helps us in implementing the OOPS principle of Inheritance.
So, lets understand by taking an example as to how we can use the abstract class in our application.
Sign up for our newsletter to receive the latest news and event postings.
Recent Comments