How can I skip rest of the tests if some of them fails?

3,590 views
Skip to first unread message

Алексей Буряков

unread,
Dec 19, 2012, 9:46:49 AM12/19/12
to moc...@googlegroups.com
I'm new to Mocha and testing at all, so maybe my question may look dumb, but how can I skip tests if one of them fail?

for example in this test.js, if should work with objects fails how i can prevent other it's from running and skip to next describe?

describe('test', function(){
  it('should work with objects', function(){
    var a = { name: 'tobi', age: 2, species: 'ferret' };
    var b = { name: 'jane', age: 8, species: 'ferret' };
    a.should.eql(b);
  })

  it('should work with arrays', function(){
    var a = [1,2,{ name: 'tobi' },4,5]
    var b = [1,2,{ name: 'jane' },4,4, 'extra stuff', 'more extra']
    a.should.eql(b);
  })

  it('should work with strings', function(){
    'some\nfoo\nbar'.should.equal('some\nbar\nbaz');
  })
})

vision media [ Tj Holowaychuk ]

unread,
Dec 19, 2012, 9:55:44 AM12/19/12
to moc...@googlegroups.com
there's a flag called --bail which will exit after the first error

Алексей Лукин

unread,
Dec 19, 2012, 10:02:20 AM12/19/12
to moc...@googlegroups.com, t...@vision-media.ca
but it seems to exit at all, and not skipping to next describe, if i have this code, i want it to skip should2 when should1 fails, but run test2
can i do this in some way? maybe not using pure mocha?

describe('test1', function(){
  it('should1', function(){

    var a = { name: 'tobi', age: 2, species: 'ferret' };
    var b = { name: 'jane', age: 8, species: 'ferret' };
    a.should.eql(b);
  })
  it('should2', function(){

    var a = { name: 'tobi', age: 2, species: 'ferret' };
    var b = { name: 'jane', age: 8, species: 'ferret' };
    a.should.eql(b);
  })

})

describe('test2', function(){

  it('should work with objects', function(){
    var a = { name: 'tobi', age: 2, species: 'ferret' };
    var b = { name: 'jane', age: 8, species: 'ferret' };
    a.should.eql(b);
  })
})

среда, 19 декабря 2012 г., 16:55:44 UTC+2 пользователь vision media [ Tj Holowaychuk ] написал:

Алексей Лукин

unread,
Dec 19, 2012, 10:10:04 AM12/19/12
to moc...@googlegroups.com, t...@vision-media.ca
or maybe i misunderstanding how to use it, and instead of doing this i must do something else?
for example: i have some blog tests
1. open editor page
2. enter title and save it
3. enter image and save post again
4. enter content and save it again
and when one of the steps fails i don't want to run others

seems i just coupling tests to tight and need to split them to that point at which i can exit single test if it fails?

vision media [ Tj Holowaychuk ]

unread,
Dec 19, 2012, 10:12:36 AM12/19/12
to moc...@googlegroups.com
currently there's only the option to bail after _any_ failure, or mocha will just continue on to the next test, there's no concept of skipping the rest of a given suite

Алексей Лукин

unread,
Jan 2, 2013, 2:55:48 AM1/2/13
to moc...@googlegroups.com
So now it has bail option too, according to https://github.com/visionmedia/mocha/commit/f0b441ceef4998e570a794dcff951bf2330eb0c5

Many thanks!

Dmitriy Navrotskyy

unread,
Apr 25, 2013, 6:35:01 PM4/25/13
to moc...@googlegroups.com
@Alex, this is the option to bail after first/any failure.

+1 for the bail-after-some-enhancement

Johan Sundström

unread,
Aug 14, 2013, 4:29:52 PM8/14/13
to moc...@googlegroups.com
On Wednesday, 19 December 2012 06:46:49 UTC-8, Алексей Лукин wrote:
I'm new to Mocha and testing at all, so maybe my question may look dumb, but how can I skip tests if one of them fail?

for example in this test.js, if should work with objects fails how i can prevent other it's from running and skip to next describe?

I just needed this too, and a variant on the same that does not proceeding down nested describe:s if some test above fails, so I made a pull request https://github.com/visionmedia/mocha/pull/951 which gets you this:

it.skipsOnFailure('should work with objects', function(){ ... });

...which skips past all subsequent it:s in that describe, and a

describe.skipsOnFailure('test', function(){ ... });

...which stops recursing into sub-describe:s, as soon as any of its it:s has failed.

The PR is not yet accepted or merged, but I am pretty sure at least the first is a good API.

The second feature may be too, though I can see that it could be useful to also come with some it.skipsDescendantDescribesOnFailure or similar too, when you want sub-sections to get skipped only based on some particular test / tests. The naming of either of the above is probably still a little too verbose, but I figured I'd air this here where other interested parties might be listening in for feedback.

-- 
 / Johan Sundström, http://ecmanaut.blogspot.com/
Reply all
Reply to author
Forward
0 new messages