Skip to content

Instantly share code, notes, and snippets.

@kolodny
Forked from jamiebuilds/assertEqualJSX.js
Created January 18, 2016 02:49
Show Gist options
  • Save kolodny/f981c5489adb6cba41f7 to your computer and use it in GitHub Desktop.
Save kolodny/f981c5489adb6cba41f7 to your computer and use it in GitHub Desktop.
import {renderToStaticMarkup} from 'react-dom/server';
import {html as beautifyHTML} from 'js-beautify';
import {diffWords} from 'diff';
import chalk from 'chalk';
import {fail} from 'assert';
const colors = new chalk.constructor({
enabled: !!global.top.karma
});
export default function assertEqualJSX(actual, expected, opts = {}) {
actual = renderToStaticMarkup(actual);
expected = renderToStaticMarkup(expected);
if (opts.sanitize) {
actual = opts.sanitize(actual);
expected = opts.sanitize(expected);
}
if (actual === expected) {
return;
}
actual = beautifyHTML(actual);
expected = beautifyHTML(expected);
const diff = diffWords(actual, expected)
.map((part) => {
if (part.added) {
return colors.black.bgRed(part.value);
} else if (part.removed) {
return colors.black.bgGreen(part.value);
} else {
return colors.gray(part.value);
}
})
.join('');
const message = [
'',
'-------------------------------------------------------------------------',
'',
colors.gray('Diff:'),
diff,
'',
colors.green('Actual:'),
colors.green(actual),
'',
colors.red('Expected:'),
colors.red(expected),
'',
'-------------------------------------------------------------------------',
].join('\n');
fail(actual, expected, message, '==');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment