Jasmine/Protractor: stop test on failure in beforeEach

对着背影说爱祢 提交于 2019-12-03 12:26:45
Leo Gallucci

Strictly answering your question and without external dependencies:

beforeEach(function() {
    // 1) login user
    expect(1).toBe(1);
    // This works on Jasmine 1.3.1
    if (this.results_.failedCount > 0) {
        // Hack: Quit by filtering upcoming tests
        this.env.specFilter = function(spec) {
            return false;
        };
    } else {
        // 2) set some user properties
        expect(2).toBe(2);
    }
});

it('does your thing (always runs, even on prior failure)', function() {
    // Below conditional only necessary in this first it() block
    if (this.results_.failedCount === 0) {
        expect(3).toBe(3);
    }
});

it('does more things (does not run on prior failure)', function() {
    expect(4).toBe(4);
});

So if 1 fails, 2,3,4,N won't run as you expect.

There is also jasmine-bail-fast but I'm not sure how it will behave in your before each scenario.

jasmine.Env.prototype.bailFast = function() {
  var env = this;
  env.afterEach(function() {
    if (!this.results().passed()) {
      env.specFilter = function(spec) {
        return false;
      };
    }
  });
};

then just call:

jasmine.getEnv().bailFast();

(credit goes to hurrymaplelad who wrote an npm that does just that, however you don't need to use it)

jasmine-bail-fast does exactly what you did overriding the specFilter function, but does it on afterEach. So it will only fail after the first "it" is run. It won't help solving this specific case.

with jasmine2 we can set throwOnExpectationFailure to true.

For example in protractor config:

//protractor.conf.js
exports.config = {
  //...
  onPrepare: () => {
    jasmine.getEnv().throwOnExpectationFailure(true);
  }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!