toHaveStyle not picking up styles from styled-components

See original GitHub issue
  • @testing-library/jest-dom version: 5.11.4
  • react-testing-library version: 10.4.9 (checked in yarn.lock)
  • node version: 13.7.0
  • npm (or yarn) version: 1.22.4 (yarn)

Relevant code or config:

import '@testing-library/jest-dom'
import React from 'react'
import { render } from '@testing-library/react'
import { Button } from '.'; // Internally, "Button" is styled using styled-components
import { defaultTheme } from '../../themes'

test('renders button variants correctly', () => {
  const { container, getByTestId } = render(
    <Button theme={defaultTheme} data-testid='subBtn' variant='secondary' >
      Decrease count
    </Button>
  )
  // Test styles
  expect(getByTestId(/subBtn/)).toHaveStyle('background-color: transparent')
})

What you did:

Trying to test the final styles for a component using “toHaveStyle” matcher. This test worked fine before I yarn upgraded the project.

What happened:

After upgrading my dependencies, some tests stopped working (the ones including toHaveStyle matcher). Other tests (.toBeInTheDocument, .toHaveClassName…) still work fine. It appears to be picking the component base styles (not considering styles applied by classnames / styled-components), The error log looks like this:

  ● renders button variants correctly

    expect(element).toHaveStyle()

    - Expected

    - background-color: transparent;
    + background-color: ButtonFace;

      12 |   )
      13 |   // Test styles
    > 14 |   expect(getByTestId(/subBtn/)).toHaveStyle('background-color: transparent')
         |                                 ^
      15 | })

I never apply a “ButtonFace” style, this makes me think it’s jest that is not picking up the stylesheets or the styles for some reason. also relevant: The Button component works just fine, tested imported in other projects and with playroom-js. It’s just the tests that are not getting the styles properly (and previously did).

Reproduction:

Unfortunately, this happens in a private repo (not the owner), so it’s kind of complicated to provide a full demo. If you need any more information (tests setup, package.json, etc) I can gladly provide it.

Problem description:

Tests were working fine previous to running “yarn upgrade”, which updated the testing-related libraries to newer versions

Suggested solution:

I’m not sure how could this be solved, I’m guessing the newer versions of the libraries introduced some breaking changes or changes in configuration which I am not addressing

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
felixmagnuscommented, Oct 8, 2020

Why was this issue closed? We are having the same problem in our project - how are we supposed to continue here?

14reactions
wilomgfxcommented, Sep 30, 2021

Same issue here, it’s a shame because it works if I’m not overriding styles from another component.

As soon as I override something, it does not pick up the overridden styles.

image

On the left is my unit test and the right is what getComputedStyles returns for that component. Tests: the base component styles Browser: The overridden styles (right one)

Read more comments on GitHub >

github_iconTop Results From Across the Web

toHaveStyles not working with module.css styles using React ...
Could you please tell us what your jest.config.js looks like? As a general rule, SASS and CSS are often excluded from jest because...
Read more >
How to test a Component's CSS styles with React ... - Ilya Zykin
But what if you use Styled Components? In this case you will not know a name of the wrapper component, it will be...
Read more >
FAQs - styled-components
At this time we do not recommend using @import within cGS due to some issues ... This may cause dynamic styles not working...
Read more >
should every component be a styled component? : r/reactjs
Thus every styled component is responsible for its style. ... of the developers working in the frontend are more javascript-centric and not ......
Read more >
styled-components tested with react-testing-library
styled -components tested with react-testing-library ... styles.css ... Activating extension 'vscode.typescript-language-features' failed: Could not find ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found