Javascript Chai expect.to.throw错误不相等
我试图使用expect断言抛出特定错误 已成功捕获错误,但错误比较失败。这包括thrownCustomError和new CustomError以及thrownCustomError和customErrorInstance。然而,似乎使用expect.to.throwCustomError“自定义错误”确实有效 在我的例子中,我特别希望断言使用参数生成错误消息的自定义错误,因此使用后一种比较相当麻烦 我做错了什么?非常感谢您的建议Javascript Chai expect.to.throw错误不相等,javascript,node.js,chai,Javascript,Node.js,Chai,我试图使用expect断言抛出特定错误 已成功捕获错误,但错误比较失败。这包括thrownCustomError和new CustomError以及thrownCustomError和customErrorInstance。然而,似乎使用expect.to.throwCustomError“自定义错误”确实有效 在我的例子中,我特别希望断言使用参数生成错误消息的自定义错误,因此使用后一种比较相当麻烦 我做错了什么?非常感谢您的建议 let chai = require("chai"); cons
let chai = require("chai");
const expect = chai.expect;
const assert = chai.assert;
class CustomError extends Error{
constructor() {
super('Custom error')
}
}
const throwFn = () => {
throw new CustomError()
}
const customErrorInstance = new CustomError()
describe('CustomError Test', () => {
it('should throw a CustomError (new)', () => {
expect(throwFn).to.throw(new CustomError())
})
it('should throw a CustomError (const)', () => {
expect(throwFn).to.throw(customErrorInstance)
})
it('should produce a strictly equal error', (done) => {
try {
throwFn()
} catch (e) {
assert(e === new CustomError(), 'Thrown error does not match new instance of error')
assert(e === customErrorInstance, 'Throw error does not match const instance of error')
done()
}
expect.fail()
})
})
输出:
CustomError Test
1) should throw a CustomError (new)
2) should throw a CustomError (const)
3) should produce a strictly equal error
0 passing (38ms)
3 failing
1) CustomError Test
should throw a CustomError (new):
AssertionError: expected [Function: throwFn] to throw 'Error: Custom error' but 'Error: Custom error' was thrown
+ expected - actual
at Context.it (test.js:19:27)
2) CustomError Test
should throw a CustomError (const):
AssertionError: expected [Function: throwFn] to throw 'Error: Custom error' but 'Error: Custom error' was thrown
+ expected - actual
at Context.it (test.js:23:27)
3) CustomError Test
should produce a strictly equal error:
AssertionError: Thrown error does not match new instance of error
at Context.it (test.js:29:4)
您每次都在测试实例,因此需要使throwFn抛出该实例:
const customErrorInstance = new CustomError()
const throwFn = () => {
throw customErrorInstance
}
// should throw a CustomError (const)
expect(throwFn).to.throw(customErrorInstance);
第一个测试,您必须更改为类,因为您在函数中抛出一个单独的实例,然后尝试与expect中的一个新实例进行比较,这永远不会是真的,根据定义,一个实例并不严格等同于另一个实例:
const throwFn = () => {
throw new CustomError()
}
expect(throwFn).to.throw(CustomError)
因为您所做的是尝试通过执行expectthrowFn.to.thrownew CustomError来比较实例,所以这将永远不会成功
这是你的代码
经过深入挖掘,柴对误差相等性的评价似乎是。。。奇怪的错误由消息和堆栈跟踪组成。由于堆栈跟踪因错误实例化的位置而异,因此如果不抛出相同的实例,expect.to.throw中就无法实现相等 我能找到的唯一只会断言错误消息的解决方案如下:
const a = new CustomError()
...
it('should produce a strictly equal error', (done) => {
try {
throwFn()
} catch (e) {
assert(e.message === a.message, 'Thrown error does not match new instance of error')
done()
}
expect.fail()
})
注意:在撰写本文时,有一个公开的PR来删除/修改此行为感谢您的回复,但这并不是一个实际的解决方案。除了幼稚的例子之外,人们怎么能期望能够指定抛出错误的实例呢?如上所述,我希望验证传递给错误构造函数的参数。考虑一个预期的:X获取:Y错误格式。我需要验证x和x的值y@DAnsermino是的,我们不应该测试错误的实例,我们应该检查类型,就像我如何修复您的第一个示例-expectthrowFn.to.throwCustomError。如果要对消息进行asserton,则传入一条消息expectthrowFn.to.throwCustomError,/应包含以下内容/
const a = new CustomError()
...
it('should produce a strictly equal error', (done) => {
try {
throwFn()
} catch (e) {
assert(e.message === a.message, 'Thrown error does not match new instance of error')
done()
}
expect.fail()
})