Skip to content

Commit

Permalink
Post processing (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJaredWilcurt authored Dec 16, 2024
1 parent 7260c4f commit 6ec2526
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 86 deletions.
1 change: 1 addition & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ Related to issue #0

* [ ] Update dependencies
* [ ] Bump
* [ ] If API changed, update `types.js`
31 changes: 16 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "vue3-snapshot-serializer",
"type": "module",
"version": "1.0.1",
"version": "2.0.0",
"description": "Vitest snapshot serializer for Vue 3 components",
"main": "index.js",
"scripts": {
"lint": "eslint *.js src tests",
"fix": "npm run lint -- --fix",
"test": "vitest --coverage",
"unit": "vitest --run",
"debug": "vitest --inspect-brk --no-file-parallelism -t \"Stringify attributes\" \"cheerio\""
},
"dependencies": {
Expand All @@ -16,12 +17,12 @@
"parse5": "^7.2.1"
},
"devDependencies": {
"@eslint/js": "^9.16.0",
"@eslint/js": "^9.17.0",
"@stylistic/eslint-plugin-js": "^2.12.1",
"@vitejs/plugin-vue": "^5.2.1",
"@vitest/coverage-v8": "^2.1.8",
"@vue/test-utils": "^2.4.6",
"eslint": "^9.16.0",
"eslint": "^9.17.0",
"eslint-config-tjw-base": "^3.1.0",
"eslint-config-tjw-jsdoc": "^2.0.1",
"eslint-plugin-jsdoc": "^50.6.1",
Expand Down
18 changes: 9 additions & 9 deletions src/formatMarkup.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,16 @@ export const diffableFormatter = function (markup) {
* @return {string} The same string, formatted based on user settings.
*/
export const formatMarkup = function (markup) {
if (globalThis.vueSnapshots?.formatter) {
if (typeof(globalThis.vueSnapshots.formatter) === 'function') {
const result = globalThis.vueSnapshots.formatter(markup);
if (typeof(result) === 'string') {
return result;
} else {
logger('Your custom markup formatter must return a string.');
if (globalThis.vueSnapshots) {
if (globalThis.vueSnapshots.formatter === 'diffable') {
markup = diffableFormatter(markup);
}
if (typeof(globalThis.vueSnapshots.postProcessor) === 'function') {
markup = globalThis.vueSnapshots.postProcessor(markup);
if (typeof(markup) !== 'string') {
logger('Your custom markup post processor must return a string.');
return '';
}
} else if (globalThis.vueSnapshots.formatter === 'diffable') {
return diffableFormatter(markup, globalThis.vueSnapshots.formatting);
}
}
return markup;
Expand Down
19 changes: 12 additions & 7 deletions src/loadOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,9 @@ export const loadOptions = function () {
globalThis.vueSnapshots.attributesToClear = attributesToClear;

// Formatter
if (
typeof(globalThis.vueSnapshots.formatter) !== 'function' &&
!['none', 'diffable'].includes(globalThis.vueSnapshots.formatter)
) {
if (!['none', 'diffable'].includes(globalThis.vueSnapshots.formatter)) {
if (globalThis.vueSnapshots.formatter) {
logger('Allowed values for global.vueSnapshots.formatter are \'none\', \'diffable\', or a custom function');
logger('Allowed values for global.vueSnapshots.formatter are \'none\' and \'diffable\'.');
}
globalThis.vueSnapshots.formatter = undefined;
}
Expand All @@ -103,7 +100,7 @@ export const loadOptions = function () {
typeof(globalThis.vueSnapshots.formatting) === 'object' &&
Object.keys(globalThis.vueSnapshots.formatting).length
) {
logger('When setting the formatter to "none" or a custom function, all formatting options will be removed.');
logger('When setting the formatter to anything other than \'diffable\', all formatting options are ignored.');
}

// Formatting
Expand Down Expand Up @@ -177,6 +174,13 @@ export const loadOptions = function () {
delete globalThis.vueSnapshots.formatting;
}

if (typeof(globalThis.vueSnapshots.postProcessor) !== 'function') {
if (globalThis.vueSnapshots.postProcessor) {
logger('The postProcessor option must be a function that returns a string, or undefined.');
}
delete globalThis.vueSnapshots.postProcessor;
}

/**
* Clean up settings
*/
Expand All @@ -185,7 +189,8 @@ export const loadOptions = function () {
...Object.keys(booleanDefaults),
'attributesToClear',
'formatter',
'formatting'
'formatting',
'postProcessor'
];
const permittedFormattingKeys = [
...Object.keys(formattingBooleanDefaults),
Expand Down
36 changes: 24 additions & 12 deletions tests/unit/src/formatMarkup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,44 @@ describe('Format markup', () => {
});

test('Applies custom formatting', () => {
globalThis.vueSnapshots.formatter = function () {
return 'test';
globalThis.vueSnapshots.postProcessor = function (input) {
return input.toUpperCase();
};

expect(formatMarkup(unformattedMarkup))
.toEqual('test');
expect(unformattedMarkup)
.toMatchInlineSnapshot(`
<DIV ID="HEADER">
<H1>
HELLO WORLD!
</H1>
<UL
CLASS="LIST"
ID="MAIN-LIST"
>
<LI>
<A
CLASS="LINK"
HREF="#"
>MY HTML</A>
</LI>
</UL>
</DIV>
`);

expect(console.info)
.not.toHaveBeenCalled();
});

test('Logs warning if custom function does not return a string', () => {
globalThis.vueSnapshots.formatter = function () {
globalThis.vueSnapshots.postProcessor = function () {
return 5;
};

expect(unformattedMarkup)
.toMatchInlineSnapshot(`
<div id="header">
<h1>Hello World!</h1>
<ul class="list" id="main-list"><li><a class="link" href="#">My HTML</a></li></ul>
</div>
`);
.toMatchInlineSnapshot('');

expect(console.info)
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: Your custom markup formatter must return a string.');
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: Your custom markup post processor must return a string.');
});

describe('diffableFormatter', () => {
Expand Down
44 changes: 30 additions & 14 deletions tests/unit/src/loadOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('Load options', () => {
.toEqual('diffable');

expect(console.info)
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: Allowed values for global.vueSnapshots.formatter are \'none\', \'diffable\', or a custom function');
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: Allowed values for global.vueSnapshots.formatter are \'none\' and \'diffable\'.');
});

test('None', () => {
Expand Down Expand Up @@ -169,32 +169,48 @@ describe('Load options', () => {
.not.toHaveBeenCalled();
});

test('Custom function', () => {
function formatter (markup) {
return markup.toUpperCase();
}
global.vueSnapshots = { formatter };
test('Warns and deletes formatting options if not using diffable formatter', () => {
global.vueSnapshots.formatter = 'none';
global.vueSnapshots.formatting = { emptyAttributes: true };

loadOptions();

expect(globalThis.vueSnapshots.formatter)
.toEqual(formatter);
expect(globalThis.vueSnapshots.formatting)
.toEqual(undefined);

expect(console.info)
.not.toHaveBeenCalled();
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: When setting the formatter to anything other than \'diffable\', all formatting options are ignored.');
});
});

test('Warns and deletes formatting options if not using diffable formatter', () => {
global.vueSnapshots.formatter = 'none';
global.vueSnapshots.formatting = { emptyAttributes: true };
describe('Post processing markup', () => {
test('Warns if postProcessor is not a function', () => {
global.vueSnapshots = {
postProcessor: 'invalid'
};

loadOptions();

expect(globalThis.vueSnapshots.formatting)
expect(globalThis.vueSnapshots.postProcessor)
.toEqual(undefined);

expect(console.info)
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: When setting the formatter to "none" or a custom function, all formatting options will be removed.');
.toHaveBeenCalledWith('Vue 3 Snapshot Serializer: The postProcessor option must be a function that returns a string, or undefined.');
});

test('Applies custom formatting after Diffable', () => {
function postProcessor (markup) {
return markup.toUpperCase();
}
global.vueSnapshots = { postProcessor };

loadOptions();

expect(globalThis.vueSnapshots.postProcessor)
.toEqual(postProcessor);

expect(console.info)
.not.toHaveBeenCalled();
});
});

Expand Down
Loading

0 comments on commit 6ec2526

Please sign in to comment.