make snapshot more robust

This commit is contained in:
jfh
2020-10-16 23:59:30 +02:00
parent b957a3c5aa
commit 8a1607a8cd
3 changed files with 24 additions and 6 deletions

View File

@@ -23,3 +23,26 @@
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
// remove the style attributes that is causing differences in snapshots
const ngAttributes = ['style'];
Cypress.Commands.add(
'cleanSnapshot',
{
prevSubject: true
},
(subject, snapshotOptions) => {
let html = subject[0].outerHTML;
for (const attribute of ngAttributes) {
const expression = new RegExp(`${attribute}[^= ]*="[^"]*"`, 'g');
html = html.replace(expression, '');
}
html = html.replace(/<!--[\s\S]*?-->/g, '');
const sanitisedBody = new DOMParser().parseFromString(html, 'text/html').querySelector('body');
return cy.wrap(sanitisedBody.firstChild).toMatchSnapshot();
}
);