meinaart / cypress-plugin-snapshots

Plugin for snapshot tests in Cypress.io
MIT License
497 stars 117 forks source link

(feat) Add the option to filter out patterns #78

Closed louis-vinchon closed 4 years ago

louis-vinchon commented 4 years ago

Is your feature request related to a problem? Please describe. It is currently impossible to ignore anything while snapshoting a DOM element or a DOM tree. Which means that snapshot testing is simply impossible for application where ids are generated, e.g. in Angular ag-grid generates row ids dynamically therefore two snapshots of the grid will never be the same since all ids have been changed.

Describe the solution you'd like An option to ignore elements attributes, or the possibility to pass a function to freely str.replace() anything on the stringified DOM tree.

Describe alternatives you've considered Selecting several elements that do not have generated ids with cy.get(). Doesn't work as this feature is broken/missing.

louis-vinchon commented 4 years ago

I would have happily done it myself but the lack (absence) of documentation killed my motivation.

meinaart commented 4 years ago

This might work (untested):

cy.get('div')
  .its('outerHtml')
 .then(html => html.replace(/ id=\".*?\"/g, ''))
  .toMatchSnapshot();
meinaart commented 4 years ago

@K41eb let me know if it works. If it does not, I will reopen this issue.