Skip to content

toMatchElement does not allow matching only some fields #241

Open
@brennancheung

Description

@brennancheung

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions