-
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
Media queries #113
Comments
It is currently not possible to test media queries, see testing-library/jest-dom#113
It may look simple, but it is far from it, I think 😄 I'm unfamiliar with both About jest-styled-componentsThat library ( The difference compared to jest-domThis library Stylesheets in jest-domStylesheets in this context are also a bit of a complex thing to test in this detail that you intend to test them. For starters, our style checking matchers will not work properly unless you manually setup attaching a css stylesheet to the What you'd need to do, hypotheticallyI'm not sure therefore if you can:
On setting the browser width in testsOn that last point, what our const { container } = render(<ComponentBeingTestedHere />)
// TODO: Attach at this point the stylesheet source code to the document...
// as shown here https://github.com/testing-library/jest-dom/blob/09f7f041805b2a4bcf5ac5c1e8201ee10a69ab9b/src/__tests__/to-have-style.js#L12-L18
browser.setWindowWidth(1280) // Note: This is not a real API!!!
expect(container.firstChild).toHaveStyle("color: blue")
browser.setWindowWidth(768) // Note: This is not a real API!!!
expect(container.firstChild).toHaveStyle("color: red") The above hypothetical test would "prove" there's a media query somehow, without actually testing it in terms of the css source code that sets it up. However, it's up to you to see if there's a way to tell the browser in the tests to set a specific window size. I'm not sure jsdom supports that, unfortunately. BottomlineBut this all boils down to the intent of this library (and the entire set of testing libraries it is part of): tests should be testing the end result, and not the implementation details. That's not to say your approach is wrong. If you still prefer to test things in this level of details, and your project is nevertheless intrinsically tied to Hope all this helps, and please, if you do pursue checking out the pointers I give here to try make this work, please share your findings here, wether positive or not. |
Hello @gnapse, I agree, jest-styled-components tests the underlying implementation, and I want to switch to jest-dom to test the end result (the CSS applied on the element). I suspected that I had to change the size of the browser in order to test media queries. But I was not sure it will work that way. I hoped that someone had already the problem and was able to give me a real working example in order to gain time. I will work on it, as soon as I have something that work I will post it here. Thank you! |
It looks like it is not currently possible https://github.com/jsdom/jsdom/blob/d6f8a97b5fb7709d6ad0215c1ae95fd4cab58489/lib/jsdom/level2/style.js#L29 |
I'm going to be closing this for the time being. Feel free to open if there's something actionable stemming from this proposal. |
I would suggest consider testing media queries through Cypress or similar framework were stylesheet actually get rendered and/or interpreted. |
Interestingly, even |
Hey! This library worked for me https://github.com/dferber90/jest-transform-css |
First thank you for this awesome library!
I would like to use
toHaveStyle
to test a media query. I am migrating xstyled tests fromjest-styled-components
tojest-dom
since the approach is better for me.My question is simple. How to do the equivalent with
toHaveStyle
?The text was updated successfully, but these errors were encountered: