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

177
Vistas
When unit testing mjs files, is there a way to mutate an imported function without using jest.mock?

Using module mocks in Jest has been done since the dawn of time, however as we move into the world of mjs it's impossible to intercept calls to require at runtime to enable this pattern.

jest.mock('./filename')

While I generally try my best to parameterize my dependencies, there are some cases where it's not possible (such as third party dependencies) or it's inconvenient and I need to explicitly mock out a single or multiple properties in a module.

I have tried various ways to reassign a property:

// foo.mjs
export function foo() {
  console.log('foo')
}

Simple reassignment - fails

import { foo } from './foo.mjs'
import * as fooModule from './foo.mjs'

fooModule.foo = function() { 
  console.log('bar')
}

console.log(foo())

Object.defineProperty - fails

import { foo } from './foo.mjs'
import * as fooModule from './foo.mjs'

Object.defineProperty(fooModule, 'foo', { value: function() { 
  console.log('bar')
}})

console.log(foo())

Object.assign - fails

import { foo } from './foo.mjs'
import * as fooModule from './foo.mjs'

Object.assign(fooModule, {
  foo: function() { 
    console.log('bar')
  }
})

console.log(foo())

All of these attempts fail with

TypeError: Cannot assign to read only property 'foo' of object '[object Module]'

In new Node with modules, is there an expected way to monkey patch modules or parts of a module such that all subsequent imports of that module receive that patch?

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