JSpec 1.1.4 - JavaScript BDD Testing Framework

What Is JSpec?

Well it is a JavaScript Testing Framework taking the form of BDD (Behavior Driven Development). BDD is simply a development technique in which you write tests (specifications), documenting how the implementation of a routine should function.

For example lets say we wish to write the method isEmpty() which should check if an array has any values. The first step would to write its specification BEFORE we write the implementation. The spec may look something like this:

describe 'Array'
  describe '.isEmpty()'
    it 'should return true when the array is empty'
      [].isEmpty().should.be_true
    end

    it 'should return false when elements are present'
      [1,2].isEmpty().should.be_false
    end
  end
end

Installation

JSpec is distributed as a Ruby gem, however if you wish to include it directly within your source tree, you may simply visit http://visionmedia.github.com/jspec and download the zip or tarball.

To install as a gem simply execute the following command:

$ sudo gem install visionmedia-jspec --source http://gems.github.com

Getting Started

To create a new project template simply execute the following command:

$ jspec init myProject

We will now have a template initialized within the ./myProject directory matching the structure below (alter as you like).

.
|___History.rdoc         Change log
|___lib                  All of your library JavaScript
| |___yourlib.core.js   
|___README.rdoc          Readme file
|___spec                 Specification suite(s)
  |___spec.core.js       Spec for yourlib.core.js
  |___spec.html          HTML to run specs with DOM formatter

Automated Testing

JSpec is packaged with auto-testing functionality, which will run your spec suites whenever they, or your library code is altered. At the root of your project directory execute the following command to being auto-testing:

$ jspec

You should see a message similar to "Binding to lib/yourlib.core.js, spec/spec.core.js, watching change every 1 second(s).", indicating we have begun auto-testing these files.

To run auto-test using several browsers execute the following:

$ jspec --browsers Safari,Firefox

Now open up either spec/spec.core.js or lib/yourlib.core.js in the text editor of your choice, and save the document. JSpec will now run the suites within the browser(s) specifed, using the DOM formatter by default which produces the following output:

DOM Formatter Output

Writing Your First Specification

Lets go ahead and write our Array#isEmpty() method, but first we need to copy the spec we wrote at the beginning of this tutorial, and paste it into spec/spec.core.js. Once saved you will see that our new code simply fails due to the isEmpty() method not existing, so lets open up lib/yourlib.core.js and write the implementation below:

Array.prototype.isEmpty = function() {
  return this.length == 0
}

Once saved the suites will run again, producing the output below indicating that both of our specifications have passed.

Passing Specs

Refactoring Your Code

Here is where behavior driven development really shines; refactoring. Refactoring is simply the act of re-writing internal code, without altering external APIs. This is done for several reasons, for example to increase performance of internal code, improve readability, or for security reasons. Specifications help reassure that your code is functioning as you originally intended.

When a specification fails JSpec will certainly let you know about it, displaying a assertion failure message like below:

JSpec Assertion Failure

Matchers

Matchers are used to make an assertion on how a routine should function. JSpec has more core matchers than most JavaScript testing frameworks, ranging from jQuery DOM related matchers, composite object matchers for arrays and objects, or scalar matchers for ints, strings, floats, etc.

Below are a few matcher examples, to create custom matchers for your library, or to read more about the core JSpec matchers available consult "Additional Information" at the end of this tutorial.

person = { pets : ['foo', 'bar', 'baz'] }
person.pets.should.be_an(Array)
person.should.have(3, 'pets')
person.pets.should.include('foo', 'bar')
person.pets.should.not.include('rawr')

Features As Of JSpec 1.1.4

Below is a list of major features currently supported:

  • Highly readable syntax
  • Framework / DOM independent
  • Nested describes
  • Does not pollute core object prototypes
  • Async support
  • Extremely simple and intuitive matcher declaration
  • Over 45 core matchers
  • Allows parens to be optional when using matchers to increase readability
  • Several helpful formatters (dom, console, terminal, ...)
  • Assertion graphs displaying how many, and which assertions pass or failed
  • Default / customizable evaluation contexts
  • DOM sandbox support
  • Optionally display failures only, speeding up development
  • Great looking default DOM theme
  • Profiling
  • Tiny (15 kb compressed, 1000-ish LOC)

Additional Information

For more information on matchers, the JSpec syntax, TextMate bundles and more visit http://visionmedia.github.com/jspec/

Comments

The issue is the default browser, you can use $ jspec --browsers browsername. Windows support is a bit poor at the moment unfortunately, I despise windows myself but I welcome any patches.

Hi, very interesting library. I got everything to work except for the auto testing; it fails with can't run applescript on i686-pc-mswin32.

Is it possible to use the auto testing on mswin?

KR,
Tom