From 8a1607a8cd15b6c1f928480717106b69398449df Mon Sep 17 00:00:00 2001 From: jfh Date: Fri, 16 Oct 2020 23:59:30 +0200 Subject: [PATCH] make snapshot more robust --- cypress/integration/ui/scenario.js | 5 +---- cypress/integration/unit/test1.js | 2 -- cypress/support/commands.js | 23 +++++++++++++++++++++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cypress/integration/ui/scenario.js b/cypress/integration/ui/scenario.js index c84dbe11..47bd5f2a 100644 --- a/cypress/integration/ui/scenario.js +++ b/cypress/integration/ui/scenario.js @@ -3,10 +3,7 @@ import { } from '../../support/ui-test-helper.js'; const testSnapshot = () => { - // cy.get('#tool_source').click({force: true}); - // cy.get('#svg_source_textarea').invoke('val').toMatchSnapshot(); - // cy.get('#tool_source_save').click({force: true}); - cy.get('#svgcontent').toMatchSnapshot(); + cy.get('#svgcontent').cleanSnapshot(); }; describe('use various parts of svg-edit', function () { diff --git a/cypress/integration/unit/test1.js b/cypress/integration/unit/test1.js index 4b23449f..26cb407e 100644 --- a/cypress/integration/unit/test1.js +++ b/cypress/integration/unit/test1.js @@ -4,8 +4,6 @@ import '../../../instrumented/editor/jquery-ui/jquery-ui-1.8.17.custom.min.js'; import SvgCanvas from '../../../instrumented/svgcanvas/svgcanvas.js'; -// import '../../../instrumented/' - describe('Basic Module', function () { // helper functions /* diff --git a/cypress/support/commands.js b/cypress/support/commands.js index ca4d256f..c64ca1ef 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -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(//g, ''); + + const sanitisedBody = new DOMParser().parseFromString(html, 'text/html').querySelector('body'); + + return cy.wrap(sanitisedBody.firstChild).toMatchSnapshot(); + } +);