Mocha test: How to manually fail the test/ or assert without stopping the execution if the assertion fails?

2,495 views
Skip to first unread message

pataraut

unread,
Jul 24, 2015, 7:10:21 PM7/24/15
to Mocha
Hi
I have a Mocha test which performs multiple validation in a test. Something like this

test.it('multiple validation', function ()
{
check condition 1, if failed still want to go to next line
check condition 2
}

If I use Mocha assert or Chai assert on condition 1, if the assertion fails, it'll stop the execution and doesn't attempt to check condition 2.
If I catch the error thrown by check condition 1, then mocha doesn't know that the test fail.

So in summary:
Is there a call\or assertion library I can use to fail the mocha test without stopping the execution?

Thank you in advance for helps

Vlad GURDIGA

unread,
Jul 25, 2015, 5:27:02 AM7/25/15
to Mocha, pkulra...@gmail.com
Hi pataraut!

Yeah, because Mocha considers the test failed when it throws an (Assertion?) error, the normal execution flow is broken. So when I want to get that, I just put every assertion in its own it() call. Something like this:

describe('Validation', function() {
  it('isValidName', function() {
    expect(isValidName(1), 'rejects a number').to.be.false;
  });

  it('isValidAge', function() {
    expect(isValidAge(-42), 'rejects negative numbers').to.be.false;
  });  
});

Would that work in your context? 8-)

Christopher Hiller

unread,
Jul 25, 2015, 12:48:30 PM7/25/15
to moc...@googlegroups.com
Try to use one assertion per test instead


--
You received this message because you are subscribed to the Google Groups "Mocha" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mochajs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages