jest-dom version: 1.11.0node version: 8.11.3react-testing-library version: 4.1.4const { getByText } = render(
<span style={{ backgroundColor: '#123456' }}>Hello World</span>
);
expect(getByText('Hello World')).toHaveStyle('background-color: #123456');
// The above assertion failed
I rendered an element with a specific hex background color in its styles.
I can't assert back that the background-color style is indeed there. This is because the actual styles I get back from the element give me the color in the rgb(r, g, b) format:
expect(element).toHaveStyle()
- Expected
- background-color: #123456;
+ background-color: rgb(18, 52, 86);
The problem is that users are forced to use the rgb() format.
It'd be nice if toHaveStyle would convert hex colors to the rgb(r, g, b) format so this is transparent for users. Otherwise document the caveat and maybe provide the user with some alternative or direct them to a library they can use to make this conversion, and that they make sure they always use rgb(r, g, b) either directly or by converting their hex value dynamically themselves.
toHaveStyle treats color values differently. For example:
toHaveStyle(`
background-color: #fff;
`)
...will fail, because #fff will be converted to a RGB value. However:
toHaveStyle(`
border: 1px solid #fff;
`)
... is fine, because #fff stays #fff.
Yes, it is annoying. Would love to have consistent format.
Does your work in #57 improve any of this?
BTW, sorry, I've seen all the work you've been contributing, but have had a hard time to be able to review every thing.
I do think #57 is ready. So feel free to merge it if you consider it done and good.
Kk, I do believe it is ready! I am going to add the test case (border) above just to verify then will merge it in!
@gnapse The PR has been merged as a fix:.
:tada: This issue has been resolved in version 1.12.1 :tada:
The release is available on:
npm package (@latest dist-tag)Your semantic-release bot :package::rocket:
This still happens in:
@testing-library/jest-dom": ^4.1.0
@testing-library/react: ^9.1.3

@gnapse
Can you expand a bit on the full example? In the screenshot we don't see the code being tested, how it sets this color that it is then not found.
Or better yet, a minimal reproducible example/repo.
Here's a minimal example, where hex is still converted to rgb: https://codesandbox.io/s/heuristic-ritchie-5hgn8
Hmmm, this is weird because that codesandbox tests work for me out of the box:

(I only needed to adjust the text in the query because it did not match the text inside the h1 but after I did that, it passed.)
I wonder why it passes for me and not for you.
Oh it passes for me now as well, maybe it was just the text not matching 🤦♂️.
I realised that my actual issue was that I was trying to use toHaveStyle on an svg element. Even though the color attribute is shared with css it is not picked up. toHaveAttribute seems to be the appropriate matcher in this case. Also prettyDOM showing expected values as rgb in error debug added to the confusion.
Most helpful comment
This still happens in:
@gnapse