Testingwith          1
Self introductionJonathan Waller                  2
ContentWhat is Node.js?Testing the codeChecking test coverageTesting the user interfacePulling it all together            ...
WHAT IS NODE.JS?4
What is Node.js?    example.jsvar	  http	  =	  require(http);http.createServer(function	  (request,	  response)	  {	   res...
Node is non-blocking          Blocking codevar	  fileContents1	  =	  fs.readFileSync(file1.txt);console.log(fileContents1)...
Node is non-blocking           Non-blocking codevar	  callback	  =	  function(err,	  fileContents){	      console.log(file...
$	  node	  example.js                8
TESTING THE CODE9
Testing the code                   describe(file.js,	  function()	  {                   	   describe(functionName,	  funct...
11
TESTING SYNCHRONOUS CODE 1var	  fileContents1	  =	  fs.readFileSync(file1.txt);console.log(fileContents1);                ...
TESTING SYNCHRONOUS CODE 2function	  readFile1(){	   return	  fs.readFileSync(file1.txt);                                 ...
TESTING ASYNCHRONOUS CODE 1var	  callback	  =	  function(err,	  fileContents){	      console.log(fileContents);           ...
TESTING ASYNCHRONOUS CODE 2function	  readFile1(callback){	   fs.readFile(file1.txt,	  callback);                         ...
TESTING ASYNCHRONOUS CODE 3async_example.readFile1(function(err,fileContents1){	   expect(fileContents1.length).to.be.grea...
$	  mocha	  test/example.js	  -­‐r	  expect.js                             17
JSCoverage CHECKING TEST COVERAGE           18
19
Checking test coverage JSCoverage$	  jscoverage	  index.js                                   json-cov                     ...
Checking test coverage             Instrumenting JS file$	  jscoverage	  index.js             Running tests (Running the in...
SpookyJSTESTING THE USER INTERFACE                             22
UI testingSpookyJS is a                  SpookyJSscriptable web testingframework for Mocha           CasperJSWrapper for C...
var	  Spooky	  =	  require(spooky);var	  spooky	  =	  new	  Spooky(	      {                                               ...
var	  util	  =	  require(util);var	  expect	  =	  require(expect.js);describe("Test	  that	  SpookyJS	  is	  working",	  f...
$	  mocha	  test/frontend/example.js	  -­‐r	  expect.js                              26
$	  make	  test  PULLING IT ALL TOGETHER            27
Make: Folder structuretestbackend:	   @mocha	  $$(find	  test/backend	  -­‐name	  "*.js")	  -­‐r	  expect.js	  -­‐R	  spec...
Testing the backend              Makefiletestbackend:	   @./node_modules/.bin/mocha	  $$(find	  test/backend	  -­‐name	  "...
Checking test coverage                      Makefilecoverage:	    @echo	  Checking	  test	  code	  coverage...	         #C...
31
Testing the frontend              Makefiletest:	   @./node_modules/.bin/mocha	  $$(find	  test/frontend	  -­‐name	  "*.js"...
Makefiletestbackend:	   @./node_modules/.bin/mocha	  $$(find	  test/backend	  -­‐name	  "*.js")	  -­‐r	  expect.js	  -­‐R	...
SummaryNode.js is Javascript on the server.Unit testing with Mocha + Expect.jsChecking coverage with JSCoverageUser interf...
Thank youありがとうございました     35
Contact Jonathan@jonwallerfacebook.com/jonwaller0gplus.to/jonwaller                          www.jonwaller.net/ja/        ...
ReferencesNode.jshttp://nodejs.org/Unit testing (Mocha)http://visionmedia.github.com/mocha/Test coveragehttp://tjholowaych...
Upcoming SlideShare
Loading in...5
×

Testing with Node.js

8,862

Published on

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.

Published in: Technology
0 Comments
16 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,862
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
87
Comments
0
Likes
16
Embeds 0
No embeds

No notes for slide

Testing with Node.js

  1. 1. Testingwith 1
  2. 2. Self introductionJonathan Waller 2
  3. 3. ContentWhat is Node.js?Testing the codeChecking test coverageTesting the user interfacePulling it all together 3
  4. 4. WHAT IS NODE.JS?4
  5. 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. 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. 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. 8. $  node  example.js 8
  9. 9. TESTING THE CODE9
  10. 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. 11
  12. 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. 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. 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. 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. 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. 17. $  mocha  test/example.js  -­‐r  expect.js 17
  18. 18. JSCoverage CHECKING TEST COVERAGE 18
  19. 19. 19
  20. 20. Checking test coverage JSCoverage$  jscoverage  index.js json-cov 20
  21. 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. 22. SpookyJSTESTING THE USER INTERFACE 22
  23. 23. UI testingSpookyJS is a SpookyJSscriptable web testingframework for Mocha CasperJSWrapper for CasperJsand PhantomJSUses WebKit, so WEBKITsupports client-sideJavascript 23
  24. 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. 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. 26. $  mocha  test/frontend/example.js  -­‐r  expect.js 26
  27. 27. $  make  test PULLING IT ALL TOGETHER 27
  28. 28. Make: Folder structuretestbackend:   @mocha  $$(find  test/backend  -­‐name  "*.js")  -­‐r  expect.js  -­‐R  spec...など 28
  29. 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. 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. 31
  32. 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. 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. 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. 35. Thank youありがとうございました 35
  36. 36. Contact Jonathan@jonwallerfacebook.com/jonwaller0gplus.to/jonwaller www.jonwaller.net/ja/ 36
  37. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×