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

351
Visualizações
Jest testing, keeps return undefined?

I'm trying to test a function with jest, and I simply can figure out what's wrong? It keeps saying it expects to return the output, but got undefined. I have tested the function elsewhere where it seems to return the correct array.

I'm calling my my function and passing it an Object, it's then supposed to return an array. Then I'm calling .toEqual(output) which is an array.

//This is my function
const allAddresses = [
];

const updateAllAddresses = (obj) => {
  const transferAmount = obj.transferAmount;
  const to = obj.to;
  const transferAddress = obj.address;
  const newBalance = obj.newBalance;
  const addressArr = [...allAddresses];
  console.log("This addressArr", addressArr);
  console.log("this is obj", obj);
  //To set your account to the new balance after transfer and
  //to check if the address you transfer to is your own account
  addressArr.map((address) => {
    if (address.account === transferAddress) {
      console.log("This is inside the map !!!!");
      address.balance = Number(newBalance);
    }
    if (address.account === to) {
      console.log("2");
      address.balance = Number(transferAmount) + Number(address.balance);
    }
    console.log("last part of the testing", addressArr);
    return addressArr;
  });
};

const obj = {
};
const output = [
];


//This is my test
describe("Update array", () => {
  test("update the array with the new information", () => {
    expect(updateAllAddresses(obj)).toEqual(output);
  });
});
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is a problem with your updateAllAddresses method. You are not returning anything then the result of your function becomes undefined;

add return to where you are using .map method.

  return addressArr.map((address) => {
    if (address.account === transferAddress) {
      console.log("This is inside the map !!!!");
      address.balance = Number(newBalance);
    }
    if (address.account === to) {
      console.log("2");
      address.balance = Number(transferAmount) + Number(address.balance);
     }
    console.log("last part of the testing", addressArr);
    return address;
  });
about 4 years ago · Juan Pablo Isaza Relatório

0

You cannot short circuit and return inside a map function. You should return the object after the map

Also, when you change address inside the map; It really does not change anything, since that address variable will be removed from memory on next iteration

about 4 years ago · Juan Pablo Isaza 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