Mocha/Chai: 测试准确的抛出错误结果

Mocha/Chai: Test for exact throw error result

有一个简单的方法,它会抛出一个错误:

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 :-)')
    }
})

现在我想对这个错误进行 mocha 测试。所以我想到了这个,它正在工作:

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()
})

到目前为止一切正常。

问题

但我想测试确切的错误消息。

我已经试过了

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

但它给了我一个失败的测试:

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

我认为文档对此有点 misleading/confusing - 只需从 expect 中删除 new 运算符,它就会匹配抛出的错误:

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

我从this post中发现我需要这样写:

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

注意错误信息在 Error() 之后,