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

134
Vistas
I can't overwrite modules without export default

When I have the following scenario:

Scenario 1:

module.mjs:

const myAwesomeFunction = () => 42

export default {
  myAwesomeFunction
}

index.mjs:

import myModule from './module.mjs'

console.log(Object.getOwnPropertyDescriptor(myModule, 'myAwesomeFunction'))

/* Returns

{
  value: [Function: myAwesomeFunction],
  writable: true,
  enumerable: true,
  configurable: true
}
*/

myModule.myAwesomeFunction = () => 84
console.log(myModule.myAwesomeFunction()) // prints 84

I can overwrite the myAwesomeFunction from my module to a custom function I made after, which is useful for testing purposes, I can use spyOn from Jasmine to mock that method before it gets used by another module

But when I have the following scenario:

Scenario 2

module.mjs

const myAwesomeFunction = () => 42

export {
  myAwesomeFunction
}

index.mjs

import * as myModule from './module.mjs'

console.log(Object.getOwnPropertyDescriptor(myModule, 'myAwesomeFunction'))

/* Returns

{
  value: [Function: myAwesomeFunction],
  writable: true,
  enumerable: true,
  configurable: false <- That's a issue
}
*/

myModule.myAwesomeFunction = () => 84
console.log(myModule.myAwesomeFunction())

/* I get:

TypeError: Cannot assign to read only property 'myAwesomeFunction' of object '[object Module]'
    at file:///private/tmp/stackoverflow/index.mjs:15:28
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:337:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)
*/

So, I can't stub methods coming from modules that don't have an export default. Like the ZX module that I've been using in a project. I tried using const myModule = await import('module.mjs'), but it doesn't work too.

For some reason, modules with export default are configurable, but without they are not.

Is there any workaround I can do to get these modules that match the second scenario configurable? My main use case is to get the spyOn from Jasmine able to mutate the original object, as it tries to do.

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