Book Image

Jasmine JavaScript Testing

By : Paulo Ragonha
Book Image

Jasmine JavaScript Testing

By: Paulo Ragonha

Overview of this book

<p>From a little renegade scripting language to the de facto standard platform of today, JavaScript has become a universal language available in the widest range of devices; it is truly the 'write once, run everywhere’ language. However, as JavaScript applications become more complicated, testing and applying sustainable software engineering practices also become mandatory.</p> <p>Jasmine JavaScript Testing is a practical guide to a more sustainable JavaScript development process. You will learn by example how to drive the development of a web application using tests and best practices.</p> <p>This book is about becoming a better JavaScript developer. So, throughout the chapters, you will not only learn about writing tests, but also about the best practices for writing software in the JavaScript language. This book is about acknowledging JavaScript as a real platform for application development and leveraging all of its potential. You will also learn about tooling and automation and how to make your life easier and more productive.</p> <p>You will learn how to create a sustainable codebase with the help of Jasmine. We will take a look at integrated testing (with a backend NodeJS server) and how you can speed this process up by faking AJAX requests. As you progress through the book, you will learn about the challenges of testing an application built on top of a framework and how you can prevent your application from suffering from dependency management hell. Also, since your applications need to get into production, you will learn about optimizing the code to reduce the number of requests the browser needs to make while loading your application.</p> <p>With this book, you will learn everything you need to know to become a real professional in the ever-demanding JavaScript universe.</p>
Table of Contents (16 chapters)

Jasmine and Behavior-Driven Development


Jasmine is a little Behavior-Driven Development framework, created by the guys 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, starting with what is a test unit.

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 Behavior-Driven Development (BDD), which is a rethink of Test-Driven Development (TDD).

In traditional unit testing practice, the developer is left with no guidelines on how to start in 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 like:

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

As you can see, this acceptance criterion is written following an underlying pattern:

  • given [an initial context]

  • when [an event occurs]

  • then [ensure some 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 above acceptance criterion written as a Jasmine test unit would be:

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.