Skip to content

deepEqual() fails when testing objects with valueOf() equality #59

Closed

Description

@broofa

deepEquals does not take into account objects/object instances that will implicitely cast to primitive values using valueOf or toString(). For example:

rkieffer@rkieffer-mbp$ pwd
/Users/rkieffer/repos/nodeunit

rkieffer@rkieffer-mbp$ node
> assert = require('./lib/assert.js')
{ AssertionError: { [Function: AssertionError] super_: { [Function: Error] captureStackTrace: [Function: captureStackTrace], stackTraceLimit: 10 } },
  fail: [Function: fail],
  ok: [Function: ok],
  equal: [Function: equal],
  notEqual: [Function: notEqual],
  deepEqual: [Function: deepEqual],
  notDeepEqual: [Function: notDeepEqual],
  strictEqual: [Function: strictEqual],
  notStrictEqual: [Function: notStrictEqual],
  throws: [Function],
  doesNotThrow: [Function],
  ifError: [Function] }
> x = {foo: {valueOf: function() {return 9;}}}
{ foo: { valueOf: [Function] } }
> y = {foo: 9}
{ foo: 9 }
> x.foo == y.foo
true
> assert.deepEqual(x, y)
AssertionError: {"foo":9} deepEqual {"foo":{}}
    at [object Context]:1:8
    at Interface.<anonymous> (repl.js:98:19)
    at Interface.emit (events.js:27:15)
    at Interface._ttyWrite (readline.js:307:12)
    at Interface.write (readline.js:145:30)
    at Stream.<anonymous> (repl.js:76:9)
    at Stream.emit (events.js:27:15)
    at Stream._onReadable (net.js:757:14)
    at IOWatcher.onReadable [as callback] (net.js:276:10)
> 

Activity

  1. added a commit that references this issue on Sep 13, 2016
  2. mreinstein commented on Sep 13, 2016

    @mreinstein
    Collaborator

    resolved via 468b759

  3. added a commit that references this issue on Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions