【发布时间】:2021-11-01 17:46:12
【问题描述】:
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
我认为这是因为它只检查引用相等而不是深度对象相等。
【问题讨论】:
-
您介意分享最小的可重现示例吗?
-
@Vulwsztyn 你能用这个来重现吗?我在一个简单的 mocha 单元测试中使用它。
标签: chai