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

Testable JavaScript

Book Description

One skill that’s essential for any professional JavaScript developer is the ability to write testable code. This book shows you what writing and maintaining testable JavaScript for the client- or server-side actually entails, whether you’re creating a new application or rewriting legacy code.

From methods to reduce code complexity to unit testing, code coverage, debugging, and automation, you’ll learn a holistic approach for writing JavaScript code that you and your colleagues can easily fix and maintain going forward. Testing JavaScript code is complicated. This book helps experienced JavaScript developers simply the process considerably.

  • Get an overview of Agile, test-driven development, and behavior-driven development
  • Use patterns from static languages and standards-based JavaScript to reduce code complexity
  • Learn the advantages of event-based architectures, including modularity, loose coupling, and reusability
  • Explore tools for writing and running unit tests at the functional and application level
  • Generate code coverage to measure the scope and effectiveness of your tests
  • Conduct integration, performance, and load testing, using Selenium or CasperJS
  • Use tools for in-browser, Node.js, mobile, and production debugging
  • Understand what, when, and how to automate your development processes

Table of Contents

  1. Dedication
  2. Preface
    1. The Goal of This Book
    2. Who This Book Is For
    3. Who This Book Is Not For
    4. Who I Am
    5. What You Will Learn from This Book
      1. Content
    6. If You Like (or Don’t Like) This Book
    7. Recap
    8. How to Contact Us
    9. Conventions Used in This Book
    10. Using Code Examples
    11. Safari® Books Online
    12. Thanks!
  3. 1. Testable JavaScript
    1. Prior Art
      1. Agile Development
      2. Test-Driven Development
      3. Behavior-Driven Development
      4. The Best Approach?
    2. Code Is for People
      1. Why
        1. Why testable
        2. Why maintainable
        3. Why understandable
      2. What
        1. What is testable
        2. What is maintainable
        3. What is understandable
      3. How
        1. How testable
        2. How maintainable
        3. How understandable
    3. Beyond Application Code
      1. Testing
      2. Debugging
    4. Recap
  4. 2. Complexity
    1. Code Size
    2. JSLint
    3. Cyclomatic Complexity
    4. Reuse
    5. Fan-Out
    6. Fan-In
    7. Coupling
      1. Content Coupling
      2. Common Coupling
      3. Control Coupling
      4. Stamp Coupling
      5. Data Coupling
      6. No Coupling
      7. Instantiation
    8. Coupling Metrics
    9. Coupling in the Real World
      1. Testing Coupled Code
    10. Dependency Injection
    11. Comments
      1. YUIDoc
      2. JSDoc
      3. Docco/Rocco
    12. The Human Test
    13. Recap
  5. 3. Event-Based Architectures
    1. The Benefits of Event-Based Programming
    2. The Event Hub
      1. Using the Event Hub
      2. Responses to Thrown Events
      3. Event-Based Architectures and MVC Approaches
      4. Event-Based Architectures and Object-Oriented Programming
      5. Event-Based Architectures and Software as a Service
    3. Web-Based Applications
    4. Testing Event-Based Architectures
    5. Caveats to Event-Based Architectures
      1. Scalability
      2. Broadcasting
      3. Runtime Checking
      4. Security
      5. State
    6. A Smarter Hub: The Event Switch
      1. Deployment
        1. Unicast events
        2. Broadcast events
      2. An Implementation
      3. Sessions
      4. Extensibility
    7. Recap
  6. 4. Unit Tests
    1. A Framework
    2. Let’s Get Clean
    3. Writing Good Tests
      1. Isolation
      2. Scope
      3. Defining Your Functions
        1. Comment blocks
        2. Tests
      4. Positive Testing
      5. Negative Testing
      6. Code Coverage
    4. Real-World Testing
      1. Dependencies
        1. Doubles
        2. Mock objects
        3. Stubs
        4. Spies
      2. Asynchronous Testing
        1. Using YUI Test
    5. Running Tests: Client-Side JavaScript
      1. PhantomJS
      2. Selenium
        1. Mobile
          1. iOS
          2. Android
    6. Running Tests: Server-Side JavaScript
      1. Jasmine
        1. Dependencies
        2. Spies
        3. Output
    7. Recap
  7. 5. Code Coverage
    1. Coverage Basics
    2. Code Coverage Data
    3. A Hands-on Example
      1. Instrumenting Files
      2. Anatomy of a Coveraged File
    4. Exercise/Deploy
      1. Client-Side JavaScript
      2. Server-Side JavaScript
    5. Persisting Coverage Information
      1. Unit Tests
      2. Integration Tests
    6. Generating Output
    7. Aggregation
    8. Hidden Files
    9. Coverage Goals
    10. Recap
  8. 6. Integration, Performance, and Load Testing
    1. Integration Testing
      1. Selenium
        1. WebDriver
        2. Remote Control
        3. Grid
      2. CasperJS
    2. Performance Testing
      1. Generating HAR Files
        1. Using a proxy
      2. Viewing HAR Files
      3. Browser Performance Testing
    3. Load Testing
      1. Browser Load Testing
    4. Tracking Resource Usage
      1. Client-Side Tracking
        1. Memory usage
        2. CPU usage
      2. Server-Side Tracking
        1. Programmatic heap snapshots
        2. Programmatic CPU usage
    5. Recap
  9. 7. Debugging
    1. In-Browser Debugging
      1. Firefox
      2. Chrome
      3. Safari
      4. Internet Explorer
    2. Node.js Debugging
    3. Remote Debugging
      1. Chrome
      2. PhantomJS
      3. Firefox
    4. Mobile Debugging
      1. Android 4
      2. iOS 6
      3. Adobe Edge Inspect
      4. Other Mobile Debugging Options
    5. Production Debugging
      1. Minified Code
      2. Source Maps
    6. Recap
  10. 8. Automation
    1. What to Automate
    2. When to Automate
    3. How to Automate
      1. Automating with Continuous Integration
      2. Automating the Development Environment
        1. The editor
        2. Unit tests
        3. Code reviews
        4. Commit hooks
        5. Other development tools
      3. Automating the Build Environment
        1. The build
        2. Jenkins
          1. Creating a Jenkins project
        3. Unit testing using Selenium
        4. Unit test output
        5. Coverage output
        6. Complexity
        7. JSLint
        8. Duplicate code
        9. Other analysis
        10. Notification
        11. Dependent builds
        12. The Full Monty
        13. Non-UI usage
      4. Deployment
    4. Recap
  11. Index
  12. About the Author
  13. Colophon
  14. Copyright