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

Theme is not applied to styled components props in Vitest #2955

Closed
MrkacekVladimir opened this issue Dec 7, 2022 · 7 comments
Closed

Theme is not applied to styled components props in Vitest #2955

MrkacekVladimir opened this issue Dec 7, 2022 · 7 comments

Comments

@MrkacekVladimir
Copy link

Current behavior:

I am using latest React, Vitest, Vite, Emotion and during testing ThemeProvider does not populate theme prop in styled.div syntax. This happens only during testing.

To reproduce:

https://codesandbox.io/s/vitest-and-emotion-styled-o3i7jm

  1. Run "npm run test-watch"
  2. See in console output, that styled does not get valid theme prop.

image

  1. If you run the application, styled gets populated with theme prop as it should

image

Expected behavior:

ThemeProvider should provide me correct theme prop when using styled in testing environment.

Environment information:

  • react version: 18.2.0
  • @emotion/react version: 11.10.5
@Andarist
Copy link
Member

Andarist commented Dec 7, 2022

Please recheck if your node_modules don't contain more than one version of @emotion/react or if your test setup doesn't instantiate @emotion/react more than once.

@MrkacekVladimir
Copy link
Author

None of that seems to be the case. If I switch to styled-components and do the exact same setup with typings definition, ThemeProvider, babel plugin... the styled API gets correct theme.

@MrkacekVladimir
Copy link
Author

MrkacekVladimir commented Dec 7, 2022

I did some further testing in the codesandbox attached above and it seems like the issue in the babel plugin.
If I remove
babel: { plugins: ['@emotion/babel-plugin'], },
from vite.config.ts, theme is getting passed into styled API.

Is there a way you could confirm that this issue is on emotion side or I should direct it on Vitest?

@Andarist
Copy link
Member

Andarist commented Dec 8, 2022

Yes, I've found out yesterday that the Babel plugin was messing things up - but didn't have time to respond here to you. I hope that by removing the Babel plugin temporarily you are able to unblock yourself.

That being said - this is Vite's bug. I've created a PR that explains the issue and fixes it in Vite here: vitejs/vite#11259

@MrkacekVladimir
Copy link
Author

Thank you very much for the effort. Currently it is a blocker we cannot really remove since without the babel plugin we are not able to use component referencing in styled. Hope this gets resolved soon :)

@Andarist
Copy link
Member

Andarist commented Dec 8, 2022

Since this isn't really our issue - I will close this one here. Let's wait on the Vite maintainers to handle my PR.

@Andarist Andarist closed this as completed Dec 8, 2022
@santosh1997
Copy link

#2351 (comment)

Facing the similar issue after jest 29 upgrade.
Can you please help?

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

No branches or pull requests

3 participants