Testing with Node.js
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Testing with Node.js

  • 8,619 views
Uploaded on

An introduction on testing Node.js code. ...

An introduction on testing Node.js code.

Covers frontend (UI) testing, backend unit tests and code coverage.
Also mentions how to use a Makefile to run frontend and backend tests at the same time.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
8,619
On Slideshare
8,601
From Embeds
18
Number of Embeds
6

Actions

Shares
Downloads
66
Comments
0
Likes
14

Embeds 18

http://www.linkedin.com 9
http://nodeslide.herokuapp.com 5
http://jolidev10-jolicloud.dotcloud.com 1
http://192.168.0.28 1
https://twitter.com 1
https://www.linkedin.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Testingwith 1
  • 2. Self introductionJonathan Waller 2
  • 3. ContentWhat is Node.js?Testing the codeChecking test coverageTesting the user interfacePulling it all together 3
  • 4. WHAT IS NODE.JS?4
  • 5. What is Node.js? example.jsvar  http  =  require(http);http.createServer(function  (request,  response)  {   response.writeHead(200,  {Content-­‐Type:  text/plain});   response.end(Hello  Worldn);}).listen(8080);console.log(Server  running  at  http://127.0.0.1:8080/); Running it$  node  example.jsServer  running  at  http://127.0.0.1:8080/ 5
  • 6. Node is non-blocking Blocking codevar  fileContents1  =  fs.readFileSync(file1.txt);console.log(fileContents1);var  fileContents2  =  fs.readFileSync(file2.txt);console.log(fileContents2); 0s 5s 10s 6
  • 7. Node is non-blocking Non-blocking codevar  callback  =  function(err,  fileContents){   console.log(fileContents);}fs.readFile(file1.txt,  callback);fs.readFile(file2.txt,  callback); 0s 5s 10s 7
  • 8. $  node  example.js 8
  • 9. TESTING THE CODE9
  • 10. Testing the code describe(file.js,  function()  {   describe(functionName,  function()  {     it(action,  function()  {       ... MOCHA expect(myObject.id).to.be(undefined); expect(myObject).to.eql({  a:  b  }) expect(myVariable1).to.be.a(number); expect(myVariable2).to.be.an(array); EXPECT.JS 10
  • 11. 11
  • 12. TESTING SYNCHRONOUS CODE 1var  fileContents1  =  fs.readFileSync(file1.txt);console.log(fileContents1); SYNC_EXAMPLE.JSvar  fileContents2  =  fs.readFileSync(file2.txt);console.log(fileContents2);function  readFile1(){   return  fs.readFileSync(file1.txt); SYNC_EXAMPLE.JS}function  readFile2(){   return  fs.readFileSync(file2.txt);} var  sync_example  =  require(‘./sync_example’);modules.exports.readFile1  =  readFile1;modules.exports.readFile2  =  readFile2; TEST/SYNC_EXAMPLE.JS 12
  • 13. TESTING SYNCHRONOUS CODE 2function  readFile1(){   return  fs.readFileSync(file1.txt); SYNC_EXAMPLE.JS}function  readFile2(){   return  fs.readFileSync(file2.txt);}modules.exports.readFile1  =  readFile1;modules.exports.readFile2  =  readFile2;var  sync_example  =  require(‘../sync_example’); TEST/SYNC_EXAMPLE.JSdescribe(sync_example.js,  function()  {   describe(readFile1,  function()  {     it(reads  the  content  of  the  file,  function()  {       var  fileContents1  =  sync_example.readFile1();       expect(fileContents1.length).to.be.greaterThan(0);     });   });   describe(readFile2,  function()  {     it(reads  the  content  of  the  file,  function()  {       var  fileContents2  =  sync_example.readFile2();       expect(fileContents2.length).to.be.greaterThan(0);     });   });}); 13
  • 14. TESTING ASYNCHRONOUS CODE 1var  callback  =  function(err,  fileContents){   console.log(fileContents); ASYNC_EXAMPLE.JS}fs.readFile(file1.txt,  callback);fs.readFile(file2.txt,  callback);function  readFile1(callback){   fs.readFile(file1.txt,  callback); ASYNC_EXAMPLE.JS}function  readFile2(callback){   fs.readFile(file2.txt,  callback);}modules.exports.readFile1  =  readFile1;modules.exports.readFile2  =  readFile2; 14
  • 15. TESTING ASYNCHRONOUS CODE 2function  readFile1(callback){   fs.readFile(file1.txt,  callback); ASYNC_EXAMPLE.JS}function  readFile2(callback){   fs.readFile(file2.txt,  callback);}modules.exports.readFile1  =  readFile1;modules.exports.readFile2  =  readFile2;describe(async_example.js,  function()  {   describe(readFile1,  function()  { TEST/ASYNC_EXAMPLE.JS     it(reads  the  content  of  the  file,  function(done)  {       async_example.readFile1(function(err,fileContents1){         expect(fileContents1.length).to.be.greaterThan(0);         done();       });     });   });   describe(readFile2,  function()  {     it(reads  the  content  of  the  file,  function(done)  {       async_example.readFile2(function(err,fileContents2){         expect(fileContents2.length).to.be.greaterThan(0);         done();       });     });   });}); 15
  • 16. TESTING ASYNCHRONOUS CODE 3async_example.readFile1(function(err,fileContents1){   expect(fileContents1.length).to.be.greaterThan(0);   done();}); =var  callback1  =  function(err,fileContents1){   expect(fileContents1.length).to.be.greaterThan(0);   done();}async_example.readFile1(callback1); 16
  • 17. $  mocha  test/example.js  -­‐r  expect.js 17
  • 18. JSCoverage CHECKING TEST COVERAGE 18
  • 19. 19
  • 20. Checking test coverage JSCoverage$  jscoverage  index.js json-cov 20
  • 21. Checking test coverage Instrumenting JS file$  jscoverage  index.js Running tests (Running the instrumented code)$  mocha  test/index.js  -­‐R  json-­‐cov  >  coverage.html 21
  • 22. SpookyJSTESTING THE USER INTERFACE 22
  • 23. UI testingSpookyJS is a SpookyJSscriptable web testingframework for Mocha CasperJSWrapper for CasperJsand PhantomJSUses WebKit, so WEBKITsupports client-sideJavascript 23
  • 24. var  Spooky  =  require(spooky);var  spooky  =  new  Spooky(   { SAMPLE SPOOKY SCRIPT     child:  {       port:  8080,       script:  ./lib/bootstrap.js,  //Loads  casperJS       spooky_lib:  ./node_modules     }   },  function  (err,  error,  response)  {         if  (err  ||  error)  {       var  e  =  new  Error(Failed  to  initialize  SpookyJS);       e.details  =  err  ||  error;       throw  e;     }       spooky.on(error,  function  (e)  {console.error(e);});     spooky.on(console,  function  (line)  {console.log(line);});     spooky.start();     spooky.then(function  (){       this.echo(Hello,  this  is  SpookyJS);     });     spooky.open(http://www.google.com/);     spooky.then(function  ()  {       this.echo(Now  viewing:    +  this.getCurrentUrl());     });     spooky.run();   }); 24
  • 25. var  util  =  require(util);var  expect  =  require(expect.js);describe("Test  that  SpookyJS  is  working",  function  ()  {   var  context  =  {}; TEST/FRONTEND.JS   var  hooks  =  require(../util/hooks);   before(hooks.before(context));   describe(Test  that  SpookyJS  can  navigate  to  Google,  function  ()  {     it(navigates  to  google.com,  and  returns  the  current  url,  function  (done)  {       context.spooky.start();       context.spooky.then(function  (){         this.echo(Hello,  this  is  SpookyJS);       });       context.spooky.open(http://www.google.com/);       context.spooky.then(function  ()  {         this.echo(this.getCurrentUrl());       });       function  onConsole(line)  {         if  (line  ===  http://www.google.com/)  {           context.spooky.removeListener(console,  onConsole);           done();           return;         }       }       context.spooky.on(console,  onConsole);       context.spooky.run();     });        });        after(hooks.after(context));}); 25
  • 26. $  mocha  test/frontend/example.js  -­‐r  expect.js 26
  • 27. $  make  test PULLING IT ALL TOGETHER 27
  • 28. Make: Folder structuretestbackend:   @mocha  $$(find  test/backend  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  spec...など 28
  • 29. Testing the backend Makefiletestbackend:   @./node_modules/.bin/mocha  $$(find  test/backend  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  spec Run tests $  make  testbackend 29
  • 30. Checking test coverage Makefilecoverage:   @echo  Checking  test  code  coverage...   #Cleaning  up   @rm  -­‐rf  _src-­‐with-­‐coverage/  &&  rm  -­‐rf  _test-­‐with-­‐coverage/     #Instrumenting  code   @./node_modules/jscoverage/jscoverage  src  _src-­‐with-­‐coverage   #Creating  tests  for  instrumented  code   @cp  -­‐r  test  _test-­‐with-­‐coverage   @find  _test-­‐with-­‐coverage  -­‐name  *.js  -­‐exec  sed  -­‐i    s//src///_src-­‐with-­‐coverage//g  "{}"  ;   #Running  tests...   @./node_modules/.bin/mocha  $$(find  _test-­‐with-­‐coverage  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  html-­‐cov  >  coverage.html   #Cleaning  up   @rm  -­‐rf  _src-­‐with-­‐coverage/  &&  rm  -­‐rf  _test-­‐with-­‐coverage/   @echo  Done.  Result  written  to  coverage.html. Check test coverage $  make  coverage 30
  • 31. 31
  • 32. Testing the frontend Makefiletest:   @./node_modules/.bin/mocha  $$(find  test/frontend  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  spec Run tests $  make  testfrontend 32
  • 33. Makefiletestbackend:   @./node_modules/.bin/mocha  $$(find  test/backend  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  spec  testfrontend:   @./node_modules/.bin/mocha  $$(find  test/frontend  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  spec  coverage:   @echo  Checking  test  code  coverage...   ...   @echo  Done.  Result  written  to  coverage.html.test:   @./node_modules/.bin/mocha  $$(find  test/  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  spec  all:  test  coverage   @echo  Tested  frontend  and  backend.  Coverage  doc  saved  to  coverage.html. Run all tests$  make  test Run all tests + show test coverage$  make  all 33
  • 34. SummaryNode.js is Javascript on the server.Unit testing with Mocha + Expect.jsChecking coverage with JSCoverageUser interface testing with SpookyJSRunning everything with Make 34
  • 35. Thank youありがとうございました 35
  • 36. Contact Jonathan@jonwallerfacebook.com/jonwaller0gplus.to/jonwaller www.jonwaller.net/ja/ 36
  • 37. ReferencesNode.jshttp://nodejs.org/Unit testing (Mocha)http://visionmedia.github.com/mocha/Test coveragehttp://tjholowaychuk.com/post/18175682663/mocha-test-coverageUser interface testinghttp://casperjs.org/https://github.com/WaterfallEngineering/SpookyJSOther useful stuffhttps://npmjs.org/ - Learn about commonly used node packageshttp://jenkins-ci.org/ - Set up continuous integration (e.g. Automatically testing when you commit) 37