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

461
Views
La prueba rápida usando JEST y ESM me da el error "Error de tipo: no se puede asignar a la propiedad de solo lectura 'suma' del objeto '[módulo de objeto]'"

Intenté usar broma para burlarme de la función importada pero recibí este error TypeError: Assignment to constant variable. o TypeError: Cannot assign to read only property 'sum' of object '[object Module]' , espero que obtuve el valor de retorno que me burlé en esta prueba

Intento 1

 import { jest } from '@jest/globals' import * as util from "./util.js" it("TypeError: Cannot assign to read only property 'sum' of object '[object Module]'", () => { jest.spyOn(util, "sum").mockImplementation(() => { return 2 }) expect(sum(1, 2)).toBe(2); })

Intento 2

 import { jest } from '@jest/globals' import { sum } from './util.js' it("TypeError: Cannot assign to read only property 'sum' of object '[object Module]'", () => { jest.mock("./util.js", () => ({ __esModule: true, sum: jest.fn().mockReturnValue(2), })); expect(sum(1, 2)).toBe(2); })

Intento 3

 import { jest } from '@jest/globals' import { sum } from "./util.js" it("TypeError: Assignment to constant variable.", () => { sum = jest.fn(() => { return 2 }) expect(sum(1, 2)).toBe(2); })

estoy siguiendo la documentación de broma https://jestjs.io/docs/ecmascript-modules para configurar mi configuración

paquete.json

 { "type": "module", "scripts": { "test": "NODE_OPTIONS=--experimental-vm-modules jest" }, }

jest.config.js

 module.exports = async () => { return { verbose: true, transform: {} }; };

creé este repositorio para su reproducción https://github.com/fei1990a/jest-esm/tree/main

Gracias por cualquier ayuda

over 4 years ago · Santiago Trujillo
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!