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.

withStyles break enzyme tests

See original GitHub issue

Seems 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

19reactions
oliviertassinaricommented, Dec 5, 2017

It creates children elements where it shouldn’t

@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:

12reactions
jondbmcommented, Dec 8, 2018

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

Read more comments on GitHub >

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

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