-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
styled function getting wrong theme object in Jest tests #30081
Comments
It doesn't look like this bug report has enough info for one of us to reproduce it. Please provide a CodeSandbox (https://material-ui.com/r/issue-template-latest), a link to a repository on GitHub, or provide a minimal code example that reproduces the problem. Note: from what you describe, you are testing component style in different breakpoints. I suggest you checkout chromatic which is a tool from the storybook team for visual testing. I think it will give you more confidence than using Jest. Feel free to neglect my suggestion if you already have considered it. |
@siriwatknp thanks for the reply. My mention of breakpoints was more of a detail about my use, than an issue. The problem is that the theme object that's coming into the |
@michaelfaith make sure you use the |
Thanks. Yeah, I double checked and we're definitely using the correct import path. I tried to recreate it in the codesandbox (https://codesandbox.io/s/mui-styled-issue-mpk2u?file=/src/Demo.tsx), but couldn't get it to fail there. I even used our |
Can you provide the link to the repo? |
The code sandbox is here: https://codesandbox.io/s/mui-styled-issue-mpk2u?file=/src/Demo.test.tsx
codesandbox doesn't appear to support jest.mock like that. Do you think that could impact this? I'll try it without that when I login in the morning and see if that helps. But I'm really grasping at straws at this point. |
Removing that mock didn't make a difference. |
I found a stackoverflow post with someone seemingly having the exact same issue: https://stackoverflow.com/questions/69328284/jest-rtl-failure-mui-v5-emotion-theme-issue I've encouraged them to post a reproduction here, if they have one. I've been struggling to recreate it outside of our project. I didn't think to mention before, we're working in a yarn (1.x) workspaces environment, in case that is helpful at all. |
will try to reproduce it this week. will post an update on this issue. |
@siriwatknp I am the individual that posted the SO post that @michaelfaith is referencing. I too posted an issue here, but it got closed for not having a code sandbox. Unfortunately, there is something within our corporate projects that is to blame it looks like. I can get the ThemeProvider to work and inject the theme just fine if I just do a very basic example in a code sandbox. |
@siriwatknp @shaneajeffery we were finally able to get this working. And it's not entirely clear how. I tried incrementally reducing the packages from our project to isolate the dependencies. It started working at some point, and so I started slowly adding dependencies back, and it never stopped working. So at the end of that exercise we ended up with the same
|
@siriwatknp @michaelfaith It is most certainly a dependency issue. I went ahead and just updated all of my dependencies in my
Now, my issue is gone and I don't need to use the The issue has to be the older version of Emotion + MUI + Jest not playing well together. If someone else runs into this issue, I would ensure that you are on the latest version of all of the libraries and seeing where that gets you. I wish I had more of a clear-cut answer, but this issue is really just a trial-and-error bit due to the sheer volume of dependencies in the tree. |
@michaelfaith @shaneajeffery Thanks for the info, it will save a lot of time for other people! Note |
@siriwatknp Yeah, that's a good point. We have this declared in our package.json.
It looks like storybook is pulling in the older emotion in addition to what we're pulling in. I wonder if this was contributing to the behavior we were experiencing.
|
Duplicates
Latest version
Current behavior 😯
I've just migrated one of my components from JSS to the styled components, as described on the migration page. Everything works fine in storybook, and the correct (custom) theme object is being applied. But when running unit tests, if I check applied styles to the rendered component, it's not getting the custom theme. It only ends up with the default theme object within
styled
.Expected behavior 🤔
Should pass in correct theme object in Jest environment.
Steps to reproduce 🕹
Steps:
createTheme
styled
and use elements from the theme within the stylestoHaveStyle
by wrapping the component in a ThemeProvider with the custom theme and rendering with@testing-library/react
.Context 🔦
We need to be able to test that the correct colors are being applied, depending on if it's dark or light theme, and under different breakpoints.
Your environment 🌎
System:
OS: Windows 10 10.0.18363
Binaries:
Node: 14.17.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.15 - ~\AppData\Roaming\npm\yarn.CMD
npm: 7.18.1 - ~\src\quantum\node_modules.bin\npm.CMD
Browsers:
Chrome: 96.0.4664.45
Edge: Spartan (44.18362.1593.0)
The text was updated successfully, but these errors were encountered: