Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt committed Oct 23, 2024
1 parent cebe2b6 commit 9e1f4cc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.git
/.github
/tests
.editorconfig
.gitignore
eslint.config.js
vitest.config.js
23 changes: 23 additions & 0 deletions src/formatMarkup.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ export const diffableFormatter = function (markup) {
}
return '';
}
if (node.nodeName === '#comment') {
let data = node.data
.split('\n')
.map((line, index, lines) => {
if (!line) {
return line;
}
if (index + 1 === lines.length) {
return line.trim();
}
return ' '.repeat(indent + 1) + line.trimStart();
})
.join('\n');
if (!data.startsWith('\n')) {
data = ' ' + data;
}
if (!data.endsWith('\n')) {
data = data + ' ';
} else {
data = data + ' '.repeat(indent);
}
return '\n' + ' '.repeat(indent) + '<!--' + data + '-->';
}

let result = '\n' + ' '.repeat(indent) + '<' + node.nodeName;

Expand Down
43 changes: 43 additions & 0 deletions tests/unit/src/formatMarkup.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mount } from '@vue/test-utils';

import { formatMarkup } from '@/formatMarkup.js';

const unformattedMarkup = `
Expand Down Expand Up @@ -83,4 +85,45 @@ describe('Format markup', () => {
expect(console.info)
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: Your custom markup formatter must return a string.');
});

describe('Comments', () => {
test('Does not change comments', () => {
const MyComponent = {
template: `
<div>
<!-- Single Line -->
<p>1</p>
<!--
Multi
Line
-->
<p>2</p>
<p v-if="false">3</p>
</div>
`
};
const wrapper = mount(MyComponent);

globalThis.vueSnapshots.formatter = 'diffable';
globalThis.vueSnapshots.removeComments = false;

expect(wrapper.html())
.toMatchInlineSnapshot(`
<div>
<!-- Single Line -->
<p>
1
</p>
<!--
Multi
Line
-->
<p>
2
</p>
<!-- v-if -->
</div>
`);
});
});
});

0 comments on commit 9e1f4cc

Please sign in to comment.