Enzyme test: TypeError: expect(…).find is not a function

半世苍凉 提交于 2019-12-11 11:54:29

问题


Why is .find not a function in the code context below?

import React from 'react';
import { shallow } from 'enzyme';
import toJson from 'enzyme-to-json';
import { AuthorizedRoutesJest } from './AuthorizedRoutes';

// Components
import {
  Main
} from '../../components';

const wrapper = shallow(<AuthorizedRoutesJest />);

describe('<AuthorizedRoutes /> component', () => {
  it('should render', () => {
    const tree = toJson(wrapper);
    expect(tree).toMatchSnapshot();
    expect(wrapper).toHaveLength(1);
  });

  it('should contain a Main component', () => {
    expect(wrapper).find(Main).toHaveLength(1);
  });
});

Summary of all failing tests FAIL client/containers/Routes/AuthorizedRoutes.test.js

AuthorizedRoutes component › should contain a Main component

TypeError: expect(...).find is not a function


回答1:


I was using .find incorrectly

Here is an example of how to use find:

it('should contain a ConnectedRouter component', () => {
  expect(wrapper.find(ConnectedRouter)).toHaveLength(1);
});

it('should contain a Switch component', () => {
  expect(wrapper.find(Switch)).toHaveLength(1);
});

it('should contain 7 Route components', () => {
  expect(wrapper.find(Route)).toHaveLength(7);
});



回答2:


This is in addition if you want to find a component with specific testID props

it('should render the date when the message was sent', () => {
   expect(chatBubbleComponent.findWhere((node) => node.prop('testID') === 'chat_sent_at')).toHaveLength(1);
});


来源:https://stackoverflow.com/questions/47395098/enzyme-test-typeerror-expect-find-is-not-a-function

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