Open bahmutov opened 4 years ago
Here is a simplier re-production of the issue
it.only('Html String Snapshot', () => {
let innerHTML = `<body>\n <span>hello</span>\n</body>`;
cy.wrap(innerHTML).toMatchSnapshot();
});
As a workaround I am calling .split('\n') and comparing as an array:
it.only('Html String Snapshot', () => {
let innerHTML = `<body>\n <span>hello</span>\n</body>`.split('\n');
cy.wrap(innerHTML).toMatchSnapshot();
});
++ to this, I couldn't figure out how to make string snapshots work, and found ways to check what I was looking for as objects as instead 🤔
Yeah cheers for your comments everyone, this was really helpful for me, my use case was that I have styled components and charting libraries so both the classes and id are randomly generated, but managed to created a cypress custom command to handle this:
const styledComponentClass = /class="sc-.*?\"/g
const endOfElement = /<\/.*?\>/g
const idAttribute = /id=".*?\"/g
Cypress.Commands.add('matchDomSnapshot', (selector) => {
cy.get(selector).then(el => {
cy.wrap(el[0].innerHTML
.replace(styledComponentClass, '')
.replace(idAttribute, '')
.split(endOfElement)
.filter(string => string !== '')
).toMatchSnapshot()
})
})
Using this plugin to snapshot strings, like HTML almost works
saves the snapshot file
but the comparison on the next run fails
Notice string vs
"string"
I logged the task values
So close!
It would be nice to use for component's HTML snapshots in https://github.com/bahmutov/cypress-react-unit-test/pull/261