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

163
Visualizações
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 à 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