Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Simple, flexible asynchronous node.js testing framework.
JavaScript
branch: clean

This branch is 1 commit ahead of jdeal:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
examples
lib
README.md
package.json

README.md

Spectra

Simple, flexible, asynchronous TDD/BDD framework for node.js.

Usage

The simplest spec module:

That's right, an empty module. An empty module is a failing test. So no excuses for not starting with a failing test first!

To do something useful, the simplest spec is:

module.exports = function (spec){
  spec.equal(2+2, 4);
  spec();
}

Note that each spec function is passed a spec function/object, which it can treat as the assert object and also call when finished. Because of the explicit callback, asynchronous functions can easily be tested, and setup work can be done ahead of test execution.

If a map is passed to the spec object, it represents a nested spec:

module.exports = function (spec){
  spec({
    'math': function (spec){
      spec({
        '2 + 2 should equal 4': function (spec){
          spec.equal(2+2, 4);
          spec();
        }
      });
    }
  });
}

You can nest as many specs as you like. Because the specs are functions, it's easy to reference variables in outer specs.

You can also define the outermost specs like this:

exports['first test'] = function (spec){
  // test something
}

exports['second test'] = function (spec){
  // test something else
}

But then you'll lose the ability to do asynchronous setup work for your outermost specs.

Multiple callbacks

If you have multiple callbacks, you can state that with a "when" assertion:

module.exports = function (spec){
  spec.when('sooner','later');
  setTimeout(function(){
    spec('sooner');
  },1000);
  setTimeout(function(){
    spec('later');
  },2000);
};
Something went wrong with that request. Please try again.