enzymejs/enzyme-matchers

toMatchElement does not allow matching only some fields

Open

#241 创建于 2018年6月19日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)JavaScript (896 star) (125 fork)batch import
Bug - EnzymeMatchersHelp wanted

描述

I see there is a closed issue #163. It works around the issue but it does not look like it fully solves problem. I'm opening this one to be more specific and provide a test case that wrongly fails.

Enzyme's matchesElement only matches the props that the user supplies. If there are additional props that aren't specified in the match it still passes.

However, toMatchElement functions differently. It requires that ALL props match. This is extremely inconvenient and often impractical to specify all the other props.

Here's an example the failing behavior:

it('jest-enyzme bug', () => {
  const wrapper = shallow(
    <form>
      <input
        type="text"
        id="description"
        placeholder="additonal prop for negative testing"
      />
    </form>
  )
  const input = wrapper.find('input')

  // Passes when the given subset of props match
  expect(input.matchesElement(<input id="description" />)).toEqual(true)

  // Detects when one of the props does not match
  expect(input.matchesElement(<input id="noMatch" />)).toEqual(false)

  // This test should NOT fail.  enzyme-matchers does not function the same.
  // It wrongly requires ALL the props to match.
  expect(input).toMatchElement(<input id="description" />, { ignoreProps: false })
})

贡献者指南

toMatchElement does not allow matching only some fields · enzymejs/enzyme-matchers#241 | Good First Issue