Mocha / Chai: test for the exact error result

There is a simple method that throws the error:

methods.js

insertItem = new ValidatedMethod({
    name    : 'article.insert.item',
    validate: new SimpleSchema({
        parent: { type: SimpleSchema.RegEx.Id }
        value : { type: String }
    }).validator(),

    run({ parent, value}) {
        var isDisabled = true
        if (isDisabled)
            throw new Meteor.Error('example-error', 'There is no reason for this error :-)')
    }
})

      

Now I want to do a mocha test for this error. So I came up with this that works:

server.test.js

it('should not add item, if element is disabled', (done) => {
    const value = 'just a string'

    function expectedError() {
        insertItem.call(
            {
                parent: '12345',
                value
            }
        )
    }

    expect(expectedError).to.throw
    done()
})

      

Everything works up to this point.

Problem

But I would like to check the exact error message.

I've already tried

expect(expectedError).to.throw(new Meteor.Error('example-error', 'There is no reason for this error :-)'))

      

But this gives me a failing test:

Error: expected [Function: expectedError] to throw 'Error: There is no reason for this error :-) [example-error]'

      

+3


source to share


2 answers


I think the docs are a little misleading / confusing on this - just remove the operator new

from yours expect

and it will match the error:



expect(expectedError).to.throw(Meteor.Error('example-error', 'There is no reason for this error :-)'))

0


source


From this post, I learned that I need to write like this:

expect(expectedError).to.throw(Meteor.Error(), 'example-error', 'This is an error');

      



Note that the error messages after Error (),

0


source







All Articles