Evan Hahn
JavaScript Testing with Jasmine
Downloa d f r o m W o w ! e B o o k < w w w.woweb o o k . c o m >
JavaScript Testing with Jasmine
by Evan Hahn
Copyright © 2013 Evan Hahn. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editor: Mary Treseler
Production Editor: Marisa LaFleur
Proofreader: Rachel Monaghan
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Rebecca Demarest
March 2013: First Edition
Revision History for the First Edition:
2013-03-22: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449356378 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. _JavaScript Testing with Jasmine_, the image of a phoebe, and related trade dress are trademarks
of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-35637-8
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
1.
Intro to Testing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Software Testing? 1
Why Is It Useful? 2
Test-Driven Development 2
Behavior-Driven Development 2
2.
Jasmine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
What Is Jasmine? 5
Getting Set Up with Jasmine 5
Testing Existing Code with describe, it, and expect 6
An Example to Test 6
Jasmine Time! 7
Matchers 8
Writing the Tests First with Test-Driven Development 9
3.
Writing Good Tests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Cardinal Rule: When in Doubt, Test 13
Test Components 13
Black-Box Testing 14
4.
Matchers in Depth. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Equality: toEqual 15
Identity: toBe 15
Yes or No? toBeTruthy, toBeFalsy 16
Negate Other Matchers with not 17
Check If an Element Is Present with toContain 17
Is It Defined? toBeDefined, toBeUndefined 18
iii
Nullness: toBeNull 18
I
s It NaN? toBeNaN 18
Comparators: toBeGreaterThan, toBeLessThan 19
Nearness: toBeCloseTo 19
Using toMatch with Regular Expressions 20
Checking If a Function Throws an Error with toThrow 20
Custom Matchers 20
5. More Jasmine Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
B
efore and After 23
Nested Suites 24
Skipping Specs and Suites 24
Matching Class Names 25
6. Spies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
The Basics: Spying on a Function 27
Calling Through: Making Your Spy Even Smarter 29
Making Sure a Spy Returns a Specific Value 30
Replacing a Function with a Completely Different Spy 30
Creating a New Spy Function 30
Creating a New Spy Object 31
7. Using Jasmine with Other Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
J
asmine and CoffeeScript 33
Jasmine and Node.js 34
Installing jasmine-node on Unix and Linux 34
Installing jasmine-node on Windows 34
Basic Usage 34
Asynchronous Tests with jasmine-node 35
jasmine-node and CoffeeScript 35
Jasmine and Ruby on Rails 36
Installation 36
Usage 36
Jasmine with Non-Rails Ruby 37
More Tools 37
8. Reference. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
J
asmine on the Web 39
The Basic Structure of a Suite 39
Matchers Reference 40
List of Falsy Values 40
Reserved Words in Jasmine 40
iv | Table of Contents
Downloa d f r o m W o w ! e B o o k < w w w.woweb o o k . c o m >
Preface
All programmers want their code to work the way they intended. Jasmine, a popular
testing framework for the JavaScript programming language, allows you to achieve that
goal. Through coded specifications, Jasmine helps make your JavaScript work exactly
how it’s supposed to. In this book, we’ll explore Jasmine in detail, from its basic concepts
to its advanced features.
This book aims to explain the concepts of testing and test-driven development, as well
as why they’re useful. It then aims to dive into Jasmine and explain how it can help
programmers test their JavaScript code. By the end of this book, I aim to give readers
an understanding of Jasmine’s concepts and syntax.
Who Should Read This Book
This book is intended for programmers who are familiar with some more advanced
JavaScript features, such as closures and callbacks, and who have a general understand‐
ing of JavaScript’s prototype system. If you are interested in learning how to write reliable
JavaScript code, this is the book for you.
Jasmine is useful when building a maintainable and scalable JavaScript application, ei‐
ther in a browser or on a server. It can help ensure that a browser’s client-side data
models are performing properly, or that a server is correctly serving pages.
Jasmine is also useful for building reliable JavaScript libraries. It can help ensure that
the exposed API of your library matches what you intend it to match.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions.
v