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

133
Views
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
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!