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

Issue with @emotion/babel plugin - variable reported as not defined #2745

Closed
adrianosmond opened this issue May 2, 2022 · 4 comments · Fixed by #2858
Closed

Issue with @emotion/babel plugin - variable reported as not defined #2745

adrianosmond opened this issue May 2, 2022 · 4 comments · Fixed by #2858

Comments

@adrianosmond
Copy link

Current behavior:

Given a very specific set of styles:

const StyledRoot = styled.div`
  ${fullWidth}

  button {
    @media (min-width: 768px) {
      color: red;
    }
  }
`;

the fullWidth variable which has been imported from a different file is reported as not defined when using @emotion/babel-plugin inside a Jest test. The styles work when @emotion/babel-plugin is not used. Making minor adjustments to the styles, like removing the @media rule, or changing the order of the nesting of button and @media mean also result in the variable no longer being reported as not defined.

To reproduce:

  1. Clone https://github.com/adrianosmond/emotion-babel-plugin-issue-repro
  2. npm i
  3. npm run test

The tests fail like so

 FAIL  src/Component.test.jsx
  ● Test suite failed to run

    ReferenceError: fullWidth is not defined

      4 |
      5 | const StyledRoot = styled.div`
    > 6 |   ${fullWidth}
        |     ^
      7 |
      8 |   button {
      9 |     @media (min-width: 768px) {

Expected behavior:

The test should pass. The variable fullWidth has been defined - it is imported on line 3 of src/Component.jsx

Environment information:

  • react version: 17..0.2
  • @emotion/styled version: 11.8.1
  • @emotion/babel-plugin version: 11.9.2
@Andarist
Copy link
Member

Andarist commented May 3, 2022

Thanks for filing a report - I've narrowed down the issue to the toInputTree function and I think that I've also recognized how the logic is broken. Gonna be working on a fix soon.

@adrianosmond
Copy link
Author

Awesome - thanks for all your hard work!

@abel-varga-ekata
Copy link

abel-varga-ekata commented Aug 18, 2022

Is there an update on this? We've bumped into this exact issue. Our workaround is to fall back and use the old babel-plugin-emotion when running the tests, if anyone's interested.

isTestEnv ? 'babel-plugin-emotion' : ['@emotion', { autoLabel: 'always' }],

@Andarist
Copy link
Member

Sorry, this somehow slipped my mind back in May and I've forgotten about this issue. I've prepared a fix for this: #2858

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