Jest cannot use a snapshot test with MenuItem of material-ui

余生颓废 提交于 2020-01-24 20:10:06

问题


I work with typescript, react and material-ui. I'm trying to do a snapshot test with jest. But material-ui's MenuItem throw Invariant Violation: getNodeFromInstance: Invalid argument. exception.

Here is the component I'm trying to test:

import * as React from 'react';
import {MuiThemeProvider, Paper, Menu, MenuItem, Divider} from 'material-ui';

class App extends React.Component < any,any > {

  public render() {
    return (
      <MuiThemeProvider>
        <div>
          <Paper >
            <Menu desktop={true}>
              <MenuItem primaryText="Back"/>
              <MenuItem primaryText="Forward" disabled={true}/>
              <Divider/>
              <MenuItem primaryText="Recently closed" disabled={true}/>
              <MenuItem primaryText="Google" disabled={true}/>
              <MenuItem primaryText="YouTube"/>
            </Menu>
          </Paper>
        </div>
      </MuiThemeProvider>
    );
  }
}

export {App};

and here is the test

import * as React from 'react';
import {App} from '../menu';
import * as renderer from 'react-test-renderer';

describe('Layout', () => {
  it('renders correctly', () => {

    const layout = renderer.create(<App />).toJSON();

    expect(layout).toMatchSnapshot();
  });
});

And the errors messages

Invariant Violation: getNodeFromInstance: Invalid argument.

      at invariant (node_modules/fbjs/lib/invariant.js:44:15)
      at Object.getNodeFromInstance (node_modules/react-dom/lib/ReactDOMComponentTree.js:162:77)
      at Object.findDOMNode (node_modules/react-dom/lib/findDOMNode.js:49:41)
      at ListItem.applyFocusState (node_modules/material-ui/List/ListItem.js:319:43)
      at MenuItem.applyFocusState (node_modules/material-ui/MenuItem/MenuItem.js:206:26)
      at MenuItem.componentDidMount (node_modules/material-ui/MenuItem/MenuItem.js:175:12)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:265:25
      at measureLifeCyclePerf (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:75:12)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:264:11
      at CallbackQueue.notifyAll (node_modules/react-test-renderer/lib/CallbackQueue.js:76:22)
      at ReactTestReconcileTransaction.close (node_modules/react-test-renderer/lib/ReactTestReconcileTransaction.js:36:26)
      at ReactTestReconcileTransaction.closeAll (node_modules/react-test-renderer/lib/Transaction.js:206:25)
      at ReactTestReconcileTransaction.perform (node_modules/react-test-renderer/lib/Transaction.js:153:16)
      at batchedMountComponentIntoNode (node_modules/react-test-renderer/lib/ReactTestMount.js:69:27)
      at ReactDefaultBatchingStrategyTransaction.perform (node_modules/react-test-renderer/lib/Transaction.js:140:20)
      at Object.batchedUpdates (node_modules/react-test-renderer/lib/ReactDefaultBatchingStrategy.js:62:26)
      at Object.batchedUpdates (node_modules/react-test-renderer/lib/ReactUpdates.js:97:27)
      at Object.render (node_modules/react-test-renderer/lib/ReactTestMount.js:125:18)
      at Object.it (src/shell/containers/__tests__/app.test.tsx:8:29)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
      at process._tickCallback (internal/process/next_tick.js:109:7

If I remove MenuItem and Menu from component the test pass.

Can you help me to deal with this errors, please ?


回答1:


This is a fix until I come up with a better solution, but mocking your Menu or Menu Item will make the tests pass.

jest.mock('material-ui/MenuItem');

Unfortunately this would make the snapshot test superfluous since you cannot see changes made to your menu items.




回答2:


I think ReactDOM.findDOMNode is causing your test to fail. When I mocked it test case had some other error. I think you might want to shallow render the component to do snapshot testing else you will have to mock a lot of dependencies.

It is better than mocking MenuItem. MenuItem is internally using findDOMNode which when mocked lead to other errors.

You might want to take a look at this thread https://github.com/YouCanBookMe/react-datetime/issues/384



来源:https://stackoverflow.com/questions/44215783/jest-cannot-use-a-snapshot-test-with-menuitem-of-material-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!