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

时间:2019-01-21 08:02:39

标签: javascript unit-testing jestjs enzyme

如何测试直接返回的组件?我正在使用jest / enzyme进行测试,而我的示例组件只是返回了另一个组件。 我的测试为此失败,但是我不知道该如何解决。

example.js

import Something from './Something'

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

example.test.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 个答案:

答案 0 :(得分:0)

嗨,您可以通过进行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

jest 文档

答案 1 :(得分:0)

尝试一下

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