SlideShare a Scribd company logo
NodeJs
testing tools and continuous integration
Davide Fiorello
Mocha
• Javascript Test Framework
• Run in Node.js and Browser
environment
• BDD, TDD, QUnit
• Synchronous and Asynchronous
Mocha - Interfaces
• BDD
1 describe('My first test', function(){
2 it('pass it!!');
3 });
• TDD
1 suite('Array', function(){
2 setup(function(){
3 });
4
5 suite('#indexOf()', function(){
6 test('should return -1 when not present', function(){
7 // ASSERT
8 });
9 });
10 });
Mocha - Sync, Async,
Pending
1 describe('Let's test..', function() {
2 it('Test something sync', function() {
3 // ASSERT
4 });
5
6 it('Test something async', function(done) {
7 user.save(function(err){
8 if (err) throw err;
9 // ASSERT
10 done();
11 });
12 });
13
14 it('Pending test');
15 });
Mocha - Before, After, ...
1 describe('Let's test..', function() {
2 before(function(){
3 // ...
4 });
5
6 beforeEach(function(done){
7 // ... -> done()
8 });
9
10 after(function(){
11 // ...
12 });
13
14 afterEach(function(){
15 // ...
16 });
17 });
Mocha - Only, skip
1 describe('A test', function() {
2 it.only('Test only this', function(done) {
3 // ...
4 });
5 });
6
7 describe('Another test', function() {
8 it.skip('Don't test this', function(done) {
9 // ...
10 });
11 });
Mocha - Has not...
• Assert (should.js, chai, expect.js)
• Spy/Mock/Stub (sinon)
• Code Coverage (istanbul, node-
jscoverage)
Should.js
should is an expressive, readable, test
framework agnostic, assertion library.
Main goals of this library to be expressive
and to be helpful.
It keeps your test code clean, and your
error messages helpful.
Should.js
1 var should = require('should');
2
3 var user = { name: 'davide', pets: ['tobi', 'loki']};
4
5 user.should.have.property('name', 'davide');
6 user.should.have.property('pets').with.lengthOf(2);
7
8 should(user).have.property('name', 'davide');
9 should(true).ok;
10
11 should.exist(user);
It extends the Object.prototype with a single non-
enumerable getter that allows you to express how that
object should behave, also it returns itself when required
with require.
Should.js - Chaining
assertions
.ok, .true, .false, .eql, .equal, .startWith, .endWith,
.within, .above, .belove, .instanceOf, .Array, .Object,
.String, .Error, .property, .length, .keys, .match, .throw,
.throwError, .json, .html
1 var should = require('should');
2
3 var user = { name: 'davide', age : 22, pets : ['pit',
'fuffy']};
4
5 user.name.should.match(/dav/);
6 user.age.should.be.above(18);
7 user.pets.should.be.Array;
8 user.pets.should.containEql('pit');
9 user.pets.should.not.containEql('mark');
Should.js - Chaining
assertions
1 var should = require('should');
2
3 var user = { name: 'davide', age : 22};
4
5 user.age.should.be.greaterThan(18).and.be.lessThan(33);
6
7 user.age.should.greaterThan(18).lessThan(33);
.an, .of, .a,.and, .be, .have, .with, .is, .which
Use them for better readability; they do nothing at all
Sinon.js
Standalone test spies, stubs and mocks for
JavaScript.No dependencies, works with any unit testing
framework.
Sinon.js
• Spies
• Stubs
• Mocks
• Fake timers
• Fake XHR
• Fake server
• Sandboxing
• Assertions
• Matchers
Sinon.js - spies
1 var sinon = require('sinon');
2 var should = require('should');
3
4 function myFunc(value) {
5 myObject.check(value);
6 }
7
8 var myObject = {
9 check : function(value) {
10 // DO Something
11 }
12 };
13 var spy = sinon.spy(myObject, "check");
14 myFunc('test');
15 spy.called.should.be.true;
Sinon.js - spies (API)
.withArgs, .callCount, .called, .calledOnce,
.firstCall, .lastCall, .calledBefore, .calledAfter,
.calledWith, .alwaysCalledWith,
.neverCalledWith, .threw, .returned, .args,
.returnValues
Sinon.js - stubs
1 var sinon = require('sinon');
2 var should = require('should');
3
4 function myFunc() {
5 return myObject.check();
6 }
7
8 var myObject = {
9 check : function() {
10 return 10;
11 // DO Something
12 }
13 };
14 var stub = sinon.stub(myObject, "check");
15 stub.onFirstCall().returns(1)
16 .onSecondCall().returns(2);
17
18 myFunc('test').should.be.equal(1);
19 myFunc('test').should.be.equal(2);
Sinon.js - Stub (API)
.withArgs, .onCall, .onFirstCall,
.onSecondCall, .returns, .returnsThis,
.returnsArg, .throws, .callsArg, .yelds,
.callArgWith, .callArgAsync
Useful Modules
• node-mocks-http
• node-rest-client
• pow-mongodb-fixtures
• readyness
Continuous Integration
• drone.io
• travis-ci.io
• codeship.io
Thanks!!
Davide Fiorello
davide@codeflyer.com
github.com/codeflyer

More Related Content

PDF
To Err Is Human
Alex Liu
 
PDF
node.js Module Development
Jay Harris
 
PDF
Adventures In JavaScript Testing
Thomas Fuchs
 
PDF
Metis - RubyConf 2011 Lightning Talk
Ken Robertson
 
PDF
G*におけるソフトウェアテスト・シーズンIII
Takuma Watabiki
 
DOCX
Service Functions
pineda2
 
DOCX
Batch processing Demo
Peeyush Ranjan
 
ODP
Circuit breakers for Java: Failsafe, Javaslang-Circuitbreaker, Hystrix and Ve...
Micha Kops
 
To Err Is Human
Alex Liu
 
node.js Module Development
Jay Harris
 
Adventures In JavaScript Testing
Thomas Fuchs
 
Metis - RubyConf 2011 Lightning Talk
Ken Robertson
 
G*におけるソフトウェアテスト・シーズンIII
Takuma Watabiki
 
Service Functions
pineda2
 
Batch processing Demo
Peeyush Ranjan
 
Circuit breakers for Java: Failsafe, Javaslang-Circuitbreaker, Hystrix and Ve...
Micha Kops
 

What's hot (20)

PDF
Intro to Sail.js
Nicholas McClay
 
KEY
10 Catalyst Tips
Jay Shirley
 
PPTX
hacking with node.JS
Harsha Vashisht
 
PDF
Real World Mocking In Swift
Veronica Lillie
 
KEY
Node.js 0.8 features
Nicholas McClay
 
PDF
How we're using Firebase at Boiler Room
Lukas Klein
 
PPTX
Linux kernel debugging
JungMinSEO5
 
PDF
Live Updating Swift Code
Bartosz Polaczyk
 
PDF
Practical JavaScript Programming - Session 6/8
Wilson Su
 
PDF
Synchronisation de périphériques avec Javascript et PouchDB
Frank Rousseau
 
PDF
C++ Programming - 9th Study
Chris Ohk
 
PDF
C++ Programming - 12th Study
Chris Ohk
 
PDF
DEF CON 23 - amit ashbel and maty siman - game of hacks
Felipe Prado
 
PDF
rsyslog v8: more than just syslog!
Yury Bushmelev
 
PDF
PL/SQL Unit Testing Can Be Fun
Raimonds Simanovskis
 
PDF
Rich and Snappy Apps (No Scaling Required)
Thomas Fuchs
 
PDF
Modern Getopt for Command Line Processing in Perl
Nova Patch
 
PDF
Ruby tricks2
Michał Łomnicki
 
PDF
Extreme JavaScript Performance
Thomas Fuchs
 
PDF
Node.js - A Quick Tour
Felix Geisendörfer
 
Intro to Sail.js
Nicholas McClay
 
10 Catalyst Tips
Jay Shirley
 
hacking with node.JS
Harsha Vashisht
 
Real World Mocking In Swift
Veronica Lillie
 
Node.js 0.8 features
Nicholas McClay
 
How we're using Firebase at Boiler Room
Lukas Klein
 
Linux kernel debugging
JungMinSEO5
 
Live Updating Swift Code
Bartosz Polaczyk
 
Practical JavaScript Programming - Session 6/8
Wilson Su
 
Synchronisation de périphériques avec Javascript et PouchDB
Frank Rousseau
 
C++ Programming - 9th Study
Chris Ohk
 
C++ Programming - 12th Study
Chris Ohk
 
DEF CON 23 - amit ashbel and maty siman - game of hacks
Felipe Prado
 
rsyslog v8: more than just syslog!
Yury Bushmelev
 
PL/SQL Unit Testing Can Be Fun
Raimonds Simanovskis
 
Rich and Snappy Apps (No Scaling Required)
Thomas Fuchs
 
Modern Getopt for Command Line Processing in Perl
Nova Patch
 
Ruby tricks2
Michał Łomnicki
 
Extreme JavaScript Performance
Thomas Fuchs
 
Node.js - A Quick Tour
Felix Geisendörfer
 
Ad

Similar to Test innode (20)

PPTX
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
mlilley
 
PDF
Test driven node.js
Jay Harris
 
PDF
Developers Testing - Girl Code at bloomon
Ineke Scheffers
 
PPTX
Qunit Java script Un
akanksha arora
 
PDF
Unit Testing JavaScript Applications
Ynon Perek
 
PDF
Unit Testing - The Whys, Whens and Hows
atesgoral
 
PPTX
unit test in node js - test cases in node
Goa App
 
PDF
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
PPT
JavaScript Testing: Mocha + Chai
James Cryer
 
PDF
Unit tests in node.js
Rotem Tamir
 
PPTX
Tests in Javascript using Jasmine and Testacular
Paulo Cesar Ortins Brito
 
ODP
Unit Testing With Javascript
thedumbterminal
 
PDF
JavaScript Unit Testing with Jasmine
Raimonds Simanovskis
 
PDF
TDD for Javascript developers
Andrey Kucherenko
 
PDF
mocha sinon chai Dc jquery 4-24
Carson Banov
 
PPTX
Testing nodejs apps
felipefsilva
 
PPTX
How do I write Testable Javascript so I can Test my CF API on Server and Client
Gavin Pickin
 
PDF
Unit testing JavaScript using Mocha and Node
Josh Mock
 
PDF
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
PPTX
JS Frameworks Day April,26 of 2014
DA-14
 
Testing NodeJS with Mocha, Should, Sinon, and JSCoverage
mlilley
 
Test driven node.js
Jay Harris
 
Developers Testing - Girl Code at bloomon
Ineke Scheffers
 
Qunit Java script Un
akanksha arora
 
Unit Testing JavaScript Applications
Ynon Perek
 
Unit Testing - The Whys, Whens and Hows
atesgoral
 
unit test in node js - test cases in node
Goa App
 
JS Lab`16. Сергей Селецкий: "Ретроспектива тестирования JavaScript"
GeeksLab Odessa
 
JavaScript Testing: Mocha + Chai
James Cryer
 
Unit tests in node.js
Rotem Tamir
 
Tests in Javascript using Jasmine and Testacular
Paulo Cesar Ortins Brito
 
Unit Testing With Javascript
thedumbterminal
 
JavaScript Unit Testing with Jasmine
Raimonds Simanovskis
 
TDD for Javascript developers
Andrey Kucherenko
 
mocha sinon chai Dc jquery 4-24
Carson Banov
 
Testing nodejs apps
felipefsilva
 
How do I write Testable Javascript so I can Test my CF API on Server and Client
Gavin Pickin
 
Unit testing JavaScript using Mocha and Node
Josh Mock
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Anna Khabibullina
 
JS Frameworks Day April,26 of 2014
DA-14
 
Ad

Recently uploaded (20)

PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Software Development Methodologies in 2025
KodekX
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
The Future of Artificial Intelligence (AI)
Mukul
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 

Test innode

  • 1. NodeJs testing tools and continuous integration Davide Fiorello
  • 2. Mocha • Javascript Test Framework • Run in Node.js and Browser environment • BDD, TDD, QUnit • Synchronous and Asynchronous
  • 3. Mocha - Interfaces • BDD 1 describe('My first test', function(){ 2 it('pass it!!'); 3 }); • TDD 1 suite('Array', function(){ 2 setup(function(){ 3 }); 4 5 suite('#indexOf()', function(){ 6 test('should return -1 when not present', function(){ 7 // ASSERT 8 }); 9 }); 10 });
  • 4. Mocha - Sync, Async, Pending 1 describe('Let's test..', function() { 2 it('Test something sync', function() { 3 // ASSERT 4 }); 5 6 it('Test something async', function(done) { 7 user.save(function(err){ 8 if (err) throw err; 9 // ASSERT 10 done(); 11 }); 12 }); 13 14 it('Pending test'); 15 });
  • 5. Mocha - Before, After, ... 1 describe('Let's test..', function() { 2 before(function(){ 3 // ... 4 }); 5 6 beforeEach(function(done){ 7 // ... -> done() 8 }); 9 10 after(function(){ 11 // ... 12 }); 13 14 afterEach(function(){ 15 // ... 16 }); 17 });
  • 6. Mocha - Only, skip 1 describe('A test', function() { 2 it.only('Test only this', function(done) { 3 // ... 4 }); 5 }); 6 7 describe('Another test', function() { 8 it.skip('Don't test this', function(done) { 9 // ... 10 }); 11 });
  • 7. Mocha - Has not... • Assert (should.js, chai, expect.js) • Spy/Mock/Stub (sinon) • Code Coverage (istanbul, node- jscoverage)
  • 8. Should.js should is an expressive, readable, test framework agnostic, assertion library. Main goals of this library to be expressive and to be helpful. It keeps your test code clean, and your error messages helpful.
  • 9. Should.js 1 var should = require('should'); 2 3 var user = { name: 'davide', pets: ['tobi', 'loki']}; 4 5 user.should.have.property('name', 'davide'); 6 user.should.have.property('pets').with.lengthOf(2); 7 8 should(user).have.property('name', 'davide'); 9 should(true).ok; 10 11 should.exist(user); It extends the Object.prototype with a single non- enumerable getter that allows you to express how that object should behave, also it returns itself when required with require.
  • 10. Should.js - Chaining assertions .ok, .true, .false, .eql, .equal, .startWith, .endWith, .within, .above, .belove, .instanceOf, .Array, .Object, .String, .Error, .property, .length, .keys, .match, .throw, .throwError, .json, .html 1 var should = require('should'); 2 3 var user = { name: 'davide', age : 22, pets : ['pit', 'fuffy']}; 4 5 user.name.should.match(/dav/); 6 user.age.should.be.above(18); 7 user.pets.should.be.Array; 8 user.pets.should.containEql('pit'); 9 user.pets.should.not.containEql('mark');
  • 11. Should.js - Chaining assertions 1 var should = require('should'); 2 3 var user = { name: 'davide', age : 22}; 4 5 user.age.should.be.greaterThan(18).and.be.lessThan(33); 6 7 user.age.should.greaterThan(18).lessThan(33); .an, .of, .a,.and, .be, .have, .with, .is, .which Use them for better readability; they do nothing at all
  • 12. Sinon.js Standalone test spies, stubs and mocks for JavaScript.No dependencies, works with any unit testing framework.
  • 13. Sinon.js • Spies • Stubs • Mocks • Fake timers • Fake XHR • Fake server • Sandboxing • Assertions • Matchers
  • 14. Sinon.js - spies 1 var sinon = require('sinon'); 2 var should = require('should'); 3 4 function myFunc(value) { 5 myObject.check(value); 6 } 7 8 var myObject = { 9 check : function(value) { 10 // DO Something 11 } 12 }; 13 var spy = sinon.spy(myObject, "check"); 14 myFunc('test'); 15 spy.called.should.be.true;
  • 15. Sinon.js - spies (API) .withArgs, .callCount, .called, .calledOnce, .firstCall, .lastCall, .calledBefore, .calledAfter, .calledWith, .alwaysCalledWith, .neverCalledWith, .threw, .returned, .args, .returnValues
  • 16. Sinon.js - stubs 1 var sinon = require('sinon'); 2 var should = require('should'); 3 4 function myFunc() { 5 return myObject.check(); 6 } 7 8 var myObject = { 9 check : function() { 10 return 10; 11 // DO Something 12 } 13 }; 14 var stub = sinon.stub(myObject, "check"); 15 stub.onFirstCall().returns(1) 16 .onSecondCall().returns(2); 17 18 myFunc('test').should.be.equal(1); 19 myFunc('test').should.be.equal(2);
  • 17. Sinon.js - Stub (API) .withArgs, .onCall, .onFirstCall, .onSecondCall, .returns, .returnsThis, .returnsArg, .throws, .callsArg, .yelds, .callArgWith, .callArgAsync
  • 18. Useful Modules • node-mocks-http • node-rest-client • pow-mongodb-fixtures • readyness
  • 19. Continuous Integration • drone.io • travis-ci.io • codeship.io