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

111
Vistas
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 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