diff --git a/e2e/snapshot-serializers/__tests__/__snapshots__/snapshot-serializers.spec.ts.snap b/e2e/snapshot-serializers/__tests__/__snapshots__/snapshot-serializers.spec.ts.snap index 80114aefa4..7789983806 100644 --- a/e2e/snapshot-serializers/__tests__/__snapshots__/snapshot-serializers.spec.ts.snap +++ b/e2e/snapshot-serializers/__tests__/__snapshots__/snapshot-serializers.spec.ts.snap @@ -9,7 +9,9 @@ exports[`FooComponent should allow generating snapshot 1`] = ` >

Line 1 -

+

val1
@@ -18,13 +20,13 @@ exports[`FooComponent should allow generating snapshot 1`] = ` `; exports[`FooComponent should allow generating snapshot 2`] = ` -
+

Line 1

-
+
val1
@@ -38,7 +40,9 @@ exports[`FooComponent should allow generating snapshot with removed component at

Line 1 -

+

val1
@@ -47,13 +51,13 @@ exports[`FooComponent should allow generating snapshot with removed component at `; exports[`FooComponent should allow generating snapshot with removed component attributes with snapshot serializer option 2`] = ` -
+

Line 1

-
+
val1
diff --git a/e2e/snapshot-serializers/foo.component.html b/e2e/snapshot-serializers/foo.component.html index cdd69d0efa..cfb25a5e4b 100644 --- a/e2e/snapshot-serializers/foo.component.html +++ b/e2e/snapshot-serializers/foo.component.html @@ -1,6 +1,7 @@

Line 1

-
+
+
{{ value1() }}
diff --git a/src/serializers/no-ng-attributes.ts b/src/serializers/no-ng-attributes.ts index 0fdc4169a9..bd1aed9d86 100644 --- a/src/serializers/no-ng-attributes.ts +++ b/src/serializers/no-ng-attributes.ts @@ -19,6 +19,15 @@ const hasAttributesToClean = (attribute: Attr): boolean => const removeAngularAttributes = (node: Element): Element => { const nodeCopy = node.cloneNode(true) as Element; + // if parent of original node is body, + // means the node is additionally generated when call TestBed.createComponent, + // this node will have id="root{n}", will cause snapshot testing not stable, + // so the id attribute should be removed + if (node.parentElement?.tagName === 'BODY') { + nodeCopy.removeAttribute('id'); + } + + // Remove angular-specific attributes Object.values(nodeCopy.attributes) .filter(hasAttributesToRemove) .forEach((attribute) => nodeCopy.attributes.removeNamedItem(attribute.name));