12

Are tests in Jasmine 2.0 run in parallel? In my experience they aren't but the article , referenced by Jasmine.js: Race Conditions when using "runs" suggests that Jasmine does run them in parallel so I wondered if I was writing my tests incorrectly.

Here is a set of tests that I would expect to execute in 1 second instead of 4 seconds.

describe("first suite", function() {
  it("first test", function(done) {
    expect(true).toBeTruthy();
    setTimeout(done, 1000);
  });

  it("second test", function(done) {
    expect(true).toBeTruthy();
    setTimeout(done, 1000);
  });
});

describe("second suite", function() {
  it("first test", function(done) {
    expect(true).toBeTruthy();
    setTimeout(done, 1000);
  });

  it("second test", function(done) {
    expect(true).toBeTruthy();
    setTimeout(done, 1000);
  });
});

Am I missing something?

jsFiddle

Community
  • 1
  • 1
Spig
  • 458
  • 1
  • 5
  • 16
  • You might want to read [this discussion](http://stackoverflow.com/questions/2734025/is-javascript-guaranteed-to-be-single-threaded). I just ran the jsFiddle in Chrome, and it "finished in 4.012s". It may depend on which browser and how JS is implemented. – zerodiff Sep 03 '14 at 21:33
  • 1
    There's no reason Jasmine couldn't run async tests in parallel and remain single threaded. Here's a [fsFiddle](http://jsfiddle.net/dspigarelliMNDNT/vr5Larxx/) of what that might look like in theory. – Spig Sep 05 '14 at 14:11
  • jest runs your tests in parallel and actually uses jasmine2 as the runner. – Mrchief Mar 26 '18 at 02:38

2 Answers2

20

Jasmine does not actually run your specs in parallel in any way. It is however possible to have specs whose asynchronous portion takes long enough that the built-in time limit elapses which will cause jasmine to start running the next spec, even though there may still be code running from earlier specs.

Gregg
  • 2,628
  • 1
  • 22
  • 27
  • 6
    Thanks for the answer! Can you provide link to official jasmine documentation(I could not find one) ? I am interested to understand more, like what is the built-in time limit, how to change it etc. ? – Darshan Sep 25 '18 at 21:41
8

If you want to run your test in parallel and you are using karma as a test launcher, you can use karma-parallel to split up your tests across multiple browser instances. It runs specs in different browser instances and is very simple and easy to install:

npm i karma-parallel

and then add the 'parallel' to the frameworks list in karma.conf.js

module.exports = function(config) {
  config.set({
    frameworks: ['parallel', 'jasmine']
  });
};

karma-parallel

Disclosure: I am the author

Joel Jeske
  • 1,618
  • 14
  • 17