代码之家  ›  专栏  ›  技术社区  ›  user3142695

如何测试直接返回的组件?

  •  1
  • user3142695  · 技术社区  · 6 年前

    如何测试直接返回的组件?我使用jest/enzyme进行测试,我的示例组件只返回另一个组件。 我的测试失败了,但我不知道如何解决这个问题。

    实例js

    import Something from './Something'
    
    export const Example = (props) => {
      return <Something {...props} />
    }
    

    实例测验js

    import { shallow } from 'enzyme'
    import { Example } from './Example'
    import { Something } from './Something'
    
    test('should render Something component', () => {
      const wrapper = shallow(<Example {...props} />)
      expect(wrapper.find(Something)).toHaveLength(1)
    })
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   t3__rry    6 年前

    您好,您可以通过执行 snapshot 测试:

    describe('Something component test suite', () => {
      test('It renders correctly', () => {
       const wrapper = shallow(<Example {...props} />)
    
       expect(wrapper).toMatchSnapshot()
      })
    })
    

    这是 开玩笑 关于快照测试的文档 https://jestjs.io/docs/en/snapshot-testing

        2
  •  0
  •   aseferov    6 年前

    试试这个

    test('should render Something component', () => {
      const wrapper = shallow(<Example {...props} />)
      expect(wrapper.find(Something).length).toEqual(1)
    })