Skip to content

Commit ab02897

Browse files
Add regexToRemoveAttributes feature
1 parent 97e60bf commit ab02897

File tree

7 files changed

+149
-62
lines changed

7 files changed

+149
-62
lines changed

package-lock.json

Lines changed: 41 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vue3-snapshot-serializer",
33
"type": "module",
4-
"version": "2.10.0",
4+
"version": "2.11.0",
55
"description": "Vitest snapshot serializer for Vue 3 components",
66
"main": "index.js",
77
"scripts": {
@@ -19,20 +19,20 @@
1919
"js-beautify": "^1.15.3"
2020
},
2121
"devDependencies": {
22-
"@eslint/js": "^9.27.0",
22+
"@eslint/js": "^9.28.0",
2323
"@stylistic/eslint-plugin": "^4.4.0",
2424
"@testing-library/user-event": "^14.6.1",
2525
"@testing-library/vue": "^8.1.0",
2626
"@vitejs/plugin-vue": "^5.2.4",
2727
"@vitest/coverage-v8": "^3.1.4",
2828
"@vue/test-utils": "^2.4.6",
29-
"eslint": "^9.27.0",
29+
"eslint": "^9.28.0",
3030
"eslint-config-tjw-base": "^4.2.0",
3131
"eslint-config-tjw-jest": "^3.0.0",
3232
"eslint-config-tjw-jsdoc": "^2.0.1",
33-
"eslint-plugin-jest": "^28.11.0",
34-
"eslint-plugin-jsdoc": "^50.6.17",
35-
"happy-dom": "^17.4.7",
33+
"eslint-plugin-jest": "^28.12.0",
34+
"eslint-plugin-jsdoc": "^50.7.0",
35+
"happy-dom": "^17.5.6",
3636
"vitest": "^3.1.4"
3737
},
3838
"repository": {

src/cheerioManipulation.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,25 @@ const stringifyAttributes = function ($, vueWrapper) {
263263
}
264264
};
265265

266+
/**
267+
* Tests every attribute on every element to see if it matches the provided regex.
268+
* If it is a match, remove the attribute from the snapshot.
269+
*
270+
* @param {object} $ The markup as a cheerio object
271+
*/
272+
const removeAttributesViaRegex = function ($) {
273+
if (globalThis.vueSnapshots?.regexToRemoveAttributes) {
274+
debugLogger({ function: 'cheerioManipulation.js:removeAttributesViaRegex' });
275+
$('*').each(function (index, element) {
276+
Object.keys(element.attribs).forEach(function (attributeName) {
277+
if (globalThis.vueSnapshots?.regexToRemoveAttributes.test(attributeName)) {
278+
$(element).removeAttr(attributeName);
279+
}
280+
});
281+
});
282+
}
283+
};
284+
266285
/**
267286
* This removes data-v-1234abcd="" from your snapshots.
268287
*
@@ -504,6 +523,7 @@ export const cheerioManipulation = function (vueWrapper) {
504523
stringifyAttributes($, vueWrapper);
505524
// Uses CSS Selectors, so must run before test tokens are removed
506525
stubOutDom($);
526+
removeAttributesViaRegex($);
507527
removeServerRenderedText($);
508528
removeTestTokens($);
509529
removeScopedStylesDataVIDAttributes($);

src/loadOptions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ export const loadOptions = function () {
232232
}
233233
globalThis.vueSnapshots.stubs = stubsToProcess;
234234

235+
const regexToRemoveAttributesValid = (
236+
globalThis.vueSnapshots.regexToRemoveAttributes === undefined ||
237+
globalThis.vueSnapshots.regexToRemoveAttributes instanceof RegExp
238+
);
239+
if (!regexToRemoveAttributesValid) {
240+
logger('The global.vueSnapshots.regexToRemoveAttributes setting must be an instanceof RegExp or undefined. Received: ' + globalThis.vueSnapshots.regexToRemoveAttributes);
241+
delete globalThis.vueSnapshots.regexToRemoveAttributes;
242+
}
243+
235244
// Formatter
236245
if (!ALLOWED_FORMATTERS.includes(globalThis.vueSnapshots.formatter)) {
237246
if (globalThis.vueSnapshots.formatter) {
@@ -391,6 +400,7 @@ export const loadOptions = function () {
391400
'formatter',
392401
'formatting',
393402
'postProcessor',
403+
'regexToRemoveAttributes',
394404
'stubs'
395405
];
396406
const permittedFormattingKeys = [

tests/unit/src/cheerioManipulation.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,54 @@ describe('Cheerio Manipulation', () => {
8686
});
8787
});
8888

89+
describe('Remove attributes via regex', () => {
90+
const markup = `
91+
<div class data-p data-pc-name data-pc-section data-scrollselectors pc51>
92+
<div class data-p data-pc-section style>
93+
<table class data-pc-section role="table">
94+
<thead class data-p-scrollable="false" data-pc-section role="rowgroup" style>
95+
<tr data-pc-section role="row">
96+
<th class data-p-filter-column="false" data-p-reorderable-column="false" data-p-resizable-column="false" data-pc-name data-pc-section first="0" pc57 role="columnheader">
97+
<div class data-pc-section>
98+
<span class data-pc-section>
99+
Component DC
100+
</span>
101+
</div>
102+
</th>
103+
</tr>
104+
</thead>
105+
</table>
106+
</div>
107+
</div>
108+
`.trim();
109+
const cleaned = `
110+
<div class data-scrollselectors pc51>
111+
<div class style>
112+
<table class role="table">
113+
<thead class role="rowgroup" style>
114+
<tr role="row">
115+
<th class first="0" pc57 role="columnheader">
116+
<div class>
117+
<span class>
118+
Component DC
119+
</span>
120+
</div>
121+
</th>
122+
</tr>
123+
</thead>
124+
</table>
125+
</div>
126+
</div>
127+
`.trim();
128+
129+
test('Removes data-p attributes', () => {
130+
globalThis.vueSnapshots.regexToRemoveAttributes = new RegExp(/data-p.*/);
131+
132+
expect(cheerioManipulation(markup))
133+
.toEqual(cleaned);
134+
});
135+
});
136+
89137
describe('data-v-ids', () => {
90138
const markup = '<div data-v-34cd6f4f class="hello"> Hello World </div>';
91139
const cleaned = '<div class="hello"> Hello World </div>';

0 commit comments

Comments
 (0)