Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda