用Tohavestyle进行测试'期望失败时通过

发布于 2025-01-31 03:32:36 字数 1031 浏览 4 评论 0原文

我正在学习测试和反应。我遇到了一些麻烦,理解为什么测试在不应该时通过:

从app.js

      <button style={{backgroundColor: 'gray'}}>
        Gray button
      </button>

来看,来自app.test.js的

    expect(colorButton).toHaveStyle( {backgroundColor: 'gray' }); // passes ok
    expect(colorButton).toHaveStyle( {backgroundColor: 'fay' }); // passes but should not
    expect(colorButton).toHaveStyle( {backgroundColor: 'dfhdhsdh' }); // passes but should not
    expect(colorButton).toHaveStyle( {backgroundColor: 'red' }); // expected error
    expect(colorButton).toHaveStyle( {backgroundColor: 'MidnightBlue' }); // expected error  

更多有关该项目的信息:

      "dependencies": {
        "@testing-library/jest-dom": "^5.16.4",
        "@testing-library/react": "^13.2.0",
        "@testing-library/user-event": "^13.5.0",
        "react": "^18.1.0",
        "react-dom": "^18.1.0",
        "react-scripts": "5.0.1", 
        "web-vitals": "^2.1.4"
      }

请有人帮助我吗?

I'm learning about testing and react. I'm having a little trouble understanding why a test is passing when it should not:

From App.js

      <button style={{backgroundColor: 'gray'}}>
        Gray button
      </button>

From App.test.js

    expect(colorButton).toHaveStyle( {backgroundColor: 'gray' }); // passes ok
    expect(colorButton).toHaveStyle( {backgroundColor: 'fay' }); // passes but should not
    expect(colorButton).toHaveStyle( {backgroundColor: 'dfhdhsdh' }); // passes but should not
    expect(colorButton).toHaveStyle( {backgroundColor: 'red' }); // expected error
    expect(colorButton).toHaveStyle( {backgroundColor: 'MidnightBlue' }); // expected error  

More info about the project:

      "dependencies": {
        "@testing-library/jest-dom": "^5.16.4",
        "@testing-library/react": "^13.2.0",
        "@testing-library/user-event": "^13.5.0",
        "react": "^18.1.0",
        "react-dom": "^18.1.0",
        "react-scripts": "5.0.1", 
        "web-vitals": "^2.1.4"
      }

Could anyone help me, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2025-02-07 03:32:36

在这种情况下,用对象测试tohavestyle正在引起错误,我真的不知道为什么。您可能应该在测试library/jest-dom github 时,您可能应该打开一个问题。

但是目前,如果您使用一个简单的字符串来测试背景应该有效,那么

expect(colorButton).toHaveStyle("background-color: gray");

完整的测试:

test("background test", () => {
    const { getByRole, debug } = render(<App />);
 
    // To check what jest-dom is rendered, debug is always a good idea.
    // And here you will see that button is rendering with style="background-color: gray;"
    debug();

    const colorButton = getByRole("button");

    expect(colorButton).toHaveStyle("background-color: gray");
    expect(colorButton).not.toHaveStyle("background-color: fay");
    expect(colorButton).not.toHaveStyle("background-color: dfhdhsdh");
    expect(colorButton).not.toHaveStyle("background-color: red");
  });

您可以检查tohavestyle的其他选项

Looks like testing toHaveStyle with an Object in this case is causing an error and i really don´t know why. You should probably open an issue in testing-library/jest-dom github.

But for now, if you use a simple string to test the background should work, just like that:

expect(colorButton).toHaveStyle("background-color: gray");

and the complete test:

test("background test", () => {
    const { getByRole, debug } = render(<App />);
 
    // To check what jest-dom is rendered, debug is always a good idea.
    // And here you will see that button is rendering with style="background-color: gray;"
    debug();

    const colorButton = getByRole("button");

    expect(colorButton).toHaveStyle("background-color: gray");
    expect(colorButton).not.toHaveStyle("background-color: fay");
    expect(colorButton).not.toHaveStyle("background-color: dfhdhsdh");
    expect(colorButton).not.toHaveStyle("background-color: red");
  });

You can check other options of toHaveStyle here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文