Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

356
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!