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

JavaScript Testing with Jasmine

Book Description

Get a concise introduction to Jasmine, the popular behavior-driven testing framework for JavaScript. This practical guide shows you how to write unit tests with Jasmine that automatically check for bugs in your application. If you have JavaScript experience—with knowledge of some advanced features—you’ll learn how to write specifications for individual components, and then use those specs to test the code you write.

Throughout the book, author Evan Hahn focuses primarily on methods for testing browser-based JavaScript applications, but you’ll also discover how to use Jasmine with CoffeeScript, Node.js, Ruby on Rails, and Ruby without Rails. You won’t find a more in-depth source for Jasmine anywhere.

  • Get an overview of both test-driven and behavior-driven development
  • Write useful specs by determining what you need to test—and what you don’t
  • Test the behavior of new and existing code against the specs you create
  • Apply Jasmine matchers and discover how to build your own
  • Organize code suites into groups and subgroups as your code becomes more complex
  • Use a Jasmine spy in place of a function or an object—and learn why it’s valuable

Table of Contents

  1. Preface
    1. Who Should Read This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. Safari® Books Online
    5. How to Contact Us
    6. Acknowledgments
  2. 1. Intro to Testing
    1. What Is Software Testing?
    2. Why Is It Useful?
    3. Test-Driven Development
    4. Behavior-Driven Development
  3. 2. Jasmine
    1. What Is Jasmine?
    2. Getting Set Up with Jasmine
    3. Testing Existing Code with describe, it, and expect
      1. An Example to Test
      2. Jasmine Time!
    4. Matchers
    5. Writing the Tests First with Test-Driven Development
  4. 3. Writing Good Tests
    1. Cardinal Rule: When in Doubt, Test
    2. Test Components
    3. Black-Box Testing
  5. 4. Matchers in Depth
    1. Equality: toEqual
    2. Identity: toBe
    3. Yes or No? toBeTruthy, toBeFalsy
    4. Negate Other Matchers with not
    5. Check If an Element Is Present with toContain
    6. Is It Defined? toBeDefined, toBeUndefined
    7. Nullness: toBeNull
    8. Is It NaN? toBeNaN
    9. Comparators: toBeGreaterThan, toBeLessThan
    10. Nearness: toBeCloseTo
    11. Using toMatch with Regular Expressions
    12. Checking If a Function Throws an Error with toThrow
    13. Custom Matchers
  6. 5. More Jasmine Features
    1. Before and After
    2. Nested Suites
    3. Skipping Specs and Suites
    4. Matching Class Names
  7. 6. Spies
    1. The Basics: Spying on a Function
    2. Calling Through: Making Your Spy Even Smarter
    3. Making Sure a Spy Returns a Specific Value
    4. Replacing a Function with a Completely Different Spy
    5. Creating a New Spy Function
    6. Creating a New Spy Object
  8. 7. Using Jasmine with Other Tools
    1. Jasmine and CoffeeScript
    2. Jasmine and Node.js
      1. Installing jasmine-node on Unix and Linux
      2. Installing jasmine-node on Windows
      3. Basic Usage
      4. Asynchronous Tests with jasmine-node
      5. jasmine-node and CoffeeScript
    3. Jasmine and Ruby on Rails
      1. Installation
      2. Usage
    4. Jasmine with Non-Rails Ruby
    5. More Tools
  9. 8. Reference
    1. Jasmine on the Web
    2. The Basic Structure of a Suite
    3. Matchers Reference
    4. List of Falsy Values
    5. Reserved Words in Jasmine
  10. About the Author
  11. Colophon
  12. Copyright