function foo() {
throw new Error('an error from (foo)');
}
I want to check if the error contains the text (foo)
, I tried:
expect(() => foo()).toThrow(expect.stringContaining('(foo)'))
But which is failed and does not work as expected:
● 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)');
| ^
Although I can find a way with regex like:
expect(() => foo()).toThrowError(/[(]foo[)]/)
But which requires me to escape some special characters, which is not what I want.
Why is expect.stringContaining
not supported in this case? Or do I miss anything?
A small demo for trying: https://github.com/freewind-demos/typescript-jest-expect-throw-error-containing-text-demo