Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!