React-Testing-Library - MatchingFunction 解释为字符串

React-Testing-Library - MatchingFunction interpreted as string

screen.getByText(match: Matcher) 接受类型为 string | RegExp | MatcherFunctionMatcher 在后者的情况下,类型为 (content: string, element: HTMLElement) => boolean.

期望: screen.getByText((content, element) => element.className.includes("my-class") && content.includes("text to search for")); 在 DOM 中搜索带有文本 text to search for

的 class my-class 的元素

实际: TestingLibraryElementError:无法找到包含文本的元素:element.className.includes("my-class") && content.includes("要搜索的文本")

好像是把MatcherFunction原型解析成了字符串?会不会是因为 lambda 语法?

这如您所愿。该函数正在执行并返回,您的测试失败了。测试运行器并没有办法用简单的英语告诉你你的函数做了什么,所以它简化了你在关于你失败的输出消息中搜索的东西(无论是字符串,正则表达式还是函数)。

您可能会说这是糟糕的开发人员体验,您是对的。但看起来一切都在按预期工作。