Skip to content

Commit a6b908f

Browse files
wellwindahnpnl
authored andcommitted
fix(serializers): generated id="root{n}" should be removed
BREAKING CHANGE This change will affect snapshot generation. One should update component snapshots via `-u` Jest CLI option. Since this change only removes the root `id`, it shouldn't affect the quality of snapshots in general.
1 parent c17be8e commit a6b908f

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

e2e/snapshot-serializers/__tests__/__snapshots__/snapshot-serializers.spec.ts.snap

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ exports[`FooComponent should allow generating snapshot 1`] = `
99
>
1010
<p>
1111
Line 1
12-
</p><div>
12+
</p><div
13+
id="foo"
14+
>
1315
<div>
1416
val1
1517
</div>
@@ -18,13 +20,13 @@ exports[`FooComponent should allow generating snapshot 1`] = `
1820
`;
1921

2022
exports[`FooComponent should allow generating snapshot 2`] = `
21-
<div
22-
id="root1"
23-
>
23+
<div>
2424
<p>
2525
Line 1
2626
</p>
27-
<div>
27+
<div
28+
id="foo"
29+
>
2830
<div>
2931
val1
3032
</div>
@@ -38,7 +40,9 @@ exports[`FooComponent should allow generating snapshot with removed component at
3840
<foo>
3941
<p>
4042
Line 1
41-
</p><div>
43+
</p><div
44+
id="foo"
45+
>
4246
<div>
4347
val1
4448
</div>
@@ -47,13 +51,13 @@ exports[`FooComponent should allow generating snapshot with removed component at
4751
`;
4852

4953
exports[`FooComponent should allow generating snapshot with removed component attributes with snapshot serializer option 2`] = `
50-
<div
51-
id="root0"
52-
>
54+
<div>
5355
<p>
5456
Line 1
5557
</p>
56-
<div>
58+
<div
59+
id="foo"
60+
>
5761
<div>
5862
val1
5963
</div>

e2e/snapshot-serializers/foo.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!-- SOMETHING -->
22
<p>Line 1</p>
3-
<div>
3+
<div id="foo">
4+
<!-- this id attribute should not be eliminate -->
45
<div *ngIf="condition1">
56
{{ value1() }}
67
</div>

src/serializers/no-ng-attributes.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ const hasAttributesToClean = (attribute: Attr): boolean =>
1919

2020
const removeAngularAttributes = (node: Element): Element => {
2121
const nodeCopy = node.cloneNode(true) as Element;
22+
// if parent of original node is body,
23+
// means the node is additionally generated when call TestBed.createComponent,
24+
// this node will have id="root{n}", will cause snapshot testing not stable,
25+
// so the id attribute should be removed
26+
if (node.parentElement?.tagName === 'BODY') {
27+
nodeCopy.removeAttribute('id');
28+
}
29+
30+
// Remove angular-specific attributes
2231
Object.values(nodeCopy.attributes)
2332
.filter(hasAttributesToRemove)
2433
.forEach((attribute) => nodeCopy.attributes.removeNamedItem(attribute.name));

0 commit comments

Comments
 (0)