-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 context not working with styled components in jest #2351
Comments
https://github.com/emotion-js/emotion/blob/main/packages/react/src/theming.js ThemeContext is just a simple React |
Unfortunately, I will not be able to provide anything like that as the code base I am working on is private and has some custom tooling/packages involved. I can try to reproduce any bits that would be helpful to debug, such as the jest, babel, and webpack configs involved. I have a hunch it is babel config related but cannot seem to narrow down exactly what config works or why. I was hoping to maybe get some insight into what dependencies emotion requires for the theme context to work properly, or what could be causing it behave strangely. |
This sounds useful, let me play around with it a bit and see what all I discover. The 'theme' object is always just an empty object by the way '{}' |
Update on this. I tested with using So, it seems like the Below is a code snippet of what I tested with, please refer to comments above to see what I had previously
|
I have dug into this a bit and found that my project includes a Not sure why this package is here, as it appears to have been replaced by |
Anyway, |
Yep, this means that for some reason your dependency tree is wrong and you load separate versions of our libraries. |
I was able to finally resolve this by using the babel-macro libs So, I assume the issue lied within my app's babel/webpack configs across dev and test environment. Anyways, thanks @iChenLei and @Andarist for your input and help :) |
I hit the same issue yesterday using the following app structure:For some reason, within the library, the Here's what I found on the styles serialization debugging: Then I checked the bundle generated by the library and I found that it has its own instance of the But why did the emotion/packages/react/src/theming.js Lines 6 to 11 in 6e0e388
Combining that, here're the findings I made in the bundled library code: The solution I found for now is to mark all the
|
Thanks @oleksandr-danylchenko for the detailed investigation 👍 For my case, solving the below warning that I saw in the console solved the theme context issue:
I'm using Create React App, so this solution worked for me. Check out other solutions in that thread for other bundlers. My case is similar to @oleksandr-danylchenko's case, where I have a "Main App" and "Component Library": Component Library {
"dependencies": {
"@emotion/styled": "^11.11.0",
}
} Main App {
"dependencies": {
"@emotion/styled": "^11.9.3",
"my-component-library": "^0.1.0", // My Component Library
}
} Since my component library is using a different Emotion version than my main app, it caused two versions of Emotion being installed in my Main App. |
I am upgrading to emotion v11
I am getting errors in my unit tests indicating that the
theme
context is not provided properly, it is just an empty object{}
Sample render util
Sample test case
Sample component
The text was updated successfully, but these errors were encountered: