Skip to content

Commit

Permalink
fix(jsdoc-test-matchers): normalize whitespace in toContainHtml mat…
Browse files Browse the repository at this point in the history
…cher

Previously, differences in indentation and empty lines could incorrectly prevent a match.
  • Loading branch information
hegemonic committed Jan 7, 2023
1 parent f073a20 commit 7024ee5
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/jsdoc-test-matchers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ const _ = require('lodash');
const { addMatchers } = require('add-matchers');
const { format } = require('prettier');

function formatHtml(str) {
return format(str, {
function stripWhitespace(str) {
// Remove leading whitespace.
str = str.replace(/^[\s]+/gm, '');
// Remove empty lines.
str = str.replace(/^\n$/gm, '');

return str;
}

function normalizeHtml(str) {
str = format(str, {
parser: 'html',
tabWidth: 2,
});

return stripWhitespace(str);
}

// Prettier lazy-loads its parsers, so preload the HTML parser while we know we're not mocked.
Expand Down Expand Up @@ -44,8 +55,8 @@ addMatchers({
return valueName === otherName;
},
toContainHtml(other, value) {
const otherDiffable = formatHtml(other);
const valueDiffable = formatHtml(value);
const otherDiffable = normalizeHtml(other);
const valueDiffable = normalizeHtml(value);

return valueDiffable.includes(otherDiffable);
},
Expand Down

0 comments on commit 7024ee5

Please sign in to comment.