Skip to content

Proposal: Snapshot property matchers for render output #8618

@TLadd

Description

@TLadd

🚀 Feature Proposal

Property Matchers solve a common issue with snapshot tests, but only work for objects. The proposed feature is to extend that functionality to the rendered output html string as well.

Motivation

The same motivation as it was for object property matchers. Oftentimes, there are properties in rendered html that are non-deterministic. Randomly-generated ids for labels/form controls are a big one. Currently, the only way I know around this is to mock whatever is generating the id to make it deterministic, usually returning a constant value. This can be problematic though if you're using something like react-testing-library to query elements by their label text, since then all ids are identical and it's unclear which label corresponds to which input.

The ability to ignore these fields when deciding if a snapshot matches or not solves this issue.

Example

Similar to object property matchers

it('renders correctly', () => {
  const tree = renderer
    .create(<Link page="http://www.facebook.com">Facebook</Link>)
    .toJSON();
  expect(tree).toMatchSnapshot({
    for: expect.any(String),
    id: expect.any(String),
  });
});

// Snapshot
will check the matchers and pass `
<div>
  <label
    class="form__label color color--cod-gray"
    for=Any<string>
  >
  <input id=Any<string> />
</div>
`;

Pitch

This would make snapshot testing easier for some pretty common cases that currently cause difficulty.

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