Enzyme shallowRender doesn't work with MUI
See original GitHub issueI am pretty new to the testing framework Enzyme. Maybe I misunderstood something about shallowRender.
I am trying to create tests with Enzyme and it turns out I can’t use shallowRender. I think the reason is the wrapping of MUI with the withStyles
function.
I have the following code that I want to test:
function NavMenu({ classes, projects }: Props) {
return (
<div className={classes.root}>
<List component="nav" className={classes.root}>
{projects &&
projects.map(project => (
<ProjectNavItem project={project} key={project.id} />
))}
</List>
</div>
);
}
export default withStyles(styles, { withTheme: true })(NavMenu);
When I do a shallowRender, I can’t see for example the div
rendered (or the List
). I only see the <NavMenu />
. This is an example of how I do shallowRender:
describe("<NavMenu />", () => {
let shallow;
beforeEach(() => {
shallow = createShallow();
});
it("should render", () => {
const wrapper = shallow(<NavMenu projects={[mockProject()]} />);
expect(wrapper).toContain(<div>); // An example (untested code).
});
});
Expected Behavior
I would expect the wrapper to contain at least one level of children. For example:
<NavMenu>
<div></div>
</NavMenu>
Current Behavior
Currently, I get only the root tag I am trying to render:
<NavMenu />
Steps to Reproduce (for bugs)
(See “should render” test)
- Create a React project with create-react-app
- Make a Component
- Make a test for the component with a shallowRender using Enzyme and Jest.
Context
I want to be able to test using shallowRender
and not mount
.
Your Environment
Tech | Version |
---|---|
Material-UI | 1.0.0-beta.38 |
React | 16.2.0 |
browser | Not important |
enzyme | 3.3.0 |
enzyme-adapter-react-16 | 1.1.1 |
jest | 20.0.4 |
- I have searched the issues of this repository and believe that this is not a duplicate.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Enzyme Shallow Rendering and Material-UI - reactjs
I'm very new to tests and I am trying to implement them in an existing react project. I have a button component which...
Read more >Using enzyme/shallow/mount and a theme with custom ...
Component that leverages theme + withStyles with custom variable doesn't fail. I have a component that I wrap with withStyles .
Read more >Shallow Rendering API - Enzyme - GitHub Pages
Shallow rendering is useful to constrain yourself to testing a component as a unit, and to ensure that your tests aren't indirectly asserting...
Read more >Unit Testing in React: Full Guide on Jest and Enzyme Testing
Analyze each directory and define which components are independent – namely, their rendering doesn't depend on the other components; they are self-completed ...
Read more >Enzyme's shallow is rendering children's children?-Reactjs
Related Query · Enzyme shallow render is rendering children components · Why doesn't enzymes find() with component props work the same way when...
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
@eluchsinger You have a single HOC, use the
dive
property over theuntilSelector
. It sounds like we haven’t updated the shallow recursion logic to handle fragments.@oliviertassinari I have created new tests with Enzyme and if I use their
shallow
and it works. Maybe I am usingcreateShallow
wrong?https://codesandbox.io/s/m7qkyzk9op