-
Notifications
You must be signed in to change notification settings - Fork 405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
toHaveStyle does not behave consistently between number and string values #561
Comments
I'm not so sure that we should support a number value. It is not valid CSS to provide a numeric value to Perhaps only The fact that it is supported in React could make me reconsider, but keep in mind that this is not a React testing framework. I'd rather leave it as it is, and use |
I understand that since the jest-dom matchers are not specifically built for React that it is less ideal to make it conform to what React handles. The problem remains that I am not able to document my test in the way I would consume the |
I would consider supporting numbers as React do. Would you be open to make that contribution? |
Hey @gnapse, I can give that a try. Is there a timeline on this? This will be my first time making code contribution to this repository but I am happy to taking it on. |
Not sure what you mean by "timeline". If you mean that if there's a deadline, I'm not in a hurry to have this feature. You're the one who needs it. I'm more than happy to help to guide you to make it happen. Submit a first draft PR, and I'll let you know what's missing in order to get it merged. Overall idea is:
|
A caveat after giving it some thoughtOn a second thought, one thing that worries me is: how would this work in general? Not all properties should be converted to assume the unit is |
Thanks for bringing up the caveat. Since different CSS properties support different ranges of numbered values, it may prove to be difficult to capture all the exceptions. After a discussion with a colleague of mine, I believe
|
One issue remainsThe following test should fail, but it passes! render(<div data-testid="element" style={{ fontSize: 8 }} />)
expect(screen.getByTestId('element')).toHaveStyle({ fontSize: 1 }) Would you agree @gnapse that we limit the scope of this issue to just this investigating and offering a solution PR for this bug? If so, would you like me to open a new issue just for this bug and close this one instead? |
I went ahead and created a new issue so we can close this one b/c I don't think my original proposal of supporting number values for font-size the way react does is a good idea anymore. Instead, the new issue focuses on the bug itself. #564 |
I couldn't agree more with your colleague. He put in much better words my initial hesitation about this. Thanks for sharing. |
@testing-library/jest-dom
version: 6.1.5node
version: 20.3.1jest
(orvitest
) version: 0.34.1npm
(oryarn
) version: 9.6.7Relevant code or config:
What you did:
I wrote the above test to understand how to assert on styles so that I can check that my fontSize values on my component is being configured correctly.
What happened:
The test above failed when fontSize has a number value and is above 0. I expected that providing a number or a pixel value should work exactly the same when asserting on styles.
Reproduction:
Problem description:
This is a problem because testers should have the confidence to be able to use between a number value or a string without worrying whether the test itself is correct.
Suggested solution:
The text was updated successfully, but these errors were encountered: