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

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