Skip to content

Commit 8853dc0

Browse files
authored
fix(vitest-angular): normalize snapshot whitespace (#2237)
1 parent 1bca39d commit 8853dc0

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

packages/vitest-angular/src/lib/snapshot-serializers/angular-fixture.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ describe('ng-snapshot serializer', () => {
8484
`);
8585
});
8686

87+
it('normalizes trailing whitespace in serialized text nodes', () => {
88+
const fixture = createComponentFixture(
89+
'app-copy',
90+
'<p>For guides on how to customize this project, visit the <a href="https://analogjs.org">Analog documentation</a></p>',
91+
);
92+
93+
expect(fixture).toMatchInlineSnapshot(`
94+
<app-copy>
95+
<p>
96+
For guides on how to customize this project, visit the
97+
<a
98+
href="https://analogjs.org"
99+
>
100+
Analog documentation
101+
</a>
102+
</p>
103+
</app-copy>
104+
`);
105+
});
106+
87107
it('serializes an Angular component', () => {
88108
@Component({
89109
selector: 'app-chip',
@@ -107,4 +127,20 @@ describe('ng-snapshot serializer', () => {
107127
</app-chip>
108128
`);
109129
});
130+
131+
it('normalizes trailing spaces and repeated blank lines', () => {
132+
const fixture = createComponentFixture('app-test', '<span>Title</span>');
133+
const serializer = createAngularFixtureSnapshotSerializer();
134+
135+
const result = serializer.serialize?.(
136+
fixture,
137+
{} as any,
138+
'',
139+
0,
140+
[],
141+
() => `<app-test> \n\n\n <span>Title</span> \n</app-test>\t`,
142+
);
143+
144+
expect(result).toBe(`<app-test>\n\n <span>Title</span>\n</app-test>`);
145+
});
110146
});

packages/vitest-angular/src/lib/snapshot-serializers/angular-fixture.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,21 @@ function fixtureVitestSerializer(fixture: any) {
100100
return doc.body.childNodes[0];
101101
}
102102

103+
function normalizeSnapshotOutput(value: string): string {
104+
return (
105+
value
106+
// Inline snapshots should not fail after formatters strip trailing spaces.
107+
.replace(/[ \t]+$/gm, '')
108+
// Comment removal can leave repeated empty lines in pretty-printed output.
109+
.replace(/\n{3,}/g, '\n\n')
110+
);
111+
}
112+
103113
export function createAngularFixtureSnapshotSerializer(): SnapshotSerializer {
104114
return {
105115
serialize(val, config, indentation, depth, refs, printer) {
106-
return printer(
107-
fixtureVitestSerializer(val),
108-
config,
109-
indentation,
110-
depth,
111-
refs,
116+
return normalizeSnapshotOutput(
117+
printer(fixtureVitestSerializer(val), config, indentation, depth, refs),
112118
);
113119
},
114120
test(val) {

tests/vitest-angular/src/snapshot-serializers/__snapshots__/angular-fixture-snapshot.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ exports[`Angular Fixture Snapshot > create angular component componentRef fixtur
3232
<p
3333
class="read-the-docs"
3434
>
35-
For guides on how to customize this project, visit the
35+
For guides on how to customize this project, visit the
3636
<a
3737
href="https://analogjs.org"
3838
target="_blank"
@@ -75,7 +75,7 @@ exports[`Angular Fixture Snapshot > create angular fixture snapshot 1`] = `
7575
<p
7676
class="read-the-docs"
7777
>
78-
For guides on how to customize this project, visit the
78+
For guides on how to customize this project, visit the
7979
<a
8080
href="https://analogjs.org"
8181
target="_blank"

0 commit comments

Comments
 (0)