Javascript Chai expect.to.throw(错误)未按预期工作

Javascript Chai expect.to.throw(错误)未按预期工作,javascript,unit-testing,mocha.js,chai,Javascript,Unit Testing,Mocha.js,Chai,我的主张是: it.only('should throw an error if the transcription cannot happen', () => { expect(TranscriptLib.myFunc({ data }, '1')).to.throw(Error) }) 我的职能是: myFunc: (data, id) => { throw new Error({

我的主张是:

    it.only('should throw an error if the transcription cannot happen', () => {
        expect(TranscriptLib.myFunc({ data }, '1')).to.throw(Error)
    })
我的职能是:

myFunc: (data, id) => {

                throw new Error({
                    message: 'Transcription failed',
                    error: someError
                })
但是,当我运行测试时,日志显示:

     Error: [object Object]

测试失败了。我做错了什么?

你的问题是重复的

解决问题的最直接方法是通过arrow函数调用代码:

it.only('should throw an error if the transcription cannot happen', () => {   
  expect(() => TranscriptLib.myFunc({ data }, '1')).to.throw(Error)
})

正如另一个问题的答案所示,您也可以使用.bind来创建一个新函数,依此类推。

IIRC expect.to.throw需要一个函数,而不是使用函数重写魔法来包装表达式。@Ry-就是这样。请留下一个答案,我可以接受