@@ -24,6 +24,12 @@ const SHALLOW_OPTS_EXPECTED = {
2424 renderRootComponent : false
2525} ;
2626
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+
2733// create an assertion template string for the given action
2834let msg = act => `expected #{act} to ${ act } #{exp}` ;
2935
@@ -34,21 +40,23 @@ let isJsx = obj => obj && (options.isJsx ? options.isJsx(obj) : (obj.__isVNode |
3440let isVNode = obj => obj . hasOwnProperty ( 'nodeName' ) && obj . hasOwnProperty ( 'attributes' ) && obj . hasOwnProperty ( 'children' ) && obj . constructor . name === 'VNode' ;
3541
3642// inject a chai assertion if the values being tested are JSX VNodes
37- let ifJsx = ( fn , opts , optsExpected ) => next => function ( jsx , ...args ) {
43+ let ifJsx = ( fn , opts , optsExpected , displayOpts ) => next => function ( jsx , ...args ) {
3844 if ( ! isJsx ( this . _obj ) ) return next . call ( this , jsx , ...args ) ;
3945 let actual = render ( this . _obj , null , opts ) . trim ( ) ;
4046 let expected = render ( jsx , null , optsExpected || opts ) . trim ( ) ;
41- 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 } ) ;
4250} ;
4351
4452// create a passthrough function
4553let through = next => function ( ...args ) { return next . call ( this , ...args ) ; } ;
4654
4755// assert that a String is equal to the given string
48- 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 ) ;
4957
5058// assert that a String contains the given string
51- 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 ) ;
5260
5361
5462/** Middleware: pass to `chai.use()` to add JSX assertion support. */
@@ -62,9 +70,8 @@ export default function assertJsx({ Assertion }) {
6270 Assertion . overwriteMethod ( 'equal' , ifJsx ( equal , SHALLOW_OPTS , SHALLOW_OPTS_EXPECTED ) ) ;
6371 Assertion . overwriteMethod ( 'equals' , ifJsx ( equal , SHALLOW_OPTS , SHALLOW_OPTS_EXPECTED ) ) ;
6472
65-
6673 [ 'include' , 'includes' , 'contain' , 'contains' ] . forEach ( method => {
67- Assertion . overwriteChainableMethod ( method , ifJsx ( include ) , through ) ;
74+ Assertion . overwriteChainableMethod ( method , ifJsx ( include , INCLUDE_RENDER_OPTS , INCLUDE_RENDER_OPTS , RENDER_OPTS ) , through ) ;
6875 } ) ;
6976}
7077
0 commit comments