styled-components: window.getComputedStyled doesn't work with v5.2.0 and Testing Library/JSDOM
Environment
System:
- OS: macOS 10.15.6
- CPU: (8) x64 Intel® Core™ i7-8569U CPU @ 2.80GHz
- Memory: 70.66 MB / 16.00 GB
- Shell: 5.7.1 - /bin/zsh
Binaries:
- Node: 12.16.1 - ~/.nvm/versions/node/v12.16.1/bin/node
- Yarn: 1.22.4 - ~/.yarn/bin/yarn
- npm: 6.14.8 - ~/.nvm/versions/node/v12.16.1/bin/npm
Reproduction
https://github.com/dfernandez79/styledcomp-bug-example
Steps to reproduce
- Clone the project: https://github.com/dfernandez79/styledcomp-bug-example
- Install the dependencies:
npm ci - Run the tests:
npm test
Expected Behavior
The tests pass with v5.1.1 but fail with v5.2.0. The test is using window.getComputedStyle to get the styles. While the implementation of computed styles is limited in JSDOM, it worked in v5.1.1 so I expect it to work in v5.2.0.
Actual Behavior
With v5.2.0 calls to window.getComputedStyle always return an empty string. Doing a console.log(prettyDOM(document)) doesn’t show any <style> tag for v5.2.0.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 28
- Comments: 16 (2 by maintainers)
Commits related to this issue
- fix https://github.com/styled-components/styled-components/issues/3262#issuecomment-716403282 — committed to laststance/react-typescript-todomvc-2022 by ryota-murakami 4 years ago
- @see https://github.com/styled-components/styled-components/issues/3262 — committed to laststance/react-typescript-todomvc-2022 by ryota-murakami 4 years ago
- fix https://github.com/styled-components/styled-components/issues/3262#issuecomment-716403282 — committed to pandasoft0/create-react-app-typescript-todo-example-2021 by pandasoft0 4 years ago
- @see https://github.com/styled-components/styled-components/issues/3262 — committed to pandasoft0/create-react-app-typescript-todo-example-2021 by pandasoft0 4 years ago
- fix https://github.com/styled-components/styled-components/issues/3262#issuecomment-716403282 — committed to bitcrusher1023/todoV by deleted user 4 years ago
- @see https://github.com/styled-components/styled-components/issues/3262 — committed to bitcrusher1023/todoV by deleted user 4 years ago
The problem is that Jest does not use the
browserfield, if you don’t want to break everything, you can just use that trick:i get exact problem also using .toHaveStyle, in 5.1.1 version test pass but 5.2 no. i follow this thread.
The solution from @gregberge works fine for me as well.
@willianjusten try using
without the
^it will also mapjest-styled-componentstostyled-components.browser.cjs.jstoBeVisibleis not working for me anymore intesting-libraryIt seems like v5.2.1 has solved it (for us at least).
@dfernandez79 It looks like the bug reappeared in the styled-component v6. It blocked me after upgrading to v6.0.3. The solution posted by @tbergquist-godaddy solved the issue in my case.
@gregberge that works fine for cases using
toHaveStyleortoBeDisabledbut inside the snapshots we’re losing the styles that comes fromjest-styled-components…I’ve updated the test app used for the bug report to
v5.2.1: https://github.com/dfernandez79/styledcomp-bug-exampleAnd the bug seems to be solved. Closing this issue 😄
I think it should be mentioned in docs, @tbergq has the final solution!
Thanks @tbergq , it’s working perfectly now =D
@fooloomanzoo Yeah,
toBeVisibleusinggetComputedStyleinternal so might be that’s something broken.This PR is my reproduction resource: Update styled-components: 5.1.1 → 5.2.0 (minor)
I have OSS project Create React App TypeScript Todo Example 2020 and I also facing issue about
toBeVisibleof @testing-library/jest-dom custom jest matchers.(it’s always return true).Parent Commit CI result is all green, but Update styled-components to version 5.2.0 Commit CI result showed break
yarn test(jest + react-testing-library) at thenot.toBeVisible()assertion.We’re having the exact same issue (we’ve just started to migrate to styled components from JSS), so I’ll follow this thread closely.