Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop jasmine test after first expect fails

Tags:

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.

like image 987
leech Avatar asked Mar 01 '14 19:03

leech


People also ask

How do I ignore Jasmine test?

Excluding Tests / Specs If you want to exclude a specific test, simply use xit() instead of it() . The x means exclude. describe('description', function () { xit('description', function () {}); }); If you want to exclude an entire describe block, use xdescribe() instead of describe() .

Do Jasmine tests run in order?

Currently (v2. x) Jasmine runs tests in the order they are defined.

Why is Jasmine tested?

Jasmine follows Behavior Driven Development (BDD) procedure to ensure that each line of JavaScript statement is properly unit tested. By following BDD procedure, Jasmine provides a small syntax to test the smallest unit of the entire application instead of testing it as a whole.

Why use Jasmine for automated testing?

It provides utilities that can be used to run automated tests for both synchronous and asynchronous code. Jasmine has many features such as: It’s fast and has low overhead and no external dependencies.

How do I change the default location for Jasmine testing?

If you don’t use the default location for the jasmine.json configuration file, you simply need to specify the custom location via the jasmine --config option. You can find more CLI options from the official docs. In this section we’ll learn about the basic elements of Jasmine testing such as suites, specs, expectations, matchers and spies, etc.

How do I use Jasmine with Python?

You can also use Jasmine for testing your Python code with jasmine-py which can be installed from PyPI using the pip install jasmine command. This package contains both a web server that serves and executes a Jasmine suite for your project and a CLI script for running tests and continuous integrations.

What is the use of toThrow() and tothrowerror() matchers in Jasmine?

Jasmine provides the toThrow () and toThrowError () matchers to test for when an exception is thrown or to test for a specific exception, respectively. For example if we have a function that throws an TypeError exception: function throwsError() { throw new TypeError("A type error"); }


2 Answers

@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
like image 116
TachyonVortex Avatar answered Sep 21 '22 15:09

TachyonVortex


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.

like image 34
Gregg Avatar answered Sep 21 '22 15:09

Gregg