diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..e6b88a9 --- /dev/null +++ b/.npmignore @@ -0,0 +1,7 @@ +/.git +/.github +/tests +.editorconfig +.gitignore +eslint.config.js +vitest.config.js diff --git a/src/formatMarkup.js b/src/formatMarkup.js index 074fac3..8d0c872 100644 --- a/src/formatMarkup.js +++ b/src/formatMarkup.js @@ -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) + ''; + } let result = '\n' + ' '.repeat(indent) + '<' + node.nodeName; diff --git a/tests/unit/src/formatMarkup.test.js b/tests/unit/src/formatMarkup.test.js index 8f589c3..443e8ce 100644 --- a/tests/unit/src/formatMarkup.test.js +++ b/tests/unit/src/formatMarkup.test.js @@ -1,3 +1,5 @@ +import { mount } from '@vue/test-utils'; + import { formatMarkup } from '@/formatMarkup.js'; const unformattedMarkup = ` @@ -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: ` +
1
+ +2
+3
++ 1 +
+ ++ 2 +
+ +