Initial introduction to JavaScript unit testing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.