46

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.

2
  • possible duplicate of Force Mocha test to fail?
    – aug
    Jun 16, 2015 at 17:52
  • call the done callback only in the 'then' function remove the outer done function, because when it will come to the catch function there will be no done callback so it will automatically show timeout error as of now since you are just consoling the outer done is passing all the test Jun 16, 2015 at 18:20

4 Answers 4

72

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.

7
  • There's absolutely no reason to solve the OP's problem by doing this rather than using the accepted answer.
    – Louis
    Nov 11, 2016 at 2:13
  • 27
    @Louis Are you talking about the accepted answer that recommends adding chai as another dependency? Limiting dependencies seems like a valid reason.
    – ken
    Nov 16, 2016 at 17:45
  • 1
    @ken The OP uses promises. So the solution should take advantage of the fact that Mocha understands promises. Half of the questions asked in mocha these days are due to problems caused by some insistence to use done when it all could be simplified by just using the promises already generated by the tested code. Because, see, while it is possible to use done with promises, it comes with some significant gotchas that the answer here omits. As for dependencies, as soon as you go beyond toy code, having a dependency on chai-as-promised is not a big deal.
    – Louis
    Nov 16, 2016 at 18:02
  • @Louis Can you expand on the gotchas? Apr 25, 2017 at 5:41
  • @DanielKobe I've improved my answer a bit now that I know other ways of causing the test to fail.
    – Daniel
    Apr 25, 2017 at 18:23
21

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

0
17

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();
});
0
8

This simple throw is working for me

describe('Lead', () => {
  it('should create a new lead', async () => {
     throw 'not implemented'
  })
})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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