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

110
Visualizações
Testing a file that calls its own function

I have a file that is the entry-point to my application;

const main = async () => {
   // Do stuff
}

main().then();

I understand that I can export the main function, delete the call in the first file and in a separate file import and call the function there;

const entry = require('./rewritten-entry.js');
entry.main().then();

But for arguments sake lets say I really want to test this first file's main function. As soon as I import the file in my test, the main function will execute before I even reach the test code;

const entry = require('./original-entry.js')    // <-- main function executes here

const test = {
  // Do test stuff
}

For a test, I don't want this to happen. Likewise I don't want to mock the function since it is exactly what I want to test.

I see this common 'init' logic all over the place so I imagine there is a way to test this.

about 4 years ago · Juan Pablo Isaza
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