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.

Design and code JavaScript applications with automated tests. Writing meaningful tests is a skill that takes learning, some unlearning, and a lot of practice, and with this book, you'll hone that skill. Fire up the editor and get hands-on through practical exercises for effective automated testing and designing maintainable, modular code.

Start by learning when and why to do manual testing vs. automated verification. Focus tests on the important things, like the pre-conditions, the invariants, complex logic, and gnarly edge cases. Then begin to design asynchronous functions using automated tests. Carefully decouple and mock out intricate dependencies such as the DOM, geolocation API, file and database access, and Ajax calls to remote servers.

Step by step, test code that uses Node.js, Express, MongoDB, jQuery, and AngularJS. Know when and how to use tools such as Chai, Istanbul, Karma, Mocha, Protractor, and Sinon. Create tests with minimum effort and run them fast without having to spin up web servers or manually edit HTML pages to run in browsers. Then explore end-to-end testing to ensure all parts are wired and working well together.

Don't just imagine creating testable code, write it.

What You Need:

A computer with a text editor and your favorite browser. The book provides instructions to install the necessary automated testing-related tools.

Publisher resources

View/Submit Errata

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

Product information

  • Title: Test-Driving JavaScript Applications
  • Author(s): Venkat Subramaniam
  • Release date: October 2016
  • Publisher(s): Pragmatic Bookshelf
  • ISBN: 9781680504903