Jest-styled-components: Doesn't work with jsdom

Created on 19 Mar 2017  路  14Comments  路  Source: styled-components/jest-styled-components

Enzyme uses jsdom environment to be able to mount components. The following error is thrown by this module.

TypeError: Cannot read property 'cssRules' of undefined

Thank you!

Most helpful comment

ola! I'll have a look at this sometime in the next couple of days.

All 14 comments

That's correct @lgraziani2712.
As stated in the README, the testEnvironment should be set to node to make this package work.
You can still use Enzyme's shallow rendering.

It would be awesome to have @threepointone's help here :)
It looks like ownerNode is always undefined with jsdom.

Yep, but since didn't see the issue, I reported it! :)

Sorry for not being able to help :(

@MicheleBertoli I cannot use shallow rendering. At least without changing my tests.

Something as simple as checking if <Button>Hello!</Button> renders the text it'll fail if <Button /> is a styled.button. With shallow render will happen this:

expect('styled.button').toBe('Hello!');

ola! I'll have a look at this sometime in the next couple of days.

Repro steps:

  • clone the repo
  • install deps
  • set testEnvironment to jsdom
  • run tests
  • you get TypeError: Cannot read property 'cssRules' of undefined

it is caused by a quite old bug in jsdom: https://github.com/tmpvar/jsdom/issues/992

Applying styled-components/styled-components#707 the tests don't break with jsdom but the styles are empty. Needs more investigation.

Applying styled-components/styled-components#707 the tests don't break with jsdom but the styles are empty. Needs more investigation.

I ended giving up on styled-components v1 and moved to v2.0.0-16+. The API has totally changed in that version and works better.

I'm using styled-components with v2.0.0-17 and jest-styled-components with v2.0.0
now getting

TypeError: styleSheet.rules is not a function

Thanks @joseaplwork, please check #5.

Jest 20 ships with a @jest-environment annotation which solves this issue, please see #13.
I'm going to close this for now, and we'll get back to it when Styled Components v2 is stable.

I think I might not understand this. How does it solve the issue? When my test relies on jsdom, @jest-environment won't allow me to run a single test magically in both, node and jsdom.

@MoeSattler @jest-environment is available from Jest 20, please make sure you are using the right version.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dlebedynskyi picture dlebedynskyi  路  8Comments

jmacioszek picture jmacioszek  路  3Comments

santino picture santino  路  5Comments

sumanbh picture sumanbh  路  3Comments

k15a picture k15a  路  4Comments