enzymejs/enzyme-matchers

toMatchElement does not allow matching only some fields

Open

#241 opened on Jun 19, 2018

View on GitHub
 (1 comment) (0 reactions) (0 assignees)JavaScript (896 stars) (125 forks)batch import
Bug - EnzymeMatchersHelp wanted

Description

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 })
})

Contributor guide