Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for...
Transcript of Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for...
![Page 1: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/1.jpg)
Best Practices:Testing Node.js for Stability
and Project SuccessWalter Scarborough
SEA 2014
![Page 2: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/2.jpg)
Congratulations!You've decided to build a node.js webservice/webapp!
![Page 3: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/3.jpg)
OR
![Page 4: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/4.jpg)
Congratulations!Your latest and greatest node.js webservice/webapp works!
Image from http://www.pxleyes.com/photoshop-picture/4a3be022a6a4b/Remote.html
![Page 5: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/5.jpg)
...but how long will it keep working as you update it?
![Page 6: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/6.jpg)
Don't leave things to chance!Set your project up for success at any stage.
Set up a good devenvironmentWrite testable codeWrite tests
![Page 7: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/7.jpg)
Technical HurdlesJavascript presents some unique language issues.How do we test network code?
Project HurdlesHow do we test backend services that are still underdevelopment?How do we organize all of this anyway?
![Page 8: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/8.jpg)
Tools of the tradeTesting
Linting - Promise Library - Testing Framework - Assertion Library - or
Network Mocks -
JSHintQ.js
Mocha.jsShould.js
Chai.jsNock.js
![Page 9: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/9.jpg)
"JavaScript is a language with more than its share of badparts."
- Douglas Crockford, JavaScript: The Good Parts
Common Mistakes:
not using vartype coercionif without parens (blocklessstatements)evaltyped wrappers
Full list: http://www.jslint.com/lint.html
![Page 10: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/10.jpg)
Solution: Javascript linting programs"Warning: JSLint will hurt your feelings."
- JSLint homepage http://www.jslint.com/lint.html
Image fromhttps://www.flickr.com/photos/pasukaru76/9824401426/in/photostream/
![Page 11: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/11.jpg)
The first step in testing is to write testable code.
Testable code isModularReadable
Some good references on the topic:
Code Complete by Steve McConnellTest Driven Development by KentBeck
![Page 12: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/12.jpg)
Node.js uses callbacks.
Too many callbacks can make code difficult to test.
![Page 13: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/13.jpg)
Nested callbacks often lead to this pyramid:Controller.createUser = function(request, response) { service.checkUsername(request.username, function(error, results) { if (error) { responseController.sendError(error.message, response); } else { service.createUser(request.username, function(error, newUser) { if (error) { responseController.sendError(error.message, response); } else { service.createProfile(newUser, function(error, profile) { if (error) { responseController.sendError(error.message, response); } else { // ... } }); } }); } });}; It's difficult to read, maintain and test.
![Page 14: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/14.jpg)
Javascript promises can make life a lot simpler:Controller.createUser = function(request, response) { service.checkUsername(request.username) .then(function()) return service.createUser(request.username); }) .then(function(newUser) { return service.createProfile(newUser) }) .then(function(profile) { // ... }) .fail(function(error) { apiResponseController.sendError(error.message, response); });};
The Q library is a great choice for node.js:http://documentup.com/kriskowal/q/
![Page 15: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/15.jpg)
is a lightweight javascript testing frameworkMochaIt can be run on individual files or an entire projectIt can be run from npm if a make file is set up and specified inpackage.jsonIt can use a variety of assertion libraries
describe("donut model", function() {
it("should contain chocolate donuts", function() {
var donut = new Donut(); donut.flavor.should.equal('chocolate');
});
![Page 16: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/16.jpg)
Nock.js"Nock is an HTTP mocking and expectations library for Node.js"
- https://github.com/pgte/nockvar nock = require('nock');
nock('https://www.donut.com') .get('/flavors') .reply(200, JSON.stringify(["chocolate","vanilla"]));
![Page 17: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/17.jpg)
Let's take this one step further with reusable mocks anddata fixtures.
DataFixture.flavors = ['chocolate', 'vanilla'];
DonutMocks.getFlavors = function(nock) { nock('https://www.donut.com') .get('/flavors') .reply(200, JSON.stringify(dataFixture.flavors)) ;
return nock;};
var nock = require('nock');
describe("donut model", function() {
it("should fetch flavors", function() {
var donut = new Donut(); donutMocks.getFlavors(nock);
donut.getFlavors() .then(function(flavors) { flavors[0].should.equal('chocolate'); }); });
![Page 18: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/18.jpg)
Project Test OrganizationmyProject/tests - Project testsmyProject/tests/fixtures - Common requests/responses for yourmocksmyProject/tests/mocks - Reusable networking mock objects
![Page 19: Best Practices: Testing Node.js for Stability and Project ......Best Practices: Testing Node.js for Stability and Project Success Walter Scarborough SEA 2014](https://reader030.fdocuments.in/reader030/viewer/2022041018/5ecc84c861c31c4569701afc/html5/thumbnails/19.jpg)
Solution ReviewLint your codeIn general, use promises instead of callbacksWrite testable codeUse mock networking objectsOrganize common requests/responses into datafixturesWrite tests