Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Mocha to fail a test

I have the following test:

it.only('validation should fail', function(done) {     var body = {         title: "dffdasfsdfsdafddfsadsa",         description: "Postman Description",         beginDate: now.add(3, 'd').format(),         endDate: now.add(4, 'd').format()     }       var rules = eventsValidation.eventCreationRules();     var valMessages = eventsValidation.eventCreationMessages();      indicative         .validateAll(rules, body, valMessages)         .then(function(data) {             console.log("SHOULD NOT GET HERE");             should.fail("should not get here");             done();          })         .catch(function(error) {             console.log("SHOULD GET HERE");             console.log(error);         });     done(); }); 

The test execution path is correct. When I have validating data, it goes to "SHOULD NOT GET HERE". The test is really to make sure it doesn't. And when I put in non validating data the code does go to "SHOULD GET HERE". So the validation rules work.

What I'm trying to do is make sure the test fails when when I have bad validation data and it validates. However when I run it as it is with good data it validates, runs the fail, but mocha still marks it as the passing. I want it to fail if the execution gets to "SHOULD NOT GET HERE".

I've tried throw new Error("fail"); as well with no luck. In both cases it actually seems to run the code in the .catch block as well.

Any suggestions? I found solutions for this in a similar question. This question is written because those solutions don't seem to be working for me.

like image 372
Pompey Magnus Avatar asked Jun 16 '15 17:06

Pompey Magnus


People also ask

Is Mocha a runner test?

Mocha is one of the most popular testing frameworks for JavaScript. In particular, Mocha has been the test runner of choice in the Node.

What does it mean in Mocha test?

Mocha is a feature-rich JavaScript test framework running on Node. js and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases.

Do Mocha tests run in order?

Mocha will run the tests in the order the describe calls execute.

Does Mocha run tests in parallel?

Mocha does not run individual tests in parallel. That means if you hand Mocha a single, lonely test file, it will spawn a single worker process, and that worker process will run the file. If you only have one test file, you'll be penalized for using parallel mode. Don't do that.


2 Answers

You can call assert.fail:

it("should return empty set of tags", function() {     assert.fail("actual", "expected", "Error message"); }); 

Also, Mocha considers the test has failed if you call the done() function with a parameter.

For example:

it("should return empty set of tags", function(done) {     done(new Error("Some error message here")); }); 

Though the first one looks clearer to me.

like image 80
Daniel Avatar answered Sep 19 '22 10:09

Daniel


In the ES2017 async/await world chai-as-promised is not needed as much. Although simple rejections are one place chai-as-promised remains a little neater to use.

A catch is required if you want to test the error in more detail.

it.only('validation should fail', async function(){     let body = { ... }     let rules = eventsValidation.eventCreationRules()     let valMessages = eventsValidation.eventCreationMessages()      try {         await indicative.validateAll(rules, body, valMessages)     } catch (error) {         expect(error).to.be.instanceOf(Error)         expect(error.message).to.match(/Oh no!/)         return     }     expect.fail(null, null, 'validateAll did not reject with an error')     // or throw new Error('validateAll did not reject with an error') }) 

async/await requires Node.js 7.6+ or a compiler like Babel

like image 20
Matt Avatar answered Sep 22 '22 10:09

Matt