Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
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.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report

0

Use chai-as-promised, with native Mocha promise handlers.

var chai = require('chai').use(require('chai-as-promised'));
var should = chai.should(); // This will enable .should for promise assertions

You no longer need done, simply return the promise.

// Remove `done` from the line below
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();

    // Return the promise
    return indicative
        .validateAll(rules, body, valMessages)
        .should.be.rejected; // The test will pass only if the promise is rejected

    // Remove done, we no longer need it
    // done();
});
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!