Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Javascript Testing - V1

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Javascript Testing - V1

Over the years I've become a bit of a testing evangelist. This is a presentation I did at Mobify on testing.

Discussed:
- Reasons for testing
- Testing methodologies
- My favourite types of Javascript testing tools

Avatar for Harold Treen

Harold Treen

October 23, 2015
Tweet

Other Decks in Programming

Transcript

  1. My Background • First 7 months of internships were in

    Quality Assurance ◦ 4 months as a QA intern at Xtreme Labs (now Pivotal Labs) ◦ 3 months as a SDET intern at Microsoft • Next, did a 12 month software engineering internship at Pivotal Labs ◦ 2 months of TDD on Android ◦ 3 months of TDD on Javascript/AngularJS ◦ 7 months of TDD on Ruby projects ◦ 1 project with 98% code coverage and 1600 tests running in ~2 minutes • ...Left me somewhat into testing :)
  2. Reasons for writing tests: • Greater confidence → Easier to

    make changes → Better Codebase → Greater confidence • Tests document pieces of code and explain how they work ◦ Easy to ramp up new team members ◦ Some test libraries generate documentation from your tests! • Forces you to use the APIs you are exposing ◦ Leads to better design • Better isolation of bugs ◦ Testing code at the unit level vs. interface level
  3. Reasons for writing tests: • Much faster and more repeatable

    than manual testing ◦ Automated test: ▪ ‘grunt test’ → Pass/Fail ◦ Manual test: ▪ Open chrome → Mobify Preview → Click on stuff → Pass/Fail ▪ Open terminal → ‘node myapp.js’ → Read output → Pass/Fail
  4. TDD - Test Driving All The Development • Want to

    ship features, not code ◦ Writing tests first allow you to define your features/functionality ◦ Only want to write enough code to satisfy that requirement - no more! • If you have only written code that is necessary to satisfy tests, then you should have 100% code coverage. ◦ If your test suite can verify every line of code works, you’ll feel more willing to rip apart all the things.
  5. Types of Tests • Unit testing - Verifying individual methods/classes

    - small units of code ◦ Dependencies usually mocked • Integration testing - Verifies functionality of multiple integrated components • Many others: ◦ Black box, white box, functional, system, end-to-end, sanity, regression, load, stress, performance, usability, install/uninstall, recovery, security, compatibility, comparison, alpha, beta • Different test environments: ◦ Browsers - Selenium/Chrome ◦ Headless Browser - PhantomJS ◦ No Browser - Node.js
  6. Testing Tools • Mocha • Jasmine • Istanbul • Nightwatch

    • Selenium • Chai • Nock • Karma • PhantomJS • Mocha PhantomJS • Mockery • grunt-mocha-test
  7. Testing Tools Frameworks • Mocha • Jasmine • Nightwatch Code

    Coverage • Istanbul Test Runners • Karma • Mocha PhantomJS • grunt-mocha-test Web Environments • PhantomJS • Chrome Web Driver • Selenium Assertion Framework • Chai Mocking Tools • Nock • Mockery And so many more...
  8. Testing Tools: Frameworks • Frameworks define an API for writing

    tests. • Popular Frameworks: ◦ Mocha ◦ Nightwatch ◦ Jasmine
  9. Testing Tools: Frameworks • Nightwatch • More for systems tests

    • Browser can be Chrome, PhantomJS, etc.
  10. Testing Tools: Code Coverage • How it works: ◦ Code

    is instrumented to log every line that is executed ◦ Instrumented code is loaded by tests ◦ Code coverage tools generates a report of what code was/wasn’t run • Istanbul is a popular code coverage library • Example Coverage Report
  11. Testing Tools: Code Coverage • ‘grunt-mocha-istanbul’ is a nice plugin

    for setting up code coverage • As implemented in ‘speedtrap’
  12. Testing Tools: Test Runners • Popular Test Runners: ◦ Karma

    ◦ grunt-mocha-test ◦ Mocha PhantomJS • Can automate certain tasks when running tests ◦ Finding new test files ◦ Passing the tests to your test framework ◦ Instrumenting tests for Code Coverage ◦ Launching tests in different environments ◦ Running tests whenever source files are edited
  13. Testing Tools: Assertion Frameworks • Assertion frameworks give a variety

    of choices of how to write tests • Chai is a popular assertion library for Javascript ◦ http://chaijs.com/ • Provides different styles for expressing tests:
  14. Testing Tools: Mocking Tools • Good tests should target isolated

    pieces of code ◦ Otherwise a change in module A might start making module B’s tests fail! • Fast testing requires mocking out slow external resources ◦ Testing an email system without sending emails ◦ Testing user creation without hitting a database ◦ Testing ability to handle HTTP responses without making requests • Some handy tools: ◦ Nock - Mocks HTTP requests in Node ◦ Mockery - Mocks module require in Node ◦ Squire.js - Dependency injection/Mocking for RequireJS
  15. Testing Tools: Mocking Tools • Nock: • When we request

    ‘http://myapp.iriscouch.com/users/1’ , we’ll get the mocked response we specified.
  16. Testing Tools: Mocking Tools • Mockery: • In our code,

    when we do: var fs = require(‘fs’); we will get: { stat: function(path, cb) { /* mock code */ } • Super handy!