SlideShare a Scribd company logo
1 of 33
© MIRANTIS 20134 PAGE 
Making frontend 
better: 
unit-testing 
Alexandra Morozova
© MIRANTIS 2014 
Preface
© MIRANTIS 2014 
Agenda 
•Why is unit-testing JS needed? 
•Specifics of unit-tests in JS 
•Instruments overview 
•Best practices and approaches
Why do we need all these 
tests? 
© MIRANTIS 2014 PAGE
Big changes done quickly 
© MIRANTIS 2014 PAGE
Fuel example 
© MIRANTIS 2014 PAGE
Fuel example 
© MIRANTIS 2014 PAGE
Step closer to PERFECT 
CODE 
© MIRANTIS 2014 PAGE
Understanding the design 
© MIRANTIS 2014 PAGE
© MIRANTIS 2014 PAGE 
Less time 
for testing
Self-documented code 
© MIRANTIS 2014 PAGE
Tests allow 
REFACTORING 
© MIRANTIS 2014 PAGE
Testing is FUN! 
© MIRANTIS 2014 PAGE
© MIRANTIS 2014 
A few cons 
•Slow down the development process 
•Share the same blind point with the code 
•Do not prove that work one with another
© MIRANTIS 2014 
Basic terms 
•Assertion - defining if test is ok or not 
expect(26+2).to.equal(28);
© MIRANTIS 2014 
Basic terms 
•Fixture - fixed state of software to test, 
basis for tests (AJAX) 
beforeEach(function() { 
loadFixtures(‘dummymarkup.html’); 
})
© MIRANTIS 2014 
Basic terms 
•method Stub - function with pre-programmed 
behaviour 
var fn = foo.stub().throws(Error); 
expect(fn).to.throw(Error);
© MIRANTIS 2014 
Basic terms 
•Spy - function that records arguments, 
scope, return value, exception thrown for 
all its calls 
sinon.spy(cursor, ‘hide’);
© MIRANTIS 2014 
Basic terms 
•Mock - fake object with pre-programmed behavior (like 
stub) and pre-programmed expectations => stubs+spies 
var mock = sinon.mock(“jQuery”);
© MIRANTIS 2014 
Basic Structure 
•Setup - beforeEach(), before() 
•Prepare data for test 
•Call a method 
•Check output data 
•Tear down - afterEach(), after()
© MIRANTIS 2014 
Setup
Prepare, call & check 
© MIRANTIS 2014
© MIRANTIS 2014 
Tear down
© MIRANTIS 2014 
Instruments 
Entire space of 
frameworks...
© MIRANTIS 2014 
Instruments 
Entire space of 
frameworks...
© MIRANTIS 2014 
Karma 
Entire space of 
frameworks...
© MIRANTIS 2014 
Karma 
$ npm install karma 
Karma with 
Require.js 
Entire space of 
frameworks...
Example: Backbone Model 
© MIRANTIS 2014
Example: Backbone Collection 
© MIRANTIS 2014
Example: Backbone Collection 
© MIRANTIS 2014
© MIRANTIS 2014 
Best practices 
• Fast 
• Isolated 
• Consistent 
• Self-descriptive 
• Single responsibility 
• No exceptions handling 
• Use assertions when needed
© MIRANTIS 2014 
Conclusion 
• Each test verifies a small chunk of code 
• Unit tests work better in isolation 
• Mocks, stubs and spies help us 
• Don’t test everything, but write many tests
Q&A 
© MIRANTIS 2013 PAGE

More Related Content

Similar to Unit testing js

Fine-Tuning of Agile Development
Fine-Tuning of Agile DevelopmentFine-Tuning of Agile Development
Fine-Tuning of Agile DevelopmentThoughtworks
 
Continuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and JenkinsContinuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and JenkinsSOASTA
 
Continuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and JenkinsContinuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and JenkinsSOASTA
 
10 Emerging Test Frameworks for Cross Browser Testing
10 Emerging Test Frameworks for Cross Browser Testing10 Emerging Test Frameworks for Cross Browser Testing
10 Emerging Test Frameworks for Cross Browser TestingPerfecto by Perforce
 
Tactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure CodeTactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure CodeDerek Ashmore
 
Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...Niels Frydenholm
 
How the Cloud Shifts the Burden of Security to Development
How the Cloud Shifts the Burden of Security to DevelopmentHow the Cloud Shifts the Burden of Security to Development
How the Cloud Shifts the Burden of Security to DevelopmentErika Barron
 
Achieve Performance Testing Excellence for Your SAP Apps
Achieve Performance Testing Excellence for Your SAP AppsAchieve Performance Testing Excellence for Your SAP Apps
Achieve Performance Testing Excellence for Your SAP AppsNeotys
 
Serena Webcast: Accelerating Application Delivery with Continuous Testing
Serena Webcast: Accelerating Application Delivery with Continuous TestingSerena Webcast: Accelerating Application Delivery with Continuous Testing
Serena Webcast: Accelerating Application Delivery with Continuous TestingSerena Software
 
Architecture of automated test cases for legacy applications
Architecture of automated test cases for legacy applicationsArchitecture of automated test cases for legacy applications
Architecture of automated test cases for legacy applicationsMikhail Vasylchenko
 
JFokus Java 9 contended locking performance
JFokus Java 9 contended locking performanceJFokus Java 9 contended locking performance
JFokus Java 9 contended locking performanceMonica Beckwith
 
Monitoring Attack Surface to Secure DevOps Pipelines
Monitoring Attack Surface to Secure DevOps PipelinesMonitoring Attack Surface to Secure DevOps Pipelines
Monitoring Attack Surface to Secure DevOps PipelinesDenim Group
 
Tune your App Perf (and get fit for summer)
Tune your App Perf (and get fit for summer)Tune your App Perf (and get fit for summer)
Tune your App Perf (and get fit for summer)Sqreen
 
How to Release Rock-solid RESTful APIs and Ice the Testing BackBlob
How to Release Rock-solid RESTful APIs and Ice the Testing BackBlobHow to Release Rock-solid RESTful APIs and Ice the Testing BackBlob
How to Release Rock-solid RESTful APIs and Ice the Testing BackBlobBob Binder
 
Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...
Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...
Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...JAXLondon2014
 
End to End Testing with nightwatchjs
End to End  Testing with nightwatchjsEnd to End  Testing with nightwatchjs
End to End Testing with nightwatchjsSrikanth Madduri
 
Creating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJSCreating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJSGunnar Hillert
 
Test automation Frame Works
Test automation Frame WorksTest automation Frame Works
Test automation Frame WorksvodQA
 

Similar to Unit testing js (20)

Unit testing js
Unit testing jsUnit testing js
Unit testing js
 
Fine-Tuning of Agile Development
Fine-Tuning of Agile DevelopmentFine-Tuning of Agile Development
Fine-Tuning of Agile Development
 
Continuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and JenkinsContinuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and Jenkins
 
Continuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and JenkinsContinuous Load Testing with CloudTest and Jenkins
Continuous Load Testing with CloudTest and Jenkins
 
10 Emerging Test Frameworks for Cross Browser Testing
10 Emerging Test Frameworks for Cross Browser Testing10 Emerging Test Frameworks for Cross Browser Testing
10 Emerging Test Frameworks for Cross Browser Testing
 
Tactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure CodeTactics for Testing DevOps Infrastructure Code
Tactics for Testing DevOps Infrastructure Code
 
Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...Getting your mobile test automation process in place - using Cucumber and Cal...
Getting your mobile test automation process in place - using Cucumber and Cal...
 
How the Cloud Shifts the Burden of Security to Development
How the Cloud Shifts the Burden of Security to DevelopmentHow the Cloud Shifts the Burden of Security to Development
How the Cloud Shifts the Burden of Security to Development
 
Achieve Performance Testing Excellence for Your SAP Apps
Achieve Performance Testing Excellence for Your SAP AppsAchieve Performance Testing Excellence for Your SAP Apps
Achieve Performance Testing Excellence for Your SAP Apps
 
Serena Webcast: Accelerating Application Delivery with Continuous Testing
Serena Webcast: Accelerating Application Delivery with Continuous TestingSerena Webcast: Accelerating Application Delivery with Continuous Testing
Serena Webcast: Accelerating Application Delivery with Continuous Testing
 
Architecture of automated test cases for legacy applications
Architecture of automated test cases for legacy applicationsArchitecture of automated test cases for legacy applications
Architecture of automated test cases for legacy applications
 
JFokus Java 9 contended locking performance
JFokus Java 9 contended locking performanceJFokus Java 9 contended locking performance
JFokus Java 9 contended locking performance
 
Monitoring Attack Surface to Secure DevOps Pipelines
Monitoring Attack Surface to Secure DevOps PipelinesMonitoring Attack Surface to Secure DevOps Pipelines
Monitoring Attack Surface to Secure DevOps Pipelines
 
Tune your App Perf (and get fit for summer)
Tune your App Perf (and get fit for summer)Tune your App Perf (and get fit for summer)
Tune your App Perf (and get fit for summer)
 
How to Release Rock-solid RESTful APIs and Ice the Testing BackBlob
How to Release Rock-solid RESTful APIs and Ice the Testing BackBlobHow to Release Rock-solid RESTful APIs and Ice the Testing BackBlob
How to Release Rock-solid RESTful APIs and Ice the Testing BackBlob
 
Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...
Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...
Detecting Events on the Web in Real Time with Java, Kafka and ZooKeeper - Jam...
 
End to End Testing with nightwatchjs
End to End  Testing with nightwatchjsEnd to End  Testing with nightwatchjs
End to End Testing with nightwatchjs
 
Creating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJSCreating Modular Test-Driven SPAs with Spring and AngularJS
Creating Modular Test-Driven SPAs with Spring and AngularJS
 
Test automation Frame Works
Test automation Frame WorksTest automation Frame Works
Test automation Frame Works
 
Secure DevOps: A Puma's Tail
Secure DevOps: A Puma's TailSecure DevOps: A Puma's Tail
Secure DevOps: A Puma's Tail
 

Recently uploaded

『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxNIMMANAGANTI RAMAKRISHNA
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxMario
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119APNIC
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxAndrieCagasanAkio
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxmibuzondetrabajo
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 

Recently uploaded (11)

『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptx
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptx
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptx
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptx
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 

Unit testing js