0

I am trying to write a test in JavaScript using Mocha and Chai. At the end of the test, I want to compare 2 JavaScript objects, these should be equal.

The output I get from the test is the following:

{ publication: 
   { publication_id: 'pubFetch1',
     title: 'Publication Fetcher',
     person_id: 'uploader',
     URL: 'http://www.pubfetchertest.com',
     publication_year: '2015',
     upload_date: '2015-05-05 00:00:00',
     page_count: '5',
     type: 'paper',
     rating: '0',
     votes: '0',
     abstract: 'Testing the Publication Fetcher',
     location: 'location' },
  comments: 
     [ { commentID: 'comment1',
         personID: 'uploader',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 10:24:36',
         reactions: [],
         myComment: true },
       { commentID: 'comment2',
         personID: 'author1',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 11:01:45',
         reactions: [Object],
         myComment: false } ],
   keywords: [ 'keyword1', 'keyword2', 'keyword3' ],
   uploader: { person_id: 'uploader', first_name: 'First', last_name: 'Last' },
   score: 5,
   authors: 
     [ { person_id: 'author1', first_name: 'First', last_name: 'Last' },
       { person_id: 'author2', first_name: 'First', last_name: 'Last' },
       { person_id: 'author3', first_name: 'First', last_name: 'Last' } ],
   editors: [],
   publishers: [] }

While the object I want to compare it with, is the following:

{ publication: 
      { publication_id: 'pubFetch1',
        title: 'Publication Fetcher',
        person_id: 'uploader',
        url: 'http://www.pubfetchertest.com',
        publication_year: '2015',
        upload_date: '2015-05-05 00:00:00',
        page_count: '5',
        type: 'paper',
        rating: '0',
        votes: '0',
        abstract: 'Testing the Publication Fetcher',
        location: 'location'},
   comments: 
      [{ commentID: 'comment1',
         personID: 'uploader',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 10:24:36',               
         reactions: [],
         myComment: true},
       { commentID: 'comment2',
         personID: 'author1',
         firstName: 'First',
         lastName: 'Last',
         text: 'Comment Content',
         time: '2015-05-05 11:01:45',
         reactions: [{ commentID: 'comment3',
                       personID: 'author2',
                       firstName: 'First',
                       lastName: 'Last',
                       text: 'Comment Content',
                       time: '2015-05-05 11:02:10',
                       reactions: [],
                       replyID: 'comment2',
                       myComment: false}],
         myComment: false}],
    keywords: ['keyword1', 'keyword2', 'keyword3'],
    uploader: {person_id: 'uploader',
               first_name: 'First',
               last_name: 'Last'},
    score: 5,
    authors: [{person_id: 'author1',
               first_name: 'First',
               last_name: 'Last'},
              {person_id: 'author2',
               first_name: 'First',
               last_name: 'Last'},
              {person_id: 'author3',
               first_name: 'First',
               last_name: 'Last'}],
    editors: [],
    publishers: []
};

I don't see any difference in these 2 objects, so why does JavaScript say these are different? I am using JSON.stringify(obj1) === JSON.stringify(obj2) to compare these 2 objects.

JSFiddle: http://jsfiddle.net/9akeh8bh/4/

JNevens
  • 11,202
  • 9
  • 46
  • 72
  • Could you please provide jsfiddle/plunker? – Miraage May 05 '15 at 09:15
  • 1
    Without looking at the objects: In JavaScript, object properties are not in any particular order. So it might be possible that the JSON representation of an object looks different from the JSON representation of another object with the same content. – basilikum May 05 '15 at 09:17
  • you can really find out the differences if you use the tool to help you. Some keys are not the same. – R Lam May 05 '15 at 09:21

2 Answers2

3

There are differences between the two objects; some keys have underscores and some are uppercase.

Have you tried standardising the key names and seeing if the comparison works?

Jonathan Smith
  • 2,390
  • 1
  • 34
  • 60
  • I have adjusted the keys, but it still returns false. Does it have something to do with the `reactions: [Object]`? – JNevens May 05 '15 at 09:42
0

Your reactions are different, some contain Objects, some do not.

Highmastdon
  • 6,960
  • 7
  • 40
  • 68
Ed Knowles
  • 1,925
  • 1
  • 16
  • 24