Skip to content

How to ignore certain html attributes in the snapshots created using the toMatchSnapshot() method provided by Jest #5014

@miteshravindra

Description

@miteshravindra

Do you want to request a feature or report a bug?
I want to request a feature

What is the current behavior?
Currently the toMatchSnapshot() function of Jest stores a serialized html version of the component in test

What is the expected behavior?
Is it possible to create a feature wherein one can instruct the function to store the serialized html version of the component ignoring certain attributes of it so that it only compares the html of the component with the required attributes

For Example:
Consider a react component under test

class Link extends React.Component {
  render() {
    return (
      <p>
        <label id='first'>Example<label>
        <input id='second' className="third" />
      </p>
    )
  } 
}

so when i write a test case for it

it('renders correctly', () => {
const tree = renderer
.create(<Link/>)
.toJSON();
expect(tree).toMatchSnapshot();
});

it will store the snapshot as
exports[renders correctly 1] =

 <p>
        <label id='first'>Example<label>
        <input id='second' class ="third"/>
      </p>

;

Is there a way I can simply write

it('renders correctly', () => {
const tree = renderer
.create(<Link/>)
.toJSON();
expect(tree).ignore({
class,
id
}).toMatchSnapshot();
});

so the function will ignore the class and id and create the snapshot as
exports[renders correctly 1] =

 <p>
        <label>Example<label>
        <input />
      </p>
;

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions