Developer-How to self-guarantee node. JS Module Quality

Source: Internet
Author: User
Tags benchmark

The group is doing SaaS products, some of which are node. JS implementations, where we use node. js to implement the Web server to provide services.

Before doing the SaaS project, the group development mode is the traditional Deverloper + QA mode, this is the traditional collaborative mode, developer is responsible for writing code development, of course, there will be a basic self-test, QA is responsible for testing, encountered problems, to mention the bug to developer to repair, Developer after fixing the bug, the QA will verify and record the bug. However, this collaboration model is not suitable for the development of SaaS products, SaaS product update Iteration fast, many modules, which requires the transformation of the model. Developer to be responsible for more tests of its own modules, unit testing to cover the whole. Only a small amount of QA will be responsible for integration testing.

  As a result, more module quality requirements developer to ensure.

Because I happen to be involved in the node. JS module, because I summarized some of the current node. JS module Development How to self-test.

It is recommended that you create a new folder in the project directory that is "test", storing the script and data for the test.

There are three main, as follows.

I. Fake data and write test scripts (script)

node. js does the server, typically relying on other modules to get data or services. If you do the module OK, but other people's module is often not necessarily completely OK. So the interface is very important. So you can write some scripts from the test, but also need other module data, you can make some false data, put in a JSON file, to invoke.

The script can be JS or bash script, etc., according to the actual.

With the script and data, you can test your own module, test the interface is OK, and so on.

Two. Benchmark

In node. js, there is a NPM module that is popular, called Benchmark, and its role is to perform performance analysis and testing. Its GitHub website is: https://github.com/bestiejs/benchmark.js.

Its usage scenarios, such as you have several ways to achieve the same function, but you do not know which performance is better. It can help you analyze it.

For example, the following is an official example.

1 varSuite =NewBenchmark.suite;2 3 //Add tests4Suite.add (' Regexp#test ',function() {5/o/.test (' Hello world! ');6 })7. Add (' String#indexof ',function() {8' Hello world! '. IndexOf (' O ') >-1;9 })Ten //Add listeners One. On (' Cycle ',function(event) { A Console.log (String (Event.target)); - }) -. On (' Complete ',function() { theConsole.log (' fastest is ' + This. Filter (' fastest '). Pluck (' name ')); - }) - //Run Async -. Run ({' async '):true }); +  - //logs: + //= regexp#test x 4,161,532 +-0.99% (cycles) A //= String#indexof x 6,139,623 +-1.00% (131 cycles) at //= fastest is String#indexof

In this test example, the analysis compares the use of regular expressions or indeof which methods have higher performance. Finally IndexOf wins.

Therefore, benchmark is recommended for use in the node. JS module for performance testing.

Three. Jshint-a JavaScript code analysis tool

This tool is often used, every time you write JavaScript code, you can use this tool to scan the code, if there are errors (error) or warning (Warning) hint, it can be used to find the code in the problem and can be optimized, can be modified in time.

At the same time Jshint provides various support for the text editor plug-in, like I used vim configured Jshint, each development, the Direct Input command: Jshint, very useful, recommended.

It is also recommended to add "user strict;" In the first line of the JavaScript code file, which is the default use of strict mode, more secure and more rigorous. For example, the following example.

1 /*  2*/3 "use strict";

Jshint Official website: http://jshint.com/

At present, according to their own experience, only summed up the above three points. If there is more practice and summary in the back, it will be updated in time here. Thank you

Kevin Song

2015-5-28

Developer-How to self-guarantee node. JS Module Quality

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.