Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest snapshot problem with component Fade #17992

Open
2 tasks done
alexiwonder opened this issue Oct 22, 2019 · 9 comments
Open
2 tasks done

Jest snapshot problem with component Fade #17992

alexiwonder opened this issue Oct 22, 2019 · 9 comments
Labels
bug 🐛 Something doesn't work external dependency Blocked by external dependency, we can’t do anything about it test

Comments

@alexiwonder
Copy link

alexiwonder commented Oct 22, 2019

  • The issue is present in the latest release.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

When Fade component has in={true}, Jest fails to render it with message TypeError: Cannot set property 'webkitTransition' of undefined

Stack trace:

      at Object.handleEnter [as onEnter] (node_modules/@material-ui/core/Fade/Fade.js:66:33)
      at Transition.performEnter (node_modules/react-transition-group/cjs/Transition.js:282:16)
      at Transition.updateStatus (node_modules/react-transition-group/cjs/Transition.js:253:14)
      at Transition.componentDidMount (node_modules/react-transition-group/cjs/Transition.js:195:10)
      at commitLifeCycles (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:12078:22)
      at commitLayoutEffects (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15276:7)
      at HTMLUnknownElement.callCallback (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11616:14)
      at Object.invokeGuardedCallbackDev (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11665:16)
      at invokeGuardedCallback (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:11718:31)       
      at commitRootImpl (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15012:9)
      at unstable_runWithPriority (node_modules/react-test-renderer/node_modules/scheduler/cjs/scheduler.development.js:815:12)
      at runWithPriority (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2038:10)
      at commitRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14853:3)
      at finishSyncRender (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14346:5)
      at performSyncWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:14318:9)        
      at node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2088:24
      at unstable_runWithPriority (node_modules/react-test-renderer/node_modules/scheduler/cjs/scheduler.development.js:815:12)
      at runWithPriority (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2038:10)
      at flushSyncCallbackQueueImpl (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2083:7)    
      at flushSyncCallbackQueue (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2071:3)        
      at scheduleUpdateOnFiber (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13696:9)        
      at scheduleRootUpdate (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16931:3)
      at updateContainerAtExpirationTime (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16959:10)
      at updateContainer (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:16979:10)
      at Object.create (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:17722:5)
      at Object.<anonymous>.test (src/components/CircleNumber.test.tsx:14:6)

Expected Behavior 🤔

Test should be able to render a composite component containing <Fade in={true}/>.

Steps to Reproduce 🕹

Steps:

import React from "react";
import renderer from "react-test-renderer";
import { Fade } from "@material-ui/core";

test("Fade should render correctly", () => {
   const tree = renderer
    .create(
        <Fade in={true}>
          <div>some text</div>
        </Fade>)
    .toJSON();
  expect(tree).toMatchSnapshot();
});

Context 🔦

Snapshot testing with Jest as explained here: https://jestjs.io/docs/en/snapshot-testing

Your Environment 🌎

Tech Version
Material-UI v4.5.1
React v16.9.0
Jest v24.9.0
@oliviertassinari oliviertassinari added test docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process. duplicate This issue or pull request already exists and removed docs Improvements or additions to the documentation good first issue Great for first contributions. Enable to learn the contribution process. test labels Oct 24, 2019
@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 24, 2019

Related to #17119. An update of our documentation is welcome.

@alexiwonder
Copy link
Author

alexiwonder commented Oct 24, 2019

@oliviertassinari I followed your comment, but still get the same error:

import React from "react";
import renderer from "react-test-renderer";
import { Fade } from "@material-ui/core";

test("Fade should render correctly", () => {
  const tree = renderer
    .create(
      <Fade in={true}>
        <div>some text</div>
      </Fade>,
      {
        createNodeMock: node => document.createElement(node.type as any);
      }
    )
    .toJSON();
  expect(tree).toMatchSnapshot();
});

@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 24, 2019

@alexiwonder Oh right, sorry, we track this problem in:
https://github.com/mui-org/material-ui/blob/cec2f0caf91f5ea235e1a62a8c6c2493075927ef/packages/material-ui/src/Fade/Fade.test.js#L33-L34

I don't remember what's wrong exactly with react-transition-group. Probably the same thing that holds us back with #13394: the usage of the deprecated findDOMNode() method.

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work external dependency Blocked by external dependency, we can’t do anything about it test and removed duplicate This issue or pull request already exists labels Oct 24, 2019
@Musacoli
Copy link

Musacoli commented Feb 18, 2020

If anyone is still facing this issue, try checking out the documentation and using one of the test utils there.

This helped me for my case, I created a render HOC to use for each test that had a MaterialUI component.

import React from 'react';
import Theme from '../../components/App/shared/ThemeProvider';
import { BrowserRouter as Router } from 'react-router-dom';
import { createRender } from '@material-ui/core/test-utils';

export const withMaterial = (Component: any, props?: any) => {
  const render = createRender();
  return render(
      <Router>
        <Theme>
          <Component {...props} />
        </Theme>
      </Router>
    );
};

What my test looked like:

import LoadingOverlay from '../../LoadingOverlay';
import {withMaterial} from 'utils/tests';

describe('Loading overlay', () => {

  it('should render as expected', function() {
    const tree = withMaterial(LoadingOverlay, { isForm: true });
    expect(tree).toMatchSnapshot();
  });
});

And this got it to pass.

@tqwewe
Copy link

tqwewe commented Jul 14, 2020

Can this be done without enzyme? I am using Jest.

@tqwewe
Copy link

tqwewe commented Jul 14, 2020

I found a solution to mock the Fade component in Material UI.

jest.mock('@material-ui/core', () => {
  const materialUI = jest.requireActual('@material-ui/core');
  return {
    ...materialUI,
    Fade: jest.fn(({ children }) => children),
  }
});

describe('Component', () => {
  const { ThemeProvider } = jest.requireMock('@material-ui/core');

  it('renders correctly', () => {
    const tree = renderer.create(
      <ThemeProvider theme={theme}>
        <Component />
      </ThemeProvider>,
    ).toJSON();
    expect(tree).toMatchSnapshot();
  });
});

@oliviertassinari
Copy link
Member

Correct solution: #17119 (comment)

@ahummel25
Copy link

ahummel25 commented Mar 18, 2021

@oliviertassinari I'm still getting this error when implementing solution from #17119 (comment)

I'm unit testing a component with react-test-renderer.

My component is using a few material-ui components:

import Drawer from '@material-ui/core/Drawer';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Divider from '@material-ui/core/Divider';

Partial test code:

const tree = renderer.create(<Navbar />, {
  createNodeMock: (node: ReactHTMLElement<HTMLElement>): HTMLElement =>
    document.createElement(node.type)
});

...
...

const as = tree.root.findAllByType('a');

// Error is thrown here
act(() => {
  as[0].props.onClick(e); // Should open Mui Drawer
});

Error:

TypeError: Cannot set property 'webkitTransition' of undefined

      103 |
      104 |       // Open mobile drawer
    > 105 |       act(() => {
          |       ^
      106 |         as[0].props.onClick(e);
      107 |       });
      108 |

      at node_modules/@material-ui/core/Fade/Fade.js:100:33
      at Object.onEnter (node_modules/@material-ui/core/Fade/Fade.js:84:11)

I've tried mocking Fade as well, but that is throwing another stack of errors. Although it was working for a while until a recent Mui version upgrade I believe.

jest.mock('@material-ui/core/Fade');
ForwardRef(Fade)(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.

      103 |
      104 |       // Open mobile drawer
    > 105 |       act(() => {
          |       ^
      106 |         as[0].props.onClick(e);
      107 |       });
      108 |

at reconcileChildFibers (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:5475:23)
at reconcileChildren (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:7984:28)
at updateForwardRef (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:8050:3)
at beginWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:10015:16)
at performUnitOfWork (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13800:12)
at workLoopSync (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13728:5)
at renderRootSync (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13691:7)
at performSyncWorkOnRoot (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13404:18)
at node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2537:26
at unstable_runWithPriority (node_modules/scheduler/cjs/scheduler.development.js:646:12)
at runWithPriority (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2486:10)
at flushSyncCallbackQueueImpl (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2532:9)
at flushSyncCallbackQueue (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:2519:3)
at batchedUpdates (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:13459:7)
at Object.act (node_modules/react-test-renderer/cjs/react-test-renderer.development.js:15280:14)

@luancardosolc
Copy link

luancardosolc commented May 28, 2023

it worked well for me:

jest.mock('@material-ui/core/Fade');

My dependencies:
"enzyme": "~3.11.0",
"enzyme-adapter-react-16": "1.15.6",
"enzyme-to-json": "~3.6.1",
"jest": "~26.6.3",
"jest-enzyme": "~7.1.2",
"react": "~16.7.0",
"react-dom": "~16.7.0",
"react-router-redux": "~4.0.8",
"react-test-renderer": "~16.7.0",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work external dependency Blocked by external dependency, we can’t do anything about it test
Projects
None yet
Development

No branches or pull requests

6 participants