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

324
Views
Import code into file - not scoped, not a module

I am currently writing some tests in vitestand need to mock the fs module. So inside the test file I did this:

vi.mock("node:fs", () => {
   return {
      readFileSync: vi.fn().mockReturnValue(`mocked value`),
      existsSync: vi.fn().mockReturnValue(true),
   }
})

This works, but I would like to create a file just for this code and import it then. However, vi.mockdoes not work when imported and executed inside a function (maybe I am doing something wrong here?)

So instead, I thought I would do something like this: import "./mockFS", hoping that the code does not get imported as a module. It does not work though.

Is there a way to achieve this, even though you normally really should not do something like this in "normal" code?

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!