withStyles break enzyme tests
See original GitHub issueSeems like when using the withStyles HOC it’ll actually break my Jest test. It creates children elements where it shouldn’t.
Here is the code to reproduce it:
import React from 'react';
import ReactDOM from 'react-dom';
import { shallow } from 'enzyme';
import withStyles from 'material-ui/styles/withStyles';
import LoadingWrapper from './LoadingWrapper';
const ReproBug = ({ loading, children }) => {
if (loading) {
return <span>Loading...</span>;
}
return children;
}
const ReproBugWithStyles = withStyles({})(ReproBug);
const children = <span>Dummy Children!</span>;
it('should not render children if loading is true', () => {
const wrapper = shallow(
<ReproBug loading={true}>{children}</ReproBug>
);
expect(wrapper.contains(children)).toEqual(false);
});
it('should not render children if loading is true', () => {
const wrapper = shallow(
// using ReproBugWithStyles instead of ReproBug
<ReproBugWithStyles loading={true}>{children}</ReproBugWithStyles>
);
expect(wrapper.contains(children)).toEqual(false); //<--- this check fails, it is actually true
});
I use the create-react-test
and simply run yarn test
. (they use Jest and enzyme IIRC)
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:20 (6 by maintainers)
Top Results From Across the Web
withStyles break enzyme tests #9266 - mui/material-ui - GitHub
Seems like when using the withStyles HOC it'll actually break my Jest test. It creates children elements where it shouldn't.
Read more >Material-UI withStyles components wont render when testing ...
I have a form I'm exporting as: export default withStyles(styles)(ShuttleForm);. But I can test the component ShuttleForm, or any component ...
Read more >Unit Testing in React: Full Guide on Jest and Enzyme Testing
In order to test inline styles, you need to duplicate object with styles in your test; if styles object changes, you must change...
Read more >Using enzyme/shallow/mount and a theme with custom ...
Component that leverages theme + withStyles with custom variable doesn't fail. ... We had to refactor way too many test without anything actually...
Read more >Unit Test your React app with Jest, Jest-dom & Enzyme
Enzyme is a JavaScript Testing utility built just for React that makes it easier to test your React Components' output. You can also...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Skaronator The issue isn’t about the children elements but with the intermediary element, it’s creating. The
shallow()
API of enzyme only render one level depth. Any higher-order component is going to obfuscate the children. You have different workarounds. I would encourage you to refer to the enzyme documentation. Still, here they are:mount()
.dive()
createShallow()
public APIuntil()
helperI solved my issue, was an obvious fix. I was using the component name in quotes eg .find(‘Avatar’) instead of importing it into the test and calling it directly .find(Avatar) - no quotes. That worked.