Brianjs
Brianjs

Reputation: 2239

Stop execution in current IT on failed expect but continue spec tests in Jasmine

Currently, if an expect() fails, execution of the test continues. This causes an exception to be thrown and the build to crash (in the example).

Using "stopSpecOnExpectationFailure" : true causes the entire spec to stop execution on the first expect() failure.

Is there a way to stop execution and fail the current it() while jasmine continues to execute the rest of the it() functions in the spec?

I am looking for a way to accomplish the example below.

Example:

it('should fail and stop on first expect', function() {
  var test;
  expect(test).toBeDefined(); //fail test and stop execution of this it() only.
  test.body; //stopping would prevent an exception being thrown
});

it('should run this test still', function() {
  expect(true).toBe(true);
});

Upvotes: 1

Views: 1197

Answers (1)

PeterS
PeterS

Reputation: 2954

Jasmine doesn't really work like that the expectations are exactly that - what the test clause should expect. It's not going to give you branching or conditional functionality.

Your really only sensible way of doing this is :

  expect(test).toBeDefined(); //fail test and stop execution of this it() only.

  if (test) {
     test.body; //stopping would prevent an exception being thrown
  }

This will fail the expectation if test isn't there and not fail. It will continue if it is. Like you said, you don't want to stop everything with the other Jasmine directive.

You could also use the fail statement, and this may appear tidier:

if (test) {
     test.body
} else {
     fail('Hey your test object is null!');
}

Upvotes: 2

Related Questions