chai - 与属性深度相等的柴抛
问题描述
expect.to.throw
为抛出的错误返回一个代理,因此我可以使用with.property
它来检查错误的某些属性。
我在我的自定义错误上附加了一个details
对象,但我无法测试它们,因为with.property
比较只使用严格等于。
我可以以某种方式使用深度相等来比较这个属性吗?
例子:
class DocNotFoundError extends Error {
constructor(message, docId) {
super(message)
this.details = { docId }
}
}
const getDoc = id => {
throw new DocNotFoundError('errors.docNotFound', id)
}
const docNotFound = expect(() => getDoc('01234567890')).to.throw('DocNotFoundError')
docNotFound.with.property('details', { docId: '01234567890' }) // fails
该错误将失败,类似于
AssertionError: expected { Object (error, ...) } to have property 'details' of { Object (docId) }, but got { Object (docId) }
+ expected - actual
我认为这是因为它只检查引用相等而不是深度对象相等。