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

158
Views
How to unit test a function inside a Higher Order Function in JavaScript?

How to adapt unit tests for an existing function that needs to be wrapped in a Higher Order Function?

I have two functions. getServerProps() fetching data and isAuthenticated() checking user authentication. Both in separate modules with their own unit tests.

export function isAuthenticated(getServerProps) {
  return async function (param1, param2, param3) {

    const isLoggedIn = await authService()

    if (!isLoggedIn) {
      return { shouldRedirect: true };
    }
    
    const serverProps = await getServerProps(param1, param2, param3);
    return { shouldRedirect: false, ...serverProps };
  };
}

.

export async function getServerProps(param1, param2, param3) {
  const data = new DataService(param1, param2, param3);
  return { ...data };
}

Changing the export of the second function to isAuthenticated(getServerProps) mixes two functions in one unit. How to isolate the initial getServerProps() for the purpose of unit testing?

What's the best way to write tests in such a situation?

about 4 years ago · Santiago Gelvez
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!