-
Notifications
You must be signed in to change notification settings - Fork 403
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 get font-size w/ css variable #322
Comments
Hi, thanks for taking the time to report this. I am having no luck reproducing it though. I just added the following test to the test module for it('handles CSS custom properties', () => {
const {container, queryByTestId} = render(`
<div data-testid="title" class="title">Title</div>
`)
const style = document.createElement('style')
style.innerHTML = `
:root {
--font-size-xlarge: 64px;
--font-weight-bold: 700;
}
.title {
font-size: var(--font-size-xlarge);
font-weight: var(--font-weight-bold);
margin: auto 0;
}
`
document.body.appendChild(style)
document.body.appendChild(container)
const title = queryByTestId('title')
expect(title).toHaveStyle({
'font-size': 'var(--font-size-xlarge)',
'font-weight': 'var(--font-weight-bold)',
})
}) Any ideas on what I may be missing? Maybe if you can provide a minimal repo where the issue can be reproduced, that'd be great. |
Hi, I'm also having a similiar problem with "@nrwl/jest": "^12.0.6" In my case, the test always passes when css variable is used within Styled Components. I've created a code sandbox here. The following test should fail because the color is actually set to var(--clr-blue) and also because var(--clr-red) doesn't exist. Am I using
Any help would be greatly appreciated. Thanks. |
As mentioned before, a minimal repository where this can be reproduced would go a long way towards figuring this out. |
@gnapse - have you tried changing your test to something like this? expect(title).toHaveStyle({
'font-size': 'var(--font-size-small)', // this should cause a failure, but it doesn't
'font-weight': 'var(--font-weight-bold)',
}) The reported issue is that the test still passes. |
I just encountered the kind of problem (false positive on a
I would guess from all this that Note: I know the original complaint was for a |
Hey guys, Did you import CSS variable and use it?. I encountered the same problem and realized that the Css variable was not defined. And I tried to use variable using css file at css-in-js. |
Just ran into this problem today. Seems like it's related to the jsdom dependency. Based on what I found, it's either due to a bug parsing the :root selector or due to how cascaded property values are implemented. See here. I'll continue to investigate because this seems like a feature that's needed. Edit Nov-29-2021:
really evaluates to:
This particular issue is not actually a problem with jest-dom, but with its JSDOM dependency. It looks like the problem will be fixed with this pull request. I've included my original suggestion for a workaround below (not sure if it's valid though):
|
I've submitted two pull requests for the JSDOM project to get this feature working:
|
@testing-library/jest-dom
version: 5.11.6node
version: v14.15.1npm
(oryarn
) version: 1.22.10svelte-testing-library
version: 3.0.0Relevant code or config:
What you did:
Test font style w/
@testing-library/jest-dom
and@testing-library/svelete-testing-library
What happened:
Test should fail if i set
font-size
not asvar(--font-size-xlarge)
, but test still passes.But works charm about font-weight.
Reproduction:
from this commit,
install w/
yarn
then run test w/yarn test
Problem description:
Seems like cannot recognize font-size with css variable, as we can see the result of
getComputedStyle(title)
belowThe text was updated successfully, but these errors were encountered: