Quick Learning - Mocha & Chai

Quick Learning - Mocha & Chai

Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

Mocha can be used with most JavaScript assertion libraries, including

  • chai
  • should.js
  • express.js
  • better-assert
  • unexpected

Mocha Installation

npm install -g mocha

By installing mocha globally you can access it at any project like a command line tool using the 'mocha' keyword. After mocha is successfully installed its just a matter of writing tests and executing it.

package.json

All npm packages contain a file, usually in the project root, called package.json - this file holds various metadata relevant to the project. This file is used to give information to npm that allows it to identify the project as well as handle the project's dependencies. For a new npm project, you can create package.json by running below command

npm init

This will initiate a command line questionnaire that will conclude with the creation of a package.json in the directory in which you initiated the command.

Test Setup

Create a project for Mocha testing. Create a test.js file. Initialize your project by running 'npm init' command. Answer the questions and hit enter. For the question ‘test command:’ respond with ‘mocha'. Your package.json will be created and you can verify the same.

No alt text provided for this image



Verify the package.json. You can see that in scripts.test the value will be mocha



Mocha Test Structure

A mocha test js file contains below sections.

  • Test groups - describe()
  • Test sub-groups - describe()
  • Test Case - it()
  • Assertion - assert

describe() is simply a way to group our tests in Mocha. We can nest our tests in groups as deep as we deem necessary. describe() takes two arguments, the first is the name of the test group, and the second is a callback function. it() is used for an individual test case. it() should be written as if you were saying it out loud: “It should equal zero”, “It should log the user in”, etc. it() takes two arguments, a string explaining what the test should do, and a callback function which contains our actual test.

example

describe('Create thing API call'function(){
    it('should throw 400 error'function(){
    });
});

Test Sub groups are meant to categorise sub-groups inside main test group. From an API testing aspect as in above example, positive test cases and negative test cases can be considered as two different sub-groups. A sub group can be called by calling a describe() function within a test group. See the example below.

var assert = require('assert');
  describe('Test group A'function() //Test Group with a sub-group
     describe('Test Subgroup A1'function() // Test Sub-Group
        it('1+1 should be 2'function() // Test case
          assert.equal(1+1, 2);
        });
        it('2+2 should be 4',function() //Test case
          assert.equal(2+2, 4);
        });
     });
     describe('Test Subgroup A2'function() {
        let maxNum = 60;
        it('MaxNum greater than 50'function() {
          assert.equal(maxNum>50true);
        });
     });
 
  });
  describe('Test group B'function() // Test Group without a sub-group
    it('true == 3>2'function() // Test case
    assert.equal(true,3>2);
    });
  });

Test can be executed by running 'npm test' command from the project folder.

No alt text provided for this image






Chai

Chai is a BDD / TDD assertion library for node and the browser that can be delightfully paired with any javascript testing framework. Chai can be paired with any javascript testing framework. Assertion with Chai provides natural language assertions, expressive and readable style.

Run below commands to install chai.

npm install --global chai

npm install --save-dev chai-as-promise

Assertion interfaces and styles

  • There are two popular way of assertion in Chai, expect and should
  • The expect interface provides function for assertion.
  • The should interface extends each object with a should property for assertion.
  • should property gets added to the Object.Prototype, so that all object can access it through prototype chain.

The code using simple Mocha assertions can be converted into chai assertions as below.

describe('Test group A'function() //Test Group with a sub-group
     describe('Test Subgroup A1'function() // Test Sub-Group
        it('1+1 should be 2'function done() // Test case
          expect(1+1).to.be.equal(2);//Asserion using chai expect interface
 
        });
    done();
        it('2+2 should be 4',function() //Test case
          expect(2+2).to.be.equal(4);//Asserion using chai should interface
        });
 });
 
  });

Get to know more about MochaJs from https://github.com/mochajs/mocha

要查看或添加评论,请登录

社区洞察

其他会员也浏览了