Chapter 2. Jasmine

What Is Jasmine?

Jasmine is a behavior-driven testing framework for JavaScript programming language. It’s a bunch of tools that you can use to test JavaScript code.

As you learned in the previous chapter, you can test your code against specifications that you write. If your code should work in a certain way, Jasmine helps you express that intention in code.

(By the way: if you’ve played around with RSpec for testing Ruby, Jasmine will look suspiciously familiar.)

Getting Set Up with Jasmine

Start by downloading the latest standalone release of Jasmine. Unzip it.

Note

Throughout this book, we’ll mostly be using browser-based Jasmine for various reasons. If you’d prefer a different environment (Node.js, Ruby/Rails, or other environments), take a look at Chapter 7, or the Jasmine wiki. These instructions are for a browser-based environment.

When you open SpecRunner.html in a web browser, you’ll see something like Figure 2-1.

First Jasmine specs
Figure 2-1. First time running Jasmine!

This file has run some example tests on some example code. It’s testing a Player and a Song. Whenever you want to run the tests, you simply need to load/reload this page.

In the src directory, you’ll see two things to be tested: a Player and a Song. The spec directory has tests for the Player. Taking a look inside the spec directory might help you understand Jasmine’s syntax (though there’s also ...

Get JavaScript Testing with Jasmine now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.