Skip to content

Commit 134c78e

Browse files
Allow diffs to pretty print while comparisons don't
1 parent 99e23ba commit 134c78e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export const options = {};
99

1010
// options to pass to renderToString() when doing a deep comparison
1111
const RENDER_OPTS = {
12-
sortAttributes: true,
13-
pretty: false
12+
sortAttributes: true
1413
};
1514

1615
// options to pass to renderToString() when doing a shallow comparison
@@ -25,6 +24,12 @@ const SHALLOW_OPTS_EXPECTED = {
2524
renderRootComponent: false
2625
};
2726

27+
// for "includes" and "contains", pretty-print the diff but not the version that gets compared
28+
const INCLUDE_RENDER_OPTS = {
29+
...RENDER_OPTS,
30+
pretty: false
31+
};
32+
2833
// create an assertion template string for the given action
2934
let msg = act => `expected #{act} to ${act} #{exp}`;
3035

@@ -35,21 +40,23 @@ let isJsx = obj => obj && (options.isJsx ? options.isJsx(obj) : (obj.__isVNode |
3540
let isVNode = obj => obj.hasOwnProperty('nodeName') && obj.hasOwnProperty('attributes') && obj.hasOwnProperty('children') && obj.constructor.name==='VNode';
3641

3742
// inject a chai assertion if the values being tested are JSX VNodes
38-
let ifJsx = (fn, opts, optsExpected) => next => function(jsx, ...args) {
43+
let ifJsx = (fn, opts, optsExpected, displayOpts) => next => function(jsx, ...args) {
3944
if (!isJsx(this._obj)) return next.call(this, jsx, ...args);
4045
let actual = render(this._obj, null, opts).trim();
4146
let expected = render(jsx, null, optsExpected || opts).trim();
42-
return fn(this, { expected, actual, jsx });
47+
let diffActual = displayOpts ? render(this._obj, null, displayOpts).trim() : actual;
48+
let diffExpected = displayOpts ? render(jsx, null, displayOpts).trim() : expected;
49+
return fn(this, { expected, actual, diffActual, diffExpected, jsx });
4350
};
4451

4552
// create a passthrough function
4653
let through = next => function(...args) { return next.call(this, ...args); };
4754

4855
// assert that a String is equal to the given string
49-
let equal = (a, { expected, actual }) => a.assert(actual===expected, msg('equal'), msg('not equal'), expected, actual, true);
56+
let equal = (a, { expected, actual, diffExpected, diffActual }) => a.assert(actual===expected, msg('equal'), msg('not equal'), diffExpected, diffActual, true);
5057

5158
// assert that a String contains the given string
52-
let include = (a, { expected, actual }) => a.assert(~actual.indexOf(expected), msg('include'), msg('not include'), expected, actual, true);
59+
let include = (a, { expected, actual, diffExpected, diffActual }) => a.assert(~actual.indexOf(expected), msg('include'), msg('not include'), diffExpected, diffActual, true);
5360

5461

5562
/** Middleware: pass to `chai.use()` to add JSX assertion support. */
@@ -63,9 +70,8 @@ export default function assertJsx({ Assertion }) {
6370
Assertion.overwriteMethod('equal', ifJsx(equal, SHALLOW_OPTS, SHALLOW_OPTS_EXPECTED));
6471
Assertion.overwriteMethod('equals', ifJsx(equal, SHALLOW_OPTS, SHALLOW_OPTS_EXPECTED));
6572

66-
6773
['include', 'includes', 'contain', 'contains'].forEach( method => {
68-
Assertion.overwriteChainableMethod(method, ifJsx(include, RENDER_OPTS), through);
74+
Assertion.overwriteChainableMethod(method, ifJsx(include, INCLUDE_RENDER_OPTS, INCLUDE_RENDER_OPTS, RENDER_OPTS), through);
6975
});
7076
}
7177

0 commit comments

Comments
 (0)