笑话反应预期.stringcontains与tohavvalue组合失败



我尝试使用expect。在一个带有create-react-app的jest测试脚本中包含的字符串来对字符串进行部分匹配,但它不起作用。如果我把它和toStrictEqual一起使用,它就能工作。这是臭虫吗?

测试:

test("should update text when typing", () => {
render(<EditorWindow />);
userEvent.type(screen.getByRole("textbox"), "bar");
expect(screen.getByRole("textbox")).toHaveValue(
expect.stringContaining("bar")
);
});
test("should match text", () => {
const a = "#title<br>type text here..bar";
expect(a).toStrictEqual(expect.stringContaining("bar"));
});

结果:

<EditorWindow> />

× should update text when typing (46 ms)
√ should match text
● <EditorWindow> /> › should update text when typing
expect(element).toHaveValue(StringContaining)
Expected the element to have value:
StringContaining "bar"
Received:
#title<br>type text here..bar
15 |     render(<EditorWindow />);
16 |     userEvent.type(screen.getByRole("textbox"), "bar");
> 17 |     expect(screen.getByRole("textbox")).toHaveValue(
|                                         ^
18 |       expect.stringContaining("bar")
19 |     );
20 |   });
at Object.<anonymous> (src/components/EditorWindow.test.tsx:17:41)

我使用以下库:

"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.6.3",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.25",
"@types/react": "^17.0.1",
"@types/react-dom": "^17.0.0",
...

希望有人能帮助我这个?提前感谢!

我设法得到工作代码没有typescript警告像这样:

test("should update text when typing", () => {
render(<EditorWindow />);
userEvent.type(screen.getByRole("textbox"), "bar");
const myTextAreaVal = (screen.getByRole("textbox") as HTMLInputElement)
.value;
expect(myTextAreaVal.includes("bar")).toBe(true);
});

我必须使用类型转换as HTMLInputElement,以摆脱typescript警告,并能够将文本区域的值存储在变量myTextAreaVal中。然后我使用myTestAreaVal.includes对字符串进行部分匹配。

谢谢Adrisolid的提示!

最新更新