Book Image

Jasmine JavaScript Testing Update

By : Paulo Vitor Zacharias Ragonha
Book Image

Jasmine JavaScript Testing Update

By: Paulo Vitor Zacharias Ragonha

Overview of this book

Table of Contents (15 chapters)

Jasmine and behavior-driven development


Jasmine is a little behavior-driven development (BDD) test framework created by the developers at Pivotal Labs, to allow you to write automated JavaScript unit tests.

But before we can go any further, first we need to get some fundamentals right, starting with what a test unit is.

A test unit is a piece of code that tests a functionality unit of the application code. But sometimes, it can be tricky to understand what a functionality unit can be, so for that reason, Dan North came up with a solution in the form of BDD, which is a rethink of test-driven development (TDD).

In traditional unit testing practice, the developer is left with loose guidelines on how to start the process of testing, what to test, how big a test should be, or even how to call a test.

To fix these problems, Dan took the concept of user stories from the standard agile construct, as a model on how to write tests.

For example, a music player application could have an acceptance criterion such as:

Given a player, when the song has been paused, then it should indicate that the song is currently paused.

As shown in the following list, this acceptance criterion is written following an underlying pattern:

  • Given: This provides an initial context

  • When: This defines the event that occurs

  • Then: This ensures an outcome

In Jasmine, this translates into a very expressive language that allows tests to be written in a way that reflects actual business values. The preceding acceptance criterion written as a Jasmine test unit would be as follows:

describe("Player", function() {
  describe("when song has been paused", function() {
    it("should indicate that the song is paused", function() {

    });
  });
});

You can see how the criterion translates well into the Jasmine syntax. In the next chapter, we will get into the details of how these functions work.

With Jasmine, as with other BDD frameworks, each acceptance criterion directly translates to a test unit. For that reason, each test unit is usually called a spec, short for specification. During the course of this book, we will be using this terminology.