Stop jasmine test after first expect fails
Asked Answered
H

5

21

I'm familiar with python unittest tests where if an assertion fails, that test is marked as "failed" and it moves on to other tests. Jasmine on the other hand will continue through all expects even if the one of them fails. How can I make Jasmine stop processing a test after the first expectation fails?

it ("shouldn't need to test other expects if the first fails", function() {
    expect(array.length).toBe(1);

    // don't need to check this if the first failed.
    expect(array[0]).toBe("foo");
});

Am I thinking about it wrong? I have some tests with lots of expect's and it seems like a waste to show all the stack traces when only the first is wrong really.

Hartmann answered 1/3, 2014 at 19:25 Comment(2)
If you have so much asserts per test, your code is testing too much each time. It may be easier to develop this way, but it is much harder to know where the problem is when the tests fail in the future (also, it kind of is the point of tests, no? To tell the future developer s/he created a bug). I kind of find it useful that Jasmine doesnt stop on the first expect, it shows everything went wrong instead of what went wrong first. Anyway, as for the question per se, I have looked a bit and haven't found anything to make it quit after the first fail.Passe
Keep in mind though that in this particular example, the first assertion (array length) is redundant: you don't need to check the exact array length if you are then also checking the exact elements anyways (because it's inherently implied by how arrays work). In JS, the only exception to this is when an array's length is set manually and thus it gets padded with undefined elements, in which case you could either assert that none of the elements are undefined, or that the array's length is not greater than a specific number. This way, assertions will complete each other.Interference
A
13

@Gregg's answer was correct for the latest version of Jasmine at that time (v2.0.0).

However, since then, this new feature was added in v2.3.0:

Allow user to stop a specs execution when an expectation fails (Fixes #577)

It's activated by adding throwFailures=true to the query string of the runner page, eg:

http://localhost:8000/?throwFailures=true
Adiaphorism answered 4/8, 2015 at 12:25 Comment(2)
To use the new feature in node.js, set "stopSpecOnExpectationFailure": true on the top level of jasmine.json or run jasmine --stop-on-failure=true (confusing just --stop-on-failure is silently ignored). If given, flag overrides config.East
@beni-cherniavsky-paskin @tachyonvortex but using stopSpecOnExpectationOnFailure stops the entire set of tests... there is a way to ONLY stop a single it (test)?Lulalulea
R
8

Jasmine doesn't support failing early, in a single spec. The idea is to give you all of the failures in case that helps figure out what is really wrong in your spec.

Rehabilitation answered 2/3, 2014 at 4:31 Comment(1)
What do you suggest in situations like OPs? Should we check with an if after expect(array.length).toBe(1); and return early?Hexamethylenetetramine
H
1

Jasmine has stop on failure feature and you can check it here: https://plnkr.co/plunk/Ko5m6MQz9VUPMMrC

This starts jasmine with oneFailurePerSpec property.

enter image description here enter image description here enter image description here

Hilliard answered 24/12, 2020 at 16:17 Comment(0)
S
0

According to the comments of https://github.com/jasmine/jasmine/issues/414 I figured out that 2 solutions exists for this: https://github.com/radialanalytics/protractor-jasmine2-fail-whale https://github.com/Updater/jasmine-fail-fast

I just started to use the protractor-jasmine2-fail-whale because it seems to have more features. Although to take screenshots in case of test failures I currently use protractor-jasmine2-html-reporter.

Scaife answered 26/4, 2017 at 17:52 Comment(0)
C
0

I'm using Jasmine in Appium (a tool to test React Native apps).

I fixed the issue by adding stopSpecOnExpectationFailure=true to Jasmine configs

jasmine v3.8.0 & jasmine-core v3.8.0

Chiffon answered 22/7, 2021 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.