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

135
Visualizações
Jest failing one function but not another

I am writing some daily challenges for my coding bootcamp and I am running into an issue on one problem. I wrote a function that combines objects and it works correctly. Here is what the problem prompt is

Prompt: Write a function named mergeObjects that accepts at least two objects as arguments, merges the properties of the second through n objects into the first object, then finally returns the first object. If any objects have the same property key, values from the object(s) later in the arguments list should overwrite earlier values.

Examples:

mergeObjects({}, {a: 1});  //=> {a: 1} (same object as first arg)
mergeObjects({a: 1, b: 2, c: 3}, {d: 4});  //=> {a: 1, b: 2, c: 3, d: 4}
mergeObjects({a: 1, b: 2, c: 3}, {d: 4}, {b: 22, d: 44});  //=> {a: 1, b: 22, c: 3, d: 44

My function

 function mergeObjects(...obj) {
  let obj1 = {}
  obj.forEach(element => {
    obj1 = {...obj1, ...element}
  });
  return obj1
}

Solution function

function mergeObjects1(target, ...objects) {
  objects.forEach(function (obj) {
    // using ES2015's 'for in' loop
    for (var key in obj) {
      target[key] = obj[key]
    }
  })
  return target
}

In my eyes these two functions provide the same result. However, when I run my code through the jest test they created it fails on the first test. But the solution they provide, does not fail. The jest test is below.

describe('15-mergeObjects', function () {
    it('returns same object', function () {
      var obj = {}
      expect(mergeObjects(obj, { a: 1 })).toBe(obj)
    })
    it('adds additional properties', function () {
      expect(mergeObjects({ a: 1, b: 2, c: 3 }, { d: 4 })).toEqual({
        a: 1,
        b: 2,
        c: 3,
        d: 4
      })
    })
    it('merges props from left to right', function () {
      expect(
        mergeObjects({ a: 1, b: 2, c: 3 }, { d: 4 }, { b: 22, d: 44 })
      ).toEqual({ a: 1, b: 22, c: 3, d: 44 })
    })
  })

  describe('15-mergeObjects', function () {
    it('returns same object', function () {
      var obj = {}
      expect(mergeObjects1(obj, { a: 1 })).toBe(obj)
    })
    it('adds additional properties', function () {
      expect(mergeObjects1({ a: 1, b: 2, c: 3 }, { d: 4 })).toEqual({
        a: 1,
        b: 2,
        c: 3,
        d: 4
      })
    })
    it('merges props from left to right', function () {
      expect(
        mergeObjects1({ a: 1, b: 2, c: 3 }, { d: 4 }, { b: 22, d: 44 })
      ).toEqual({ a: 1, b: 22, c: 3, d: 44 })
    })
  })

Can anyone provide an explanation as to why the solution function passes while my function does not?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

While the results look the same, the two functions are slightly different.

In your function, you are creating a new object and then adding to it all of the required properties.

In the solution function, they are adding properties to the original target object and then returning it.

The returned objects from both functions will have the same keys and the same values, but different references, so they are not considered the same object in JavaScript.

In the test

 expect(mergeObjects1(obj, { a: 1 })).toBe(obj)

.toBe() checks whether two objects are the same (identity), therefore it fails the case of your function.

Note that there is a different test, .toEqual(), which checks whether two objects have the same keys and the same values (but possibly different references). Your function would pass this test

 expect(mergeObjects1(obj, { a: 1 })).toEqual(obj)
about 4 years ago · Santiago Gelvez Relatório
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