Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
Asserting ObjectId equality in test cases

I have a test case that wants to assert that the mongoose document returned from the method under test is the same as one I already have. To check that they are equal, my intent is to simply verify that they have the same _id property.

const expected = MyModel.find(/*...*/);
const actual = await foo(); // method under test
expect(actual._id).to.deep.equal(expected._id)

However, I have found that the deep.equal check passes even when the _ids of the documents are not the same.

I reproduced the problem into this small example:

import { expect } from 'chai';
import { Types } from 'mongoose';

const a = new Types.ObjectId();
const b = new Types.ObjectId();

expect(a).to.deep.equal(b); // unexpectedly passes

I have verified that I can work around the problem by comparing the string representation of each ObjectId instead:

expect(a.toString()).equals(b.toString()); // fails (as expected)

Why does chai incorrectly assert that the two ObjectIds, which are different object instances with different values, are deeply equal in the original example?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!