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

122
Visualizações
Chai deep equal not working on URLSearchParams

I'm trying to test if my URLSearchParams are correct with chai's to.deep.equal option but it returns true (is equal) when it shouldn't.

See the running code at https://stackblitz.com/edit/node-qx7qra?file=index.test.js

Let's assume that we're testing this function:

function getIncorrectParams() {
  return new URLSearchParams({
    from: '2020',
    to: '2021',
    ids: ['abc-1'],
  });
}

If I write:

test('via string', () => {
  expect(getIncorrectParams().toString()).not.to.equal(
    new URLSearchParams({
      from: '2020',
      to: '2021',
      ids: ['abc-1', 'abc-2'],
    }).toString()
  );
});

Chai correctly notices that the strings are not the same, good. But as soon as I remove the toString and test with:

test('via deep equal', () => {
  expect(getIncorrectParams()).not.to.deep.equal(
    new URLSearchParams({
      from: '2020',
      to: '2021',
      ids: ['abc-1', 'abc-2'],
    })
  );
});

It claims that the assertion failed because the two objects are "equal". Note if you actually go ahead and run the example, the debug output clearly shows that there are differences!

And the deep.equal property works if I use "normal" object. See this example where I convert the URLSearchParams to a "plain object" first:

test('deep equal plain objects', () => {
  expect(Object.fromEntries(getIncorrectParams().entries())).not.to.deep.equal({
    from: '2020',
    to: '2021',
    ids: ['abc-1', 'abc-2'],
  });
});
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