Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda