concept Jasmine in category BDD

appears as: Jasmine
BDD in Action: Behavior-Driven Development for the whole software lifecycle

This is an excerpt from Manning's book BDD in Action: Behavior-Driven Development for the whole software lifecycle.

Unit testing is well supported in JavaScript, and low-level BDD unit-testing libraries like Jasmine and Mocha are widely used. We’ll look at these libraries in chapter 10, when we discuss BDD-style unit and integration testing. But in this chapter we’ll focus on options available for higher-level BDD in JavaScript, using Gherkin-style scenario definitions.

The result of this formalization is that a number of more BDD-flavored unit-testing tools have emerged over recent years that make these techniques easier and more intuitive to practice. Tools like RSpec, NSpec, Spock, and Jasmine have flourished over recent years. While there’s no obligation to use these tools (many of our examples will be written simply using JUnit), they can make it easier to write more concise, more expressive, low-level executable specifications.

Although more traditional unit-testing tools such as QUnit are widely used in the JavaScript world, there are several BDD-style JavaScript unit-testing libraries around. The best known of these are Jasmine (http://jasmine.github.io) and Mocha (http://visionmedia.github.io/mocha/). As far as BDD goes, both these libraries have a very similar style. You’ve already seen Jasmine in action in chapter 9, where you used it to test an AngularJS web application. Let’s take a closer look at how it works.

Jasmine lets you describe granular requirements in a format similar to RSpec. It supports declarative, example-based specifications, nested contexts, and its own mocking library. Here’s a simple example of some requirements expressed using Jasmine:

As you can see, Jasmine specifications have a layout that’s very similar to the layout of RSpec specifications. Groups of related specifications are marked by the describe function , which can contain specifications or more nested describe functions . Each specification is marked by the it method , which is made up of a title and a function containing the specification test code. Jasmine uses the expect() function to describe expected outcomes, though as you’ll see in section 10.5.2, this isn’t your only option.