Search code examples
node.jsassertdeepequals

strange new Date in deepEqual


I have this code

var assert = require('assert');

describe('date', function() {
  it('deep equal', function() {
    assert.deepEqual({date: ''}, {date:new Date()});
  });
});

when I run the test with mocha I get this

  AssertionError: { date: '' } deepEqual { date: 2017-03-08T21:58:45.767Z }
  + expected - actual

   {
  -  "date": ""
  +  "date": [Date: 2017-03-08T21:58:45.767Z]
   }

  at Context.<anonymous> (test/test_date.js:5:12)

Why the date generated in the deepEqual have this format [Date: 2017-03-08T21:58:45.767Z] and not this format 2017-03-08T21:58:45.767Z ?

Why the date generated is between the brackets [Date: ...] ?


Solution

  • It seems to me that this is the way your testing suite shows you that the object is an instance of the Date class. You would not get this information if it were simply 2017-03-08T21:58:45.767Z, and would possibly be harder to debug in a more complex scenario.

    In the first line,

      AssertionError: { date: '' } deepEqual { date: 2017-03-08T21:58:45.767Z }
    

    It's showing the toISOString() representation, but this could be misleading because the value of date is not that string. The value is a Date object, so in the diff it makes that clear.