Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jasmine2: how to handle failing beforeAll

My jasmine2/protractor test look like this

var testUserId = null;

describe("user test", function() {
  beforeAll(function(done) {
    createTestUser().
      .then(function(userId){testUserId = userId})
      .then(done)
      .catch(done.fail);

  it("should do stuff with the test user", function(done) {
    // bla bla
  });

  afterAll(function(done) {
    deleteTestUser(testUserId).
      .then(done)
      .catch(done.fail);
  });

})

createTestUser and deleteTestUser return promises. If a problem occur they reject with an error message. May Problem is now, that the tests get started even if the error occurs in beforeAll. And I get

Failures:
1) should do stuff with the test user
Message: [my error message from beforeAll]

If there are many tests it tries to perform all of them and fail with the very sam error message. Is it possible to prevent it from executing the tests if the beforeAll function fails?

Thx!

("jasmine-core": "2.8.0", "protractor": "5.2.1")


Edit:

It's not exactly what I was asking for but at least I found a solution to keep the amount of error messages close like this:

var testUserId = null;
describe("user test", function() {
  beforeAll(function(done) {
    createTestUser().
      .then(function(userId){testUserId = userId})
      .then(done)
      .catch(done.fail("test user could not be created"));

  it("should do stuff with the test user", function(done) {
    if (testUserId) {
      // bla bla
    } else {
      done();
  });

This way I get a least only the correct error message ("test user could not be created") and not the ones of the unmet expectations in "// bla bla" (Naturally I still get one for each it, but whatever). I also wrapped the functions in a function-factory so that I don't have to write the if-condition everytime.

like image 813
Paflow Avatar asked Dec 18 '17 16:12

Paflow


1 Answers

From what I understand, jasmine does not support that despite the attention:

Skipping out in the middle of the spec run (this issue) is a bit more complicated, because depending on the type of error, Jasmine probably still needs to run any afterEach (or afterAll depending) to cleanup state for the next spec. This would then require the QueueRunner to know which of the functions it is given are setup and teardown and not just have a list of functions to call.

One option would be to use the "fail fast" option which can be done by using one of the third-party libraries like jasmine-fail-fast or protractor-jasmine2-fail-whale.

There are though some other workarounds, like manually checking if there was a preceding failure in the it() functions:

  • Jasmine/Protractor: stop test on failure in beforeEach
like image 123
alecxe Avatar answered Oct 17 '22 18:10

alecxe