Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Jasmine Tests Sequentially

I'm using Karma/Jasmine to run many spec files. My tests make use of some global functions. Some tests mock up the global functions, which other tests depend on. Since the tests are being run asynchronously, some tests fail since the expected behaviour of the global functions is changed by other tests.

Is there a way to run the tests sequentially?

like image 642
user8709724 Avatar asked Mar 02 '18 21:03

user8709724


2 Answers

I don't think that async tests will run two tests at the exact same time. This side effect is most likely happening because you are not reseting your global functions after the individual test ends. If you don't restore the global function after each test and the next test that runs (which could be any individual test in your suite) could fail if it relies on the same function.

For example (using sinon)

describe("A suite", function() {
    beforeEach(function() {
      sinon.stub(someGlobal, 'someFunc')
    });

    afterEach(function() {
      someGlobal.sumFunc.restore()
    })

    it("uses global function", function() {
      ...
    });
});

*You can, however, set random to false in your jasmine config to run your specs in order - https://jasmine.github.io/setup/nodejs.html.

like image 165
Tyler Avatar answered Oct 30 '22 23:10

Tyler


set "random" to false in jasmine.json

The file should be added in spec/support/jasmine.json

like image 40
Sergey Tokarev Avatar answered Oct 30 '22 23:10

Sergey Tokarev