@@ -1537,7 +1537,91 @@ describe('toMatchObject()', () => {
15371537 }
15381538 }
15391539
1540- [
1540+ const testNotToMatchSnapshots = tuples => {
1541+ tuples . forEach ( ( [ n1 , n2 ] ) => {
1542+ it ( `{pass: true} expect(${ stringify ( n1 ) } ).toMatchObject(${ stringify (
1543+ n2 ,
1544+ ) } )`, ( ) => {
1545+ jestExpect ( n1 ) . toMatchObject ( n2 ) ;
1546+ expect ( ( ) =>
1547+ jestExpect ( n1 ) . not . toMatchObject ( n2 ) ,
1548+ ) . toThrowErrorMatchingSnapshot ( ) ;
1549+ } ) ;
1550+ } ) ;
1551+ } ;
1552+
1553+ const testToMatchSnapshots = tuples => {
1554+ tuples . forEach ( ( [ n1 , n2 ] ) => {
1555+ it ( `{pass: false} expect(${ stringify ( n1 ) } ).toMatchObject(${ stringify (
1556+ n2 ,
1557+ ) } )`, ( ) => {
1558+ jestExpect ( n1 ) . not . toMatchObject ( n2 ) ;
1559+ expect ( ( ) =>
1560+ jestExpect ( n1 ) . toMatchObject ( n2 ) ,
1561+ ) . toThrowErrorMatchingSnapshot ( ) ;
1562+ } ) ;
1563+ } ) ;
1564+ } ;
1565+
1566+ describe ( 'circular references' , ( ) => {
1567+ describe ( 'simple circular references' , ( ) => {
1568+ const circularObj = { a : 'hello' } ;
1569+ circularObj . ref = circularObj ;
1570+
1571+ const differentCircularObj = { a : 'world' } ;
1572+ differentCircularObj . ref = differentCircularObj ;
1573+
1574+ const otherCircularObj = { a : 'hello' } ;
1575+ otherCircularObj . ref = otherCircularObj ;
1576+
1577+ const primitiveInsteadOfRef = { } ;
1578+ primitiveInsteadOfRef . ref = 'not a ref' ;
1579+
1580+ testNotToMatchSnapshots ( [
1581+ [ circularObj , { } ] ,
1582+ [ otherCircularObj , circularObj ] ,
1583+ ] ) ;
1584+
1585+ testToMatchSnapshots ( [
1586+ [ { } , circularObj ] ,
1587+ [ circularObj , differentCircularObj ] ,
1588+ [ primitiveInsteadOfRef , circularObj ] ,
1589+ ] ) ;
1590+ } ) ;
1591+
1592+ describe ( 'transitive circular references' , ( ) => {
1593+ const transitiveCircularObj = { a : 'hello' } ;
1594+ transitiveCircularObj . nestedObj = { parentObj : transitiveCircularObj } ;
1595+
1596+ const otherTransitiveCircularObj = { a : 'hello' } ;
1597+ otherTransitiveCircularObj . nestedObj = {
1598+ parentObj : otherTransitiveCircularObj ,
1599+ } ;
1600+
1601+ const differentTransitiveCircularObj = { a : 'world' } ;
1602+ differentTransitiveCircularObj . nestedObj = {
1603+ parentObj : differentTransitiveCircularObj ,
1604+ } ;
1605+
1606+ const primitiveInsteadOfRef = { } ;
1607+ primitiveInsteadOfRef . nestedObj = {
1608+ parentObj : 'not the parent ref' ,
1609+ } ;
1610+
1611+ testNotToMatchSnapshots ( [
1612+ [ transitiveCircularObj , { } ] ,
1613+ [ otherTransitiveCircularObj , transitiveCircularObj ] ,
1614+ ] ) ;
1615+
1616+ testToMatchSnapshots ( [
1617+ [ { } , transitiveCircularObj ] ,
1618+ [ differentTransitiveCircularObj , transitiveCircularObj ] ,
1619+ [ primitiveInsteadOfRef , transitiveCircularObj ] ,
1620+ ] ) ;
1621+ } ) ;
1622+ } ) ;
1623+
1624+ testNotToMatchSnapshots ( [
15411625 [ { a : 'b' , c : 'd' } , { a : 'b' } ] ,
15421626 [ { a : 'b' , c : 'd' } , { a : 'b' , c : 'd' } ] ,
15431627 [ { a : 'b' , t : { x : { r : 'r' } , z : 'z' } } , { a : 'b' , t : { z : 'z' } } ] ,
@@ -1560,18 +1644,9 @@ describe('toMatchObject()', () => {
15601644 [ new Error ( 'bar' ) , { message : 'bar' } ] ,
15611645 [ new Foo ( ) , { a : undefined , b : 'b' } ] ,
15621646 [ Object . assign ( Object . create ( null ) , { a : 'b' } ) , { a : 'b' } ] ,
1563- ] . forEach ( ( [ n1 , n2 ] ) => {
1564- it ( `{pass: true} expect(${ stringify ( n1 ) } ).toMatchObject(${ stringify (
1565- n2 ,
1566- ) } )`, ( ) => {
1567- jestExpect ( n1 ) . toMatchObject ( n2 ) ;
1568- expect ( ( ) =>
1569- jestExpect ( n1 ) . not . toMatchObject ( n2 ) ,
1570- ) . toThrowErrorMatchingSnapshot ( ) ;
1571- } ) ;
1572- } ) ;
1647+ ] ) ;
15731648
1574- [
1649+ testToMatchSnapshots ( [
15751650 [ { a : 'b' , c : 'd' } , { e : 'b' } ] ,
15761651 [ { a : 'b' , c : 'd' } , { a : 'b!' , c : 'd' } ] ,
15771652 [ { a : 'a' , c : 'd' } , { a : jestExpect . any ( Number ) } ] ,
@@ -1597,16 +1672,7 @@ describe('toMatchObject()', () => {
15971672 [ [ 1 , 2 , 3 ] , [ 1 , 2 , 2 ] ] ,
15981673 [ new Error ( 'foo' ) , new Error ( 'bar' ) ] ,
15991674 [ Object . assign ( Object . create ( null ) , { a : 'b' } ) , { c : 'd' } ] ,
1600- ] . forEach ( ( [ n1 , n2 ] ) => {
1601- it ( `{pass: false} expect(${ stringify ( n1 ) } ).toMatchObject(${ stringify (
1602- n2 ,
1603- ) } )`, ( ) => {
1604- jestExpect ( n1 ) . not . toMatchObject ( n2 ) ;
1605- expect ( ( ) =>
1606- jestExpect ( n1 ) . toMatchObject ( n2 ) ,
1607- ) . toThrowErrorMatchingSnapshot ( ) ;
1608- } ) ;
1609- } ) ;
1675+ ] ) ;
16101676
16111677 [
16121678 [ null , { } ] ,
0 commit comments