65

I can't find a way how to stop some part of it's from run if one of them failed

I'm using mocha-as-promised, so the code might look different from as usuall

describe("remote promises", function() {
  describe("browsing", function() {
    describe("getting page", function() {
      it("should navigate to test page and check title", function() {
        this.timeout(TIMEOUT);
        return browser.get("http://admc.io/wd/test-pages/guinea-pig.html").then(function() {
          return browser.title();
        }).then(function(title) {
          return assert.ok(~title.indexOf("I am a page title - Sauce Labs"), "Wrong title!");
        });
      })
      it("submit element should be clicked1", function() {
        this.timeout(TIMEOUT);
        return browser.elementById("submit").then(function(el) {
          return browser.clickElement(el);
        }).then(function() {
            return browser["eval"]("window.location.href");
          }).then(function(location) {
            assert.ok(~location.indexOf("http://"), "Wrong location!");
          });
      })
    });
    describe("clicking submit", function() {
      it("submit element should be clicked2", function() {
        this.timeout(TIMEOUT);
        return browser.elementById("submit").then(function(el) {
          return browser.clickElement(el);
        }).then(function() {
            return browser["eval"]("window.location.href");
          }).then(function(location) {
            assert.ok(~location.indexOf("http://"), "Wrong location!");
          });
      });
    });

  });
});

and i want that if should navigate to test page and check title is failed then submit element should be clicked1 should be skipped

EDIT: seems i'm just making my tests wrong, will wait for some time before deleting question

EDIT:

as i replied in comment - i already received this answer in mocha google groups, but there are some other restrictions i not mentioned in question - i'm using grunt-simple-mocha and as i inspected code - there is no bail option when i pass options to mocha constructor

i failed to find where are options from command line are passed to Suite instance, and the only line where it may be as i see it is a

suite.bail(this.bail());

which looks weird for me

i think i will open issue at mocha github pages, maybe they will extend passed options with bail setting later, or just explain me what i did wrong and how i can solve my problem in other way

EDIT: and now, according to https://github.com/visionmedia/mocha/commit/f0b441ceef4998e570a794dcff951bf2330eb0c5 latest Mocha have bail option from the box. Thanks to authors!

1
  • 1
    This question also is helpful if one searches for: mocha failfast or failure on first error.
    – k0pernikus
    Jun 2, 2017 at 13:31

1 Answer 1

137

Mocha supports bailing after the first test failure, is that what you want?

From mocha --help:

-b, --bail                      bail after first test failure
3
  • yes, thanks for answer. i already received this answer in mocha google groups page. i will accept it since it's correct answer to my original question, but i will append my question with short description what i actually wanted. maybe you can take a look and extend your answer, but seems mocha can't do what i want right now
    – llamerr
    Dec 21, 2012 at 9:00
  • 1
    now Mocha have bail option that can be passed via options too
    – llamerr
    Jan 2, 2013 at 7:59
  • As @llamerr mentioned, just put --bail inside a file called mocha.opts if you want this by default. Feb 10, 2015 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.