# Tools for Promises Unit Testing

Venkat.R
10.9K views

## Basic Promise Code

Lets have a look at the below code. It has very simple function called basic and returns the promise data after 1.6 seconds (Assume that its retrieved from Database after 1.6 seconds).

Basic Promise Code
function basic (result) {
if (!result) {
return Promise.reject('Failure');
}
return new Promise((resolve, reject) => {
// Assume that this below data is retrieved from Database which takes 1.6 seconds.
setTimeout(() => resolve(result), 1600);
});
}
module.exports = {
basic,
};
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

### Unit Testing Basics

As explained in the previous step, Adding Mocha and Chai for this basic Promise Code. In order to write basic unit testing in mocha, understanding this below two methods is must.

• describe - This function helps to groups selected test cases. so its easy to refer and maintain.
• it - This function helps to write your use case and test your library / module and expect the result.

#### Mocha Hooks

There is few more functions which are considered as Hooks. below are the four important hooks where this can be place inside describe function before any it function.

• before - runs before all tests in this block.
• beforeEach - runs before each test in this block.
• afterEach - runs after each test in this block.
• after - runs after all tests in this block.

#### Unit Testing

In this Unit Testing example, We have main group Basic Testing and three sub groups Basic, Error Case, Success Case and each sub groups has one test case. Scroll this Code below to get basic understanding.

Basic Promise Unit Testing
const mocha = require('mocha');
const expect = require('chai').expect;
const basicFile = require('./basic');
describe('Basic Testing', () => {
const noop = () => {};
describe('Basic', () => {
it('should have method basic', () => {
expect(basicFile).to.be.an('object');
expect(typeof basicFile.basic).to.equal('function');
});
// You can add more basic Test Cases.
});
describe('Error Case', () => {
it('should return Error message for empty param', () => {
return basicFile.basic().then(noop, (errorMessage) => {
expect('Failure').to.equal(errorMessage);
});
});
// You can add more Error Cases.
});
describe('Success Case', () => {
it('should return param with Hello for valid param', () => {
return basicFile.basic('Success').then((successMessage) => {
expect('Success').to.equal(successMessage);
}, noop);
});
// You can add more Success Cases.
});
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If you are still not clear, then have a look at below explaination about each test cases.

• Basic - We are testing whether basic is a function.
• Error Case - basic should return "Failure", when there is no parameter passed to the function.
• Success Case - basic should return given parameter as a result for success scenario.

Cool. If you noticed the Success Case and Error Case Scenario, We have used return keyword for Promise method, so that it can wait and complete the operation. Its suggested to return instead of done() for these types of test cases.

Lets see different example where it included external file which is Database library.

Open Source Your Knowledge: become a Contributor and help others learn.