In migrating our test suite to Jasmine 2.0, we ran into an issue where tests would fail because they all interact with some singleton, which unfortunately had state that was being mucked with other tests before any one had finished. Though Mocha had some of the features we wanted (including running tests in series), refactoring wasn’t quite practical.

In our previous suite, verifying specs was be done in series. Jasmine 2.0 doesn’t provide that ability natively (as far as I know), but its not hard to achieve. There are probably several ways of doing this, but combining Jasmine 2.x’s support of async tasks and Promises provide a simple way of chaining everything together.

Kick off the chain with a resolved promise, and then use the done callback in a beforeEach clause to chain the next test, preventing the test from beginning until the previous one has resolved completed.

The skeleton of the spec looks like:

```` describe(‘My Test Suite’, function() {

/* Basic suite setup */

// Promise start point var priorTestPromise = Promise.resolve();

beforeEach(function(done) {

// queue this task to run sequentially after the last,
// moving forward will be blocked by the afterEach
priorTestPromise.then(function () {

  /* Test setup must go in promise chain */

  return new Promise(done);
});

});

/* All the specs */

it(‘should do something’, function (done) {

/* spec code, potentially async */

done();

});

});

````

One advantage of putting the chaining in the beforeEach is that each test doesn’t need to know about the chaining, and can still run synchronously. However, if the test is async, you can use Jasmine’s done() callback and block execution of specs down the line.

Note that (in the near future) if you’re running in Phantom, you’ll need to include a Promise polyfill, and in node you’ll need to use the --harmony flag.