function foo() {
throw new Error('an error from (foo)');
}
我想检查错误是否包含文本(foo),我试过:
expect(() => foo()).toThrow(expect.stringContaining('(foo)'))
但是它失败了并且没有按预期工作:
● test › checks error containing text
expect(received).toThrowError(expected)
Expected asymmetric matcher: StringContaining "(foo)"
Received name: "Error"
Received message: "an error from (foo)"
1 | function foo() {
> 2 | throw new Error('an error from (foo)');
| ^
尽管我可以找到一种使用正则表达式的方法,例如:
expect(() => foo()).toThrowError(/[(]foo[)]/)
但这需要我转义一些特殊字符,这不是我想要的。
为什么expect.stringContaining在这种情况下不支持?还是我想念什么?
一个尝试的小演示:https://github.com/freewind-demos/typescript-jest-expect-throw-error-containing-text-demo
小唯快跑啊
相关分类