SlideShare une entreprise Scribd logo
1  sur  20
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

Contenu connexe

Tendances

10 Catalyst Tips
10 Catalyst Tips10 Catalyst Tips
10 Catalyst TipsJay Shirley
 
Real World Mocking In Swift
Real World Mocking In SwiftReal World Mocking In Swift
Real World Mocking In SwiftVeronica Lillie
 
How we're using Firebase at Boiler Room
How we're using Firebase at Boiler RoomHow we're using Firebase at Boiler Room
How we're using Firebase at Boiler RoomLukas Klein
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debuggingJungMinSEO5
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Wilson Su
 
Synchronisation de périphériques avec Javascript et PouchDB
Synchronisation de périphériques avec Javascript et PouchDBSynchronisation de périphériques avec Javascript et PouchDB
Synchronisation de périphériques avec Javascript et PouchDBFrank Rousseau
 
C++ Programming - 9th Study
C++ Programming - 9th StudyC++ Programming - 9th Study
C++ Programming - 9th StudyChris Ohk
 
C++ Programming - 12th Study
C++ Programming - 12th StudyC++ Programming - 12th Study
C++ Programming - 12th StudyChris Ohk
 
DEF CON 23 - amit ashbel and maty siman - game of hacks
DEF CON 23 - amit ashbel and maty siman - game of hacks DEF CON 23 - amit ashbel and maty siman - game of hacks
DEF CON 23 - amit ashbel and maty siman - game of hacks Felipe Prado
 
rsyslog v8: more than just syslog!
rsyslog v8: more than just syslog!rsyslog v8: more than just syslog!
rsyslog v8: more than just syslog!Yury Bushmelev
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Thomas Fuchs
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlNova Patch
 
Extreme JavaScript Performance
Extreme JavaScript PerformanceExtreme JavaScript Performance
Extreme JavaScript PerformanceThomas Fuchs
 

Tendances (20)

Intro to Sail.js
Intro to Sail.jsIntro to Sail.js
Intro to Sail.js
 
10 Catalyst Tips
10 Catalyst Tips10 Catalyst Tips
10 Catalyst Tips
 
hacking with node.JS
hacking with node.JShacking with node.JS
hacking with node.JS
 
Real World Mocking In Swift
Real World Mocking In SwiftReal World Mocking In Swift
Real World Mocking In Swift
 
Node.js 0.8 features
Node.js 0.8 featuresNode.js 0.8 features
Node.js 0.8 features
 
How we're using Firebase at Boiler Room
How we're using Firebase at Boiler RoomHow we're using Firebase at Boiler Room
How we're using Firebase at Boiler Room
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
Live Updating Swift Code
Live Updating Swift CodeLive Updating Swift Code
Live Updating Swift Code
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
 
Synchronisation de périphériques avec Javascript et PouchDB
Synchronisation de périphériques avec Javascript et PouchDBSynchronisation de périphériques avec Javascript et PouchDB
Synchronisation de périphériques avec Javascript et PouchDB
 
C++ Programming - 9th Study
C++ Programming - 9th StudyC++ Programming - 9th Study
C++ Programming - 9th Study
 
C++ Programming - 12th Study
C++ Programming - 12th StudyC++ Programming - 12th Study
C++ Programming - 12th Study
 
DEF CON 23 - amit ashbel and maty siman - game of hacks
DEF CON 23 - amit ashbel and maty siman - game of hacks DEF CON 23 - amit ashbel and maty siman - game of hacks
DEF CON 23 - amit ashbel and maty siman - game of hacks
 
rsyslog v8: more than just syslog!
rsyslog v8: more than just syslog!rsyslog v8: more than just syslog!
rsyslog v8: more than just syslog!
 
PL/SQL Unit Testing Can Be Fun
PL/SQL Unit Testing Can Be FunPL/SQL Unit Testing Can Be Fun
PL/SQL Unit Testing Can Be Fun
 
Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)Rich and Snappy Apps (No Scaling Required)
Rich and Snappy Apps (No Scaling Required)
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in Perl
 
Ruby tricks2
Ruby tricks2Ruby tricks2
Ruby tricks2
 
Extreme JavaScript Performance
Extreme JavaScript PerformanceExtreme JavaScript Performance
Extreme JavaScript Performance
 
Node.js - A Quick Tour
Node.js - A Quick TourNode.js - A Quick Tour
Node.js - A Quick Tour
 

Similaire à Test innode

Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.jsJay Harris
 
JavaScript Proven Practises
JavaScript Proven PractisesJavaScript Proven Practises
JavaScript Proven PractisesRobert MacLean
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScriptJohannes Hoppe
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScriptJohannes Hoppe
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with JestMichał Pierzchała
 
Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113SOAT
 
Browser testing with nightwatch.js - Drupal Europe
Browser testing with nightwatch.js - Drupal EuropeBrowser testing with nightwatch.js - Drupal Europe
Browser testing with nightwatch.js - Drupal EuropeSalvador Molina (Slv_)
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014Guillaume POTIER
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014alexandre freire
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeJosh Mock
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsFITC
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 
Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)Anis Bouhachem Djer
 
Unit Testing Express Middleware
Unit Testing Express MiddlewareUnit Testing Express Middleware
Unit Testing Express MiddlewareMorris Singer
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsOhad Kravchick
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Anna Khabibullina
 

Similaire à Test innode (20)

Test driven node.js
Test driven node.jsTest driven node.js
Test driven node.js
 
JavaScript Proven Practises
JavaScript Proven PractisesJavaScript Proven Practises
JavaScript Proven Practises
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113Conf soat tests_unitaires_Mockito_jUnit_170113
Conf soat tests_unitaires_Mockito_jUnit_170113
 
Browser testing with nightwatch.js - Drupal Europe
Browser testing with nightwatch.js - Drupal EuropeBrowser testing with nightwatch.js - Drupal Europe
Browser testing with nightwatch.js - Drupal Europe
 
How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014How to test complex SaaS applications - The family july 2014
How to test complex SaaS applications - The family july 2014
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
Unit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and NodeUnit testing JavaScript using Mocha and Node
Unit testing JavaScript using Mocha and Node
 
ES2015 workflows
ES2015 workflowsES2015 workflows
ES2015 workflows
 
Test-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS ApplicationsTest-Driven Development of AngularJS Applications
Test-Driven Development of AngularJS Applications
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)Angularjs Test Driven Development (TDD)
Angularjs Test Driven Development (TDD)
 
Unit Testing Express Middleware
Unit Testing Express MiddlewareUnit Testing Express Middleware
Unit Testing Express Middleware
 
Testing in JavaScript
Testing in JavaScriptTesting in JavaScript
Testing in JavaScript
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 
Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)Js fwdays unit tesing javascript(by Anna Khabibullina)
Js fwdays unit tesing javascript(by Anna Khabibullina)
 

Dernier

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Dernier (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

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