26

I need to assert equality between two points in my JavaScript unit tests:

var pnt1 = {x: 2, y: 3};

and

var pnt2 = {x: 2, y: 3};

When I do

assert.equal(pnt1, pnt2);

It says the points are different. Can I exclude from the check the fact that the objects are different instances (so in fact they are "not equal")?

I'd like to avoid creating a list of assert, one for each field to test (in this case .x and .y)

Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159

1 Answers1

28

Instead of .equal, use .deepEqual:

assert.deepEqual(pnt1, pnt2);

This will perform a deep comparison instead of simply checking for equality.

Pang
  • 9,564
  • 146
  • 81
  • 122
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • brilliant. so deepEqual will traverse the object till the basic types (where we don't have the concept of different instances, at least in JS). – Gianluca Ghettini Nov 26 '15 at 10:11
  • That's the gist of it, yes, @GianlucaGhettini. – Cerbrus Nov 26 '15 at 10:23
  • If it's not obvious, `assert.deepEqual({a: 1, b: 2}, {b: 2, a: 1})` is also true, even though OP's example object keys are in the same order. – ggorlen Oct 23 '21 at 04:00