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

139
Visualizações
How to test function that returns object with functions inside

I have this function:

export const setData = (a, b) => (b !== undefined ? { a, b } : { a });

export const processData = arr => {
  const obj = {};
  arr.forEach(item => obj[item[0]] = val => setData(item[1], val);
  return obj;
};

The way I use this function is like this:

const data = [
   ['setSomething', 'SET_SOMETHING'],
   ['setAnotherThing', 'SET_ANOTHER_THING']
];

const setters = processData(data);

and then I can call them like setters.setSomething() or setters.setSomething(1234).

How can I test this with Jest? I thought of this:

const mocked = [
  ['setSomething', 'SET_SOMETHING'],
  ['setAnotherThing', 'SET_ANOTHER_THING'],
];

const result = processData(mocked);

expect(result).toEqual({
  setSomething: () => {}
  setAnotherThing: () => {}
});

But it fails saying this:

Expected:

    "setSomething": [Function setSomething],
    "setAnotherThing": [Function setAnotherThing],

Received:

    "setSomething": [Function anonymous],
    "setAnotherThing": [Function anonymous],

Any way to test this?

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