SlideShare ist ein Scribd-Unternehmen logo
1 von 13
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

Weitere ähnliche Inhalte

Was ist angesagt?

Best Practice Testing with Lime 2
Best Practice Testing with Lime 2Best Practice Testing with Lime 2
Best Practice Testing with Lime 2
Bernhard Schussek
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your project
Michelangelo van Dam
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
adamlogic
 
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and JasmineRails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 

Was ist angesagt? (20)

날로 먹는 Django admin 활용
날로 먹는 Django admin 활용날로 먹는 Django admin 활용
날로 먹는 Django admin 활용
 
Best Practice Testing with Lime 2
Best Practice Testing with Lime 2Best Practice Testing with Lime 2
Best Practice Testing with Lime 2
 
Soa lab 3
Soa lab 3Soa lab 3
Soa lab 3
 
WordPress Capabilities Magic
WordPress Capabilities MagicWordPress Capabilities Magic
WordPress Capabilities Magic
 
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j queryTraining in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
Training in Asp.net mvc3 platform-apextgi,noidaAspnetmvc3 j query
 
Using of TDD practices for Magento
Using of TDD practices for MagentoUsing of TDD practices for Magento
Using of TDD practices for Magento
 
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015Why You Should be Using Web Components Right Now. And How. ForwardJS July 2015
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...
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
Nagios Conference 2014 - Troy Lea - JavaScript and jQuery - Nagios XI Tips, T...
 
1cst
1cst1cst
1cst
 
Modular Angular JS
Modular Angular JSModular Angular JS
Modular Angular JS
 
Handlebars
HandlebarsHandlebars
Handlebars
 
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridasFrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
FrontInBahia 2014: 10 dicas de desempenho para apps mobile híbridas
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your project
 
jQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a TreejQuery and Rails, Sitting in a Tree
jQuery and Rails, Sitting in a Tree
 
Fixing Magento Core for Better Performance - Ivan Chepurnyi
Fixing Magento Core for Better Performance - Ivan ChepurnyiFixing Magento Core for Better Performance - Ivan Chepurnyi
Fixing Magento Core for Better Performance - Ivan Chepurnyi
 
Jquery examples
Jquery examplesJquery examples
Jquery examples
 
Odoo - CMS performances optimization
Odoo - CMS performances optimizationOdoo - CMS performances optimization
Odoo - CMS performances optimization
 
Canjs
CanjsCanjs
Canjs
 
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and JasmineRails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
Rails-like JavaScript using CoffeeScript, Backbone.js and Jasmine
 
Magento Indexes
Magento IndexesMagento Indexes
Magento Indexes
 

Ähnlich wie Best practices for js testing

Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)Is HTML5 Ready? (workshop)
Is HTML5 Ready? (workshop)
Remy Sharp
 
Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02Is html5-ready-workshop-110727181512-phpapp02
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
After max+phonegap
After max+phonegapAfter max+phonegap
After max+phonegap
yangdj
 
混搭移动开发:PhoneGap+JQurey+Dreamweaver
混搭移动开发:PhoneGap+JQurey+Dreamweaver混搭移动开发:PhoneGap+JQurey+Dreamweaver
混搭移动开发:PhoneGap+JQurey+Dreamweaver
yangdj
 
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
09 - express nodes on the right angle - vitaliy basyuk - it event 2013 (5)
Igor Bronovskyy
 

Ähnlich wie Best practices for js testing (20)

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

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
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
 

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

Hinweis der Redaktion

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