function foo() { throw new Error('an error from (foo)');}我想檢查錯(cuò)誤是否包含文本(foo),我試過:expect(() => foo()).toThrow(expect.stringContaining('(foo)'))但是它失敗了并且沒有按預(yù)期工作: ● 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)'); | ^盡管我可以找到一種使用正則表達(dá)式的方法,例如:expect(() => foo()).toThrowError(/[(]foo[)]/)但這需要我轉(zhuǎn)義一些特殊字符,這不是我想要的。為什么expect.stringContaining在這種情況下不支持?還是我想念什么?一個(gè)嘗試的小演示:https://github.com/freewind-demos/typescript-jest-expect-throw-error-containing-text-demo
如何檢查函數(shù)是否拋出包含 Jest 文本的錯(cuò)誤?
寶慕林4294392
2023-04-27 10:24:43