Shallow testing (enzyme) component with custom theme and mui-styles' makeStyles hook
See original GitHub issueCan you add a way (or add documentation) to shallow test a component with a custom theme and using styling hooks?
Using unwrap worked with withStyles to shallow mount a component, but now with hook styling there is no longer a HOC styling wrapper. Dive is also not working as expected.
ex.
const theme = createTheme(/*custom theme here */);
const useStyles = makeStyles(theme => ({
customStyle: {
color: /* custom theme value here */
}));
const MyComponent = () => {
const classes = useStyles();
return (
<div className={classes.customStyle} />
);
}
I’m use shallow rendering (enzyme), and I wrap this component in the themeprovider from mui-styles. Diving into the themeprovider I get an error. Can you either add documentation surrounding this or add a clear way to test styling hooks?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:15 (6 by maintainers)
Top Results From Across the Web
Shallow testing (enzyme) component with custom theme and ...
I'm use shallow rendering (enzyme), and I wrap this component in the themeprovider from mui-styles. Diving into the themeprovider I get an error ......
Read more >How do I mock and test MaterialUI - makeStyles - Stack Overflow
I manual mocked makeStyles globally, created the theme on the go and bound it to the original makeStyles because the custom theme wasn't...
Read more >Shallow Testing Hooks with Enzyme - Carbon Five Blog
Hooks allow you to ditch class components and stick with functional components, even when you need local state and lifecycle methods. They also ......
Read more >Testing with Jest and Enzyme in React — Part 4 (shallow vs ...
The difference between shallow and mount using an example. Step 1 — Create a new component named Form.js. Open the project named “testing-demo-app”....
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 Free
Top 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

@BenBrewerBowman I was running into the same issues and I’ve found a way around using jest mocks. Here are the steps for my workaround:
<root>/__mocks__/@material-ui/styles.tsYou should now be able to use shallow rendering on all your tests. Just a couple of notes:
import { makeStyles } from "@material-ui/styles";edit: updated mock to be much simpler (inspired by: https://github.com/mui-org/material-ui/issues/14357#issuecomment-470324935)
@BenBrewerBowman I believe we already have a discussion for this problem: #11864.
I would encourage you to use the mount API and to forget about the existence of the shallow one, or even to consider using react-testing-library. I don’t think that enzyme shallow API support the context provider pattern yet, e.g. https://github.com/airbnb/enzyme/issues/2103. React shallow test utils might not support it either: https://github.com/facebook/react/pull/14329.
I understand the concern. Did you benchmark it? We are migration the whole Material-UI test suite away from the shallow API. I personally think that the code refactoring flexibility the mount API provides worth a slower test suite. We haven’t benchmarked any significant difference yet. The tests on the master branch take 32s, the tests on the next branch, where we started the mount migration take 42s (but we added many new tests).