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

  1. Clone the project: https://github.com/dfernandez79/styledcomp-bug-example
  2. Install the dependencies: npm ci
  3. 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

Most upvoted comments

The problem is that Jest does not use the browser field, if you don’t want to break everything, you can just use that trick:

// jest.config.js

module.exports = {
  moduleNameMapper: {
    'styled-components':
      '<rootDir>/node_modules/styled-components/dist/styled-components.browser.cjs.js',
  },
}

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

module.exports = {
  moduleNameMapper: {
    '^styled-components':
      '<rootDir>/node_modules/styled-components/dist/styled-components.browser.cjs.js',
  },
}

without the ^ it will also map jest-styled-components to styled-components.browser.cjs.js

toBeVisible is not working for me anymore in testing-library

      15 |     );
      16 | 
    > 17 |     expect(getByText('Item 1')).not.toBeVisible();
         |                                     ^
      18 |     fireEvent.click(getByText('Click to open'));
      19 |     expect(getByText('Item 1')).toBeVisible();
      20 |   });

It 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.

The solution from @gregberge works fine for me as well.

@willianjusten try using

module.exports = {
  moduleNameMapper: {
    '^styled-components':
      '<rootDir>/node_modules/styled-components/dist/styled-components.browser.cjs.js',
  },
}

without the ^ it will also map jest-styled-components to styled-components.browser.cjs.js

@gregberge that works fine for cases using toHaveStyle or toBeDisabled but inside the snapshots we’re losing the styles that comes from jest-styled-components

I’ve updated the test app used for the bug report to v5.2.1: https://github.com/dfernandez79/styledcomp-bug-example

And 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, toBeVisible using getComputedStyle internal 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 toBeVisible of @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 the not.toBeVisible() assertion.

Screen Shot 2020-09-13 at 15 26 13

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.