Unit testing in front-end development (1)-mocha

Source: Internet
Author: User

There are many front-end test frameworks based on Nodejs, and finally the Mocha is selected. Mocha You may not have heard, but its development of-TJ, do the front-end students must have heard. Talk less, cook

  1. Because Mocha is based on Nodejs, the first choice we want to install NODEJS environment, will not students please Baidu, Google (see this, is not in the heart of the impulse, spontaneously)
  2. Installing the mocha is actually a Nodejs module. More than one "-G" parameter, in order to use the aspect will mocha install the system environment variable, any directory can be used.
    Install - G Mocha
  3. Create a new test catalog Mocha, store the files needed for this demo
  4. Install the assertion module for unit tests, and choose TJ's should assertion module. This module will not be installed in the system environment variable, will be in the current directory to produce a node_modules directory, the current project to hold all the modules used
    Install should
  5. Create a new unit test case file. Add a new one in the current directoryTestDirectoryMocha All test cases are executed by default in the test directory), and then create a newTest.jsThe file that holds the unit test case code
    1 varassert = require ("should");2 varFS = require ("FS");3 4Describe (' Sync test ',function() {5It (' array testing ',function() {6Assert.equal ( -1, [1, 2, 3].indexof (5));7Assert.equal ( -1, [1, 2, 3].indexof (0));8     })9 TenIt (' string legality test ',function() { OneAssert.equal ("123", "123"); A     }) - }) -  the  -Describe (' Async test ',function() { -It (' Read files asynchronously ',function(done) { -         //index.js Please add it yourself, test to read the contents and compare +Fs.readfile ("Index.js", "UTF8",function(err, data) { -DATA.SHOULD.EQL ("Test"); + Done (); A         }) at     }) -})
  6. Now that the preparation is done, let's run our own test case and enter mocha at the command line. The input results are as follows, 3 test case, all through

7. If the test case is modified, the error is changed. The pass and error points of the test case are clearly displayed.


Please refer to the following information for more details, such as handling, advanced features and some individual configurations.

will continue to write a comprehensive use of sharing


    1. Mocha's homepage: http://mochajs.org/
    2. Should:https://github.com/shouldjs/should.js

Unit testing in front-end development (1)-mocha

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.