1. Using the Mocha tool for unit testing
① first prepare the node environment
② installation MOCHA:NPM Install Mocha can also be installed globally with NPM install global mocha
③ Install Assertion Library: NPM Install Chai Address: http://chaijs.com/
④ Test Cases:
To edit a simple addition function
Add.js
functionreturn a+b; } module.exports = add;
Writing Test Unit files
Introducing the Add Module
var add = require ('./add ')
The expectvar expect = require (' Chai ') in the assertion Library Chai is introduced. Expect; describe (function() { It (function() { expect (add). To.be.equal (2); }) ;
where the describe () function is a test description that represents a set of related test cases for which module is tested. It () is a test case in which a test case (it block) can be executed inside a describe block.
The test script should include one or more describe blocks, and each describe block should include one or more it blocks.
How do I execute a test file?
Direct command:mocha add.test or mocha add.test.js
Results show:
Reference article: Test Framework Mocha Example Tutorial
Getting Started with JavaScript unit tests