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

116
Visualizações
How to test if function is called with async keyword

I want to write a simple test for my vue3 app, test should assert that specific function (updateRoute in this case) is declared with async in different components

Note: according to my current project I can't isolate this function in a single file to make it reusable

example:

const updateRoute = () => { doSomethig() }

should fail

while

const updateRoute = async () => { await doSomethig() }

should pass

the testing library doesn't matter, it can be Jest or anything else

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is no reliable way and no reason to detect async function. For a function that returns a promise, it's the same as:

const updateRoute = () => {
  return doSomethig().then(() => {}) // Returns a promise of undefined
}

It should be tested that updateRoute returns a promise, with async or not. In Jest it's:

expect(updateRoute()).toEqual(expect.any(Promise));

As for mandatory await, this can be handled with ESLint require-await rule if necessary.

about 4 years ago · Juan Pablo Isaza Relatório

0

Check if the contructor.name of the function is equal to 'AsyncFunction':

const x = async () => {};
const y = () => {}

console.log(x.constructor.name);
console.log(x.constructor.name === 'AsyncFunction');

console.log(y.constructor.name);
console.log(y.constructor.name === 'AsyncFunction');

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