Running Specs in Parallel

Support for parallel execution in Node.js is available in 5.0.0 and later releases. You can run your Jasmine specs in parallel if all the following apply:

To use parallel mode:

  1. Read the lists of limitations and bugs below and make sure none of them apply to you. In particular, make sure that you don’t have to disable randomization, specify a random seed, or specify a custom order to make your suite run reliably.
  2. Set your jasmine dependency to 5.0.0 or later.
  3. Add the --parallel=<n> argument to your jasmine command, e.g. jasmine --parallel=4 to run specs in four parallel processes.

The ideal number of workers depends on the nature of your specs and the number and type of available CPU cores. CPU bound suites often run fastest if the number of workers is slightly less than the number of cores. I/O bound suites may benefit from a lower number of workers.

Limitations

The following limitations apply only to parallel mode, not to the normal sequential execution mode.

New global setup/teardown API

Jasmine 5.0 offers a new API for exactly-once global setup and teardown. It is intended to support parallel execution scenarios that require out-of-process setup to be done once regardless of the number of workers. It can also be used in the default sequential execution mode.

To use the global setup/teardown API, add globalSetup and/or globalTeardown functions to your Jasmine configuration, e.g.:

module.exports = {
  spec_dir: "spec",
  // ...
  globalSetup() {
    // ... your setup code here
  }
  globalTeardown() {
    // ... your teardown code here
  }
}

If a global setup/teardown function is declared async or otherwise returns a promise, Jasmine will wait for the returned promise to be resolved before continuing. Callback-style async is not supported.

Unsure which setup/teardown tool to use?

Differences to be aware of

Updating reporters to work in parallel mode

Parallel mode imposes a number of limitations on reporters. Since some of the most popular third-party reporters will need some changes to work in parallel mode, Jasmine assumes that a reporter is incompatible with parallel mode unless it declares compatibility. A reporter can declare that it is compatible with parallel mode by exposing a reporterCapabilities property with value {parallel: true}.

Event order

The biggest limitation to be aware of is that events are delivered as they are received from worker processes. That means that events for unrelated specs and suites can interleave. A reporter that assumes that a specDone event is for the same spec as the most recently received specStarted event, or that all events between a suiteStarted and suiteDone event relate to children of that suite, will not work in parallel mode. Jasmine makes only the following guarantees about event ordering in parallel mode:

In Jasmine 4.6 and later, suite and spec reporter events have a parentSuiteId property that allows reporters to identify a spec or suite’s parent suite without relying on event order.

Concurrent event dispatch

In sequential mode, Jasmine waits for asynchronous reporter functions to complete before continuing. In parallel mode, it does not. Async reporters should either be prepared to process events concurrently or queue them up internally.

API differences

In addition, several APIs used by reporters are unavailable or limited:

Note that the above applies only to parallel mode. In the default sequential mode, Jasmine 5 is fully compatible with existing reporters.

Programmatic usage

You can run specs in parallel programmatically using the ParallelRunner class.

const ParallelRunner = require('jasmine/parallel');
const runner = new ParallelRunner({numWorkers: 3});
runner.loadConfigFile('spec/support/jasmine.json');
runner.execute();

ParallelRunner supports many of the same methods as the Jasmine class. See the API reference documentation for details.