Skip to content

Commit

Permalink
add toRender matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Mar 12, 2019
1 parent 5f2f087 commit b9d99af
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion test/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
var marked = require('../../lib/marked.js');
const marked = require('../../');
const HtmlDiffer = require('@markedjs/html-differ').HtmlDiffer;
const htmlDiffer = new HtmlDiffer({ignoreSelfClosingSlash: true});

beforeEach(function () {
marked.setOptions(marked.getDefaults());

jasmine.addMatchers({
toRender: function () {
return {
compare: function (spec, expected) {
const result = {};
const actual = marked(spec.markdown, spec.options);
result.pass = htmlDiffer.isEqual(expected, actual);

if (result.pass) {
result.message = spec.markdown + '\n------\n\nExpected: Should Fail';
} else {
var expectedHtml = expected.replace(/\s/g, '');
var actualHtml = actual.replace(/\s/g, '');

for (var i = 0; i < expectedHtml.length; i++) {
if (actualHtml[i] !== expectedHtml[i]) {
actualHtml = actualHtml.substring(
Math.max(i - 30, 0),
Math.min(i + 30, actualHtml.length));

expectedHtml = expectedHtml.substring(
Math.max(i - 30, 0),
Math.min(i + 30, expectedHtml.length));

break;
}
}
result.message = 'Expected:\n' + expectedHtml + '\n\nActual:\n' + actualHtml;
}
return result;
}
};
}
});
});

0 comments on commit b9d99af

Please sign in to comment.