O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Test-Driving JavaScript Applications

Book Description

Debunk the myth that JavaScript is not easily testable. Whether you use Node.js, Express, MongoDB, jQuery, AngularJS, or directly manipulate the DOM, you can test-drive JavaScript. Learn the craft of writing meaningful, deterministic automated tests with Karma, Mocha, and Chai. Test asynchronous JavaScript, decouple and properly mock out dependencies, measure code coverage, and create lightweight modular designs of both server-side and client-side code. Your investment in writing tests will pay high dividends as you create code that's predictable and cost-effective to change.

Table of Contents

  1.  Acknowledgments
  2.  Introduction
    1. What’s in This Book?
    2. Who Is This Book For?
    3. Online Resources
    4. Your Own Workspace
  3. 1. Automation Shall Set You Free
    1. The Challenges of Change
    2. Testing vs. Verification
    3. Adopting Automated Verification
    4. Why Is It Hard to Verify?
    5. How to Approach Automated Testing
    6. Wrapping Up
  4. Part I. Creating Automated Tests
    1. 2. Test-Drive Your Design
      1. Let’s Get Started
      2. Design with Positive, Negative, and Exception Tests
      3. Design Server-Side Code
      4. Measure Server-Side Code Coverage
      5. Prepare for Client-Side Testing
      6. Design Client-Side Code
      7. Measure Client-Side Code Coverage
      8. Wrapping Up
    2. 3. Test Asynchrony
      1. Server-Side Callbacks
      2. Client-Side Callbacks
      3. Test Your Promises
      4. Wrapping Up
    3. 4. Tactfully Tackle Dependencies
      1. A Problem and Spiking to Learn
      2. Visualize a Modular Design
      3. Separate Dependencies Where Possible
      4. Employ Test Doubles
      5. Inject the Dependencies
      6. Test Interactions
      7. Use Sinon to Reduce Noise
      8. Review and Run
      9. Wrapping Up
  5. Part II. Real-World Automated Testing
    1. 5. Test-Drive Node.js Apps
      1. Start with a Strategic—Just Enough—Design
      2. Leap Into Tactical—Test First—Design
      3. Continue the Design
      4. Create a Spike to Gain Insight
      5. Modularize to Facilitate Testing
      6. Separate Concerns
      7. Integrate and Run
      8. Review the Coverage and Design
      9. Providing HTTP Access
      10. Wrapping Up
    2. 6. Test-Drive Express Apps
      1. Design for Testability
      2. Set Up the Express App and Run a Canary Test
      3. Design the Database Connection
      4. Design the Model
      5. Design the Routes
      6. Measure Code Coverage
      7. Take It for a Drive
      8. Wrapping Up
    3. 7. Working with the DOM and jQuery
      1. Create a Strategic Design
      2. Create the Tactical Design with Tests
      3. Evolve Code in Small Steps
      4. Take the UI for a Short Drive
      5. Complete the Design
      6. Test with jQuery
      7. Measure Code Coverage
      8. Wrapping Up
    4. 8. Using AngularJS
      1. Testing the AngularJS Way
      2. Start with an Initial Design
      3. Focus on the Controller
      4. Design Service Interaction
      5. Separate Concerns, Reduce Mocking
      6. Continue the Design
      7. Design the Service
      8. Measure Code Coverage
      9. Take the UI for a Drive
      10. Wrapping Up
    5. 9. Test-Drive Angular 2
      1. Spike to Learn Angular
      2. Design Angular Apps with Tests
      3. Test-Drive the Component
      4. Test-Drive the Service
      5. Test-Drive the Pipe
      6. Test-Drive the BootStrap Code
      7. Take It for a Ride
      8. Complete the Design
      9. Wrapping Up
    6. 10. Integrate and Test End-to-End
      1. Get to Know Protractor
      2. Start the Server and Configure the Database
      3. Test jQuery UI
      4. Let’s Use a Page Object
      5. Test AngularJS UI
      6. Test Angular 2 UI
      7. Wrapping Up
    7. 11. Test-Drive Your Apps
      1. The Fruits of Our Efforts
      2. Size and Levels of Testing
      3. Test Driving: The Programmer’s Guide
      4. Test Driving: The Team Lead’s/Architect’s Guide
      5. Test Driving: The Manager’s Guide
      6. Rock On!
  6. A1. Web Resources
  7.  Bibliography