Prior to the development of the project because of the improvement, the basic is extensive development. To improve code quality, unit testing is essential.
For RESTful APIs, test the framework with SuperTest. For Nodejs, the introduction of mocha and should makes it easy to write unit tests.
First talk about SuperTest, which encapsulates the mocha and expect modules. Usage is also relatively concise, examples:
varRequest = require (' SuperTest ');varExpress = require (' Express '); let should=require (' should '); varApp = require ('.. /app ');varPort = Process.env.PORT | | 3000; App.set (' Port ', Port);d Escribe (' SuperTest verification ',function() {Beforeeach (function() {Console.log (' Before every test in every suit '); }); It (' Get request ',function(done) {request (APP). Get (‘/‘). Set (' Cache-control ', ' No-cache '). Expect (200, done); }); It (' Post request ',function(done) {request (APP). Post ('/sayhello '). Set (' Content-type ', ' Application/json '). Send ({username:' Ryan ', Password:' Chen '}). Expect (' Content-type ',/json/). Expect (200,{message: "Ryan Hello"}, done); });});
As shown in the code above, the overall look is relatively easy and refreshing. You can set the header and compare the return value, as for token verification, the test is canceled according to the environment variable.
Should is a good comrade, can help us to compare conveniently. As for usage, the following:
1Describe (' Should test ',function () {2"Use Strict";3Beforeeach (function () {4 });5It (' OK ',function(done) {6 true. Should.be.ok;7(5). should.be.exactly (6). And.be.a.number;8 Done ();9 });TenIt (' true ',function(done) { One true. should.be.true; A' True '. should.not.be.true; - Done (); - }); the //Content Compare,not Reference -It (' EQL ',function () { -({foo: ' Bar ', num:1}). SHOULD.EQL ({foo: ' Bar ', num:1}); - //[1, 2, 3].should.eql ({' 0 ': 1, ' 1 ': 2, ' 2 ': 3}); + - }); + //to be exactly, AIt (' equal ',function () { at(4). Should.equal (4); -' Test '. Should.equal (' Test ')); -[1,2,3].should.be.not.exactly ([]); - }); - //>= or <= -It (' within ',function () { in(5). Should.be.within (5, ten). And.within (5, 5); -(5). Should.be.above (0); to }); + //. Infinity -It (' Infinity ',function () { the(1/0). should.be.Infinity; * }); $ //instanceofPanax NotoginsengIt (' instanceof ',function () { -Let ball=NewBall (one, "red"); the ball.should.be.an.instanceOf (ball); +[].should.be.an.instanceof(Array); A }); the +It (' Properties ',function () { -Let ball=NewBall (one, "red"); $ $Ball.should.have.properties (' Color '); -Ball.should.have.properties ({size:11,color: "Red"}); - }); theIt (' Empty ',function () { - [].should.be.empty;Wuyi‘‘. Should.be.empty; the ({}). should.be.empty; -(function() { Wu Arguments.should.be.empty; - })(); About }); $It (' CONTAINEQL ',function () { -' Hello Boy ', should.containeql (' Boy ')); -[1,2,3].should.containeql (3); -[[1],[2],[3]].should.containeql ([3]); A[[1],[2],[3, 4]].SHOULD.NOT.CONTAINEQL ([3]); + }); the //Regex -It (' regex or function ',function () { $[' A ', ' B ', ' C '].should.match (/[a-z]/); the(5). Should.match (function(n) {returnn > 0; }); the(5). Should.not.match (function(n) {returnN < 0; }); the }); theIt (' Match each ',function () { -[Ten, One, 12].should.matcheach (function(IT) {returnIt >= 10; }); in }); the theIt (' Exception ',function () { About(function(){ the Throw NewError (' Fail '); the}). Should.Throw(); the }); + -It (' status ',function () { the //Res.should.have.status ($);Bayi //res.should.have.header (' content-length ', ' 123 '); the //Res.should.be.json the - }) - the }); the the Class Ball the { - Constructor (Size,color) the { theObject.assign ( This, {size,color}); the }94}View Code
In the console, with the Mocha Command test, the project root is found by default in the test folder, so the unit test to be placed under this file. After execution, all unit tests under the current folder are tested:
Development tools If the use of webstorm, there is a more convenient plug-in. Can be found in the plug-in Nodejs, after installation, you can add mocha in the boot configuration,
After configuration, start directly
As you can see, you can choose one of these test case tests, which is much easier than typing the script. Bingo
Nodejs Unit Test