feat: ignore Angular IDs similar to React functionality (#80)

This commit is contained in:
Paul Spears 2021-02-15 20:33:27 -06:00 committed by GitHub
parent 9e94d1127f
commit 68d2f9befa
2 changed files with 17 additions and 10 deletions

View file

@ -10,7 +10,7 @@ const path = require('path')
const {
serializeDomElement,
serializeReactToHTML,
serializeToHTML,
identity,
countSnapshots
} = require('./utils')
@ -172,7 +172,7 @@ function registerCypressSnapshot () {
const pickSerializer = (asJson, value) => {
if (Cypress.dom.isJquery(value)) {
return asJson ? serializeDomElement : serializeReactToHTML
return asJson ? serializeDomElement : serializeToHTML
}
return identity
}

View file

@ -12,29 +12,36 @@ function serializeDomElement ($el) {
json.attributes.value = $el.context.value
}
return deleteReactIdFromJson(json)
return deleteTransientIdsFromJson(json)
}
// remove React id, too transient
function deleteReactIdFromJson (json) {
// remove React and Angular ids, which are transient
function deleteTransientIdsFromJson(json) {
if (json.attributes) {
delete json.attributes['data-reactid']
Object.keys(json.attributes)
.filter(key => key.startsWith('_ng'))
.forEach(attr => delete json.attributes[attr])
delete json.attributes['']
}
if (Array.isArray(json.childNodes)) {
json.childNodes.forEach(deleteReactIdFromJson)
json.childNodes.forEach(deleteTransientIdsFromJson)
}
return json
}
const stripReactIdAttributes = (html) => {
const stripTransientIdAttributes = (html) => {
const dataReactId = /data\-reactid="[\.\d\$\-abcdfef]+"/g
const angularId = /_ng(content|host)\-[0-9a-z-]+(="")?/g
return html.replace(dataReactId, '')
.replace(angularId, '')
}
const serializeReactToHTML = (el$) => {
const serializeToHTML = (el$) => {
const html = el$[0].outerHTML
const stripped = stripReactIdAttributes(html)
const stripped = stripTransientIdAttributes(html)
const options = {
wrap_line_length: 80,
indent_inner_html: true,
@ -55,7 +62,7 @@ const countSnapshots = (snapshots) =>
module.exports = {
SNAPSHOT_FILE_NAME: 'snapshots.js',
serializeDomElement,
serializeReactToHTML,
serializeToHTML,
identity,
countSnapshots
}