SlideShare a Scribd company logo
HTML5 Developer Conf. 2012
Best practices for JavaScript Testing




            Karl Mendes
Best practices for a JS test workflow




       1.   Create testable code
       2.   Find a good testing framework
       3.   Test against real HTML
       4.   Test against 'real' data
       5.   Use continuous integration
       6.   Enable in-browser debugging
       7.   Enable automated cross-browser testing
       8.   Enforce red, green, refactor!
Best practices for a JS test workflow




       1.   Create testable code
       2.   Find a good testing framework
       3.   Test against real HTML
       4.   Test against 'real' data
       5.   Use continuous integration
       6.   Enable in-browser debugging
       7.   Enable automated cross-browser testing
       8.   Enforce red, green, refactor!
Test against real HTML
Test against real HTML



                $(function() {
                    $('#hide-action').on('click', function(e) {
 JavaScript             e.preventDefault();
                        $('#my-image').hide();
                    });
                });



  HTML          <a id="hide-action" href="#hide”>Hide image</a>
                <img id="my-image" src="img.png"/>




                it('should hide the image when click on button', function() {
                    var image = $('#my-image');
    Test            expect(image).toBeVisible();
                    $('#hide-action').trigger('click');
                    expect(image).not.toBeVisible();
                });




                           Test result: Success
Test against real HTML



                $(function() {
                    $('#hide-action').on('click', function(e) {
 JavaScript             e.preventDefault();
                        $('#my-image').hide();
                    });
                });



  HTML          <a id="hide-action" href="#hide”>Hide image</a>
                <img id=”main-image" src="img.png"/>




                it('should hide the image when click on button', function() {
                    var image = $('#my-image');
    Test            expect(image).toBeVisible();
                    $('#hide-action').trigger('click');
                    expect(image).not.toBeVisible();
                });




                           Test result: Failed
Enable automated cross-browser testing
Enable automated cross-browser testing


                                 var myArray = [1,2,3,];

                           IE8             IE9              FF   Chrome
    myArray.length         4               3                3    3



    var myArray = [1,2,3,];

    describe('array test', function() {
        it('should return the right length', function() {
            expect(myArray.length).toBe(3);
        });
    });
Enable automated cross-browser testing



  IE9, FF, Chrome




  IE8
Enforce red, green, refactor!
Enforce red, green, refactor!




                                RED




                                      GREEN
              REFACTOR
@karlmendes
http://karlmendes.com

More Related Content

PDF
Testing with mock object
PDF
2013-06-25 - HTML5 & JavaScript Security
PDF
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
PDF
Embracing Capybara
PDF
Angular Directives from Scratch
PDF
Functional testing with capybara
PPTX
Meetup django common_problems(1)
DOCX
Filters in AngularJS
Testing with mock object
2013-06-25 - HTML5 & JavaScript Security
Angular js is the future. maybe. @ ConFoo 2014 in Montreal (CA)
Embracing Capybara
Angular Directives from Scratch
Functional testing with capybara
Meetup django common_problems(1)
Filters in AngularJS

What's hot (20)

PDF
날로 먹는 Django admin 활용
ODP
Best Practice Testing with Lime 2
DOC
Soa lab 3
PDF
WordPress Capabilities Magic
PDF
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
PPTX
Using of TDD practices for Magento
PDF
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
ODP
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
PDF
Modular Angular JS
PDF
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
PDF
Zf2 how arrays will save your project
PDF
jQuery and Rails, Sitting in a Tree
PPTX
Fixing Magento Core for Better Performance - Ivan Chepurnyi
DOC
Jquery examples
PDF
Odoo - CMS performances optimization
PDF
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
PPTX
Magento Indexes
날로 먹는 Django admin 활용
Best Practice Testing with Lime 2
Soa lab 3
WordPress Capabilities Magic
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
Using of TDD practices for Magento
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Modular Angular JS
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
Zf2 how arrays will save your project
jQuery and Rails, Sitting in a Tree
Fixing Magento Core for Better Performance - Ivan Chepurnyi
Jquery examples
Odoo - CMS performances optimization
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Magento Indexes
Ad

Similar to Best practices for js testing (20)

PDF
You don't know people
PDF
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
PDF
UI Testing Best Practices - An Expected Journey
PDF
Clean Javascript
PPTX
Javascript first-class citizenery
PDF
Is HTML5 Ready? (workshop)
PDF
Is html5-ready-workshop-110727181512-phpapp02
PDF
Automated testing for client-side - Adam Klein, 500 Tech
PDF
Client side unit tests - using jasmine & karma
PDF
Тестирование и Django
KEY
jQuery Bay Area Conference 2010
PDF
After max+phonegap
PDF
混搭移动开发:PhoneGap+JQurey+Dreamweaver
PDF
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
PPTX
Testing ASP.NET - Progressive.NET
PPTX
jQuery for web development
PDF
jQuery secrets
PDF
Virtual Madness @ Etsy
PDF
QConSP 2015 - Dicas de Performance para Aplicações Web
PDF
UA testing with Selenium and PHPUnit - PHPBenelux Summer BBQ
You don't know people
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
UI Testing Best Practices - An Expected Journey
Clean Javascript
Javascript first-class citizenery
Is HTML5 Ready? (workshop)
Is html5-ready-workshop-110727181512-phpapp02
Automated testing for client-side - Adam Klein, 500 Tech
Client side unit tests - using jasmine & karma
Тестирование и Django
jQuery Bay Area Conference 2010
After max+phonegap
混搭移动开发:PhoneGap+JQurey+Dreamweaver
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Testing ASP.NET - Progressive.NET
jQuery for web development
jQuery secrets
Virtual Madness @ Etsy
QConSP 2015 - Dicas de Performance para Aplicações Web
UA testing with Selenium and PHPUnit - PHPBenelux Summer BBQ
Ad

Recently uploaded (20)

PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Big Data Technologies - Introduction.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Diabetes mellitus diagnosis method based random forest with bat algorithm
20250228 LYD VKU AI Blended-Learning.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine Learning_overview_presentation.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding
Big Data Technologies - Introduction.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation_ Review paper, used for researhc scholars
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf

Best practices for js testing

  • 2. Best practices for JavaScript Testing Karl Mendes
  • 3. Best practices for a JS test workflow 1. Create testable code 2. Find a good testing framework 3. Test against real HTML 4. Test against 'real' data 5. Use continuous integration 6. Enable in-browser debugging 7. Enable automated cross-browser testing 8. Enforce red, green, refactor!
  • 4. Best practices for a JS test workflow 1. Create testable code 2. Find a good testing framework 3. Test against real HTML 4. Test against 'real' data 5. Use continuous integration 6. Enable in-browser debugging 7. Enable automated cross-browser testing 8. Enforce red, green, refactor!
  • 6. Test against real HTML $(function() { $('#hide-action').on('click', function(e) { JavaScript e.preventDefault(); $('#my-image').hide(); }); }); HTML <a id="hide-action" href="#hide”>Hide image</a> <img id="my-image" src="img.png"/> it('should hide the image when click on button', function() { var image = $('#my-image'); Test expect(image).toBeVisible(); $('#hide-action').trigger('click'); expect(image).not.toBeVisible(); }); Test result: Success
  • 7. Test against real HTML $(function() { $('#hide-action').on('click', function(e) { JavaScript e.preventDefault(); $('#my-image').hide(); }); }); HTML <a id="hide-action" href="#hide”>Hide image</a> <img id=”main-image" src="img.png"/> it('should hide the image when click on button', function() { var image = $('#my-image'); Test expect(image).toBeVisible(); $('#hide-action').trigger('click'); expect(image).not.toBeVisible(); }); Test result: Failed
  • 9. Enable automated cross-browser testing var myArray = [1,2,3,]; IE8 IE9 FF Chrome myArray.length 4 3 3 3 var myArray = [1,2,3,]; describe('array test', function() { it('should return the right length', function() { expect(myArray.length).toBe(3); }); });
  • 10. Enable automated cross-browser testing IE9, FF, Chrome IE8
  • 11. Enforce red, green, refactor!
  • 12. Enforce red, green, refactor! RED GREEN REFACTOR

Editor's Notes

  • #14: If you’re interested on an extended exploration of all of those requirements you can read my blog post about it.