Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda