Skip to content
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

No style rules when testing directly a styled components from babel macro #325

Closed
dorianamouroux opened this issue Jun 4, 2020 · 3 comments

Comments

@dorianamouroux
Copy link

I'm currently updated styled-components of my project to the latest version ("jest-styled-components": "^7.0.2" & "styled-components": "^5.1.1").

Although the great majority of my tests pass, some of them have the following error message: No style rules found on passed Component.

After investigation, I noticed the only failing tests were the one where the styled components is exported directly, which are styled-components created with the babel macro. I tried to create those components without the babel macro and the tests are passing.

I created a repository to reproduce the bug:

https://github.com/dorianamouroux/styled-components-test-issue

The component:

import styled from 'styled-components'
import styledMacro from 'styled-components/macro'

export const Tag = styled.div`
  background-color: orange;
`

export const TagMacro = styledMacro.div`
  background-color: orange;
`

The spec:

import React from 'react'
import { shallow } from 'enzyme'

import { Tag, TagMacro } from './Tag'

describe('Tag', () => {
  it('no macro - has the right color', () => {
    // test passes, no babel macro
    expect(shallow(<Tag />)).toHaveStyleRule('background-color', 'orange')
  })

  it('macro - has the right color', () => {
    // fail, with the babel macro
    expect(shallow(<TagMacro />)).toHaveStyleRule('background-color', 'orange')
  })

  it('macro - has the right color', () => {
    // test passes, with a work around
    function WrapperTag() {
      return <TagMacro />
    }
    expect(shallow(<WrapperTag />).find(TagMacro)).toHaveStyleRule('background-color', 'orange')
  })
})

I'm using Enzyme, React and Styled-components macro. All with the latest version.

@dorianamouroux dorianamouroux changed the title No style rules when testing directly a styled components No style rules when testing directly a styled components from babel macro Jun 4, 2020
@VarunBalakrishna
Copy link

+1 same issue for me

@VarunBalakrishna
Copy link

VarunBalakrishna commented Jun 24, 2020

I posted a workaround for this here: #290 (comment)

@Tokimon
Copy link

Tokimon commented Jun 24, 2020

And i have posted the actual reason for this here (it is due to too aggressive class name filtering really):
#297 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants