question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enzyme shallowRender doesn't work with MUI

See original GitHub issue

I 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)

Edit create-react-app

(See “should render” test)

  1. Create a React project with create-react-app
  2. Make a Component
  3. 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:closed
  • Created 6 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
oliviertassinaricommented, Mar 20, 2018

@eluchsinger You have a single HOC, use the dive property over the untilSelector. It sounds like we haven’t updated the shallow recursion logic to handle fragments.

const shallow = createShallow({ dive: true });
0reactions
eluchsingercommented, Mar 20, 2018

@oliviertassinari I have created new tests with Enzyme and if I use their shallow and it works. Maybe I am using createShallow wrong?

https://codesandbox.io/s/m7qkyzk9op

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found