evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.71k stars 1.11k forks source link

TypeError: Cannot set property X of #<Object> which has only a getter #3734

Open damusix opened 3 months ago

damusix commented 3 months ago

I have read https://esbuild.github.io/api/#platform and am aware that it clearly states:

When [bundling](https://esbuild.github.io/api/#bundle) is enabled the default output [format](https://esbuild.github.io/api/#format) is set to cjs, which stands for CommonJS (the module format used by node). ES6-style exports using export statements will be converted into getters on the CommonJS exports object.

However, this makes it extremely difficult to stub things that are compiled by esbuild. Is there an option to NOT make the ES6-style exports into getters?

import * as MyLib from './mylib'

const libMock = Sinon.mock(MyLib, 'myFunc').returns(true)

Doing something simple such as the above is impossible given that Object.defineProperty(...) will not override the property, and libraries such as Sinon, Proxyquire, ts-mock-imports, etc will also not work. Basically, testing ES6 code using esbuild is impossible at the moment.

evanw commented 3 months ago

The behavior you are complaining about is an expected part of how ES modules work in JavaScript. For example:

$ node --input-type=module -e 'import * as fs from "fs"; Object.defineProperty(fs, "foo", { value: "bar" })'
file://./[eval1]:1
import * as fs from "fs"; Object.defineProperty(fs, "foo", { value: "bar" })
                                 ^

TypeError: Cannot redefine property: foo
    at Function.defineProperty (<anonymous>)
    at file://./[eval1]:1:34
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.eval (node:internal/modules/esm/loader:218:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.10.0

Since esbuild implements the JavaScript specification, esbuild does this too. Specifically the [[Set]] internal method of a module namespace exotic object in returns false, which then causes the PutValue abstract operation to throw a TypeError exception. So this is how the import statement is supposed to behave in JavaScript.

The testing libraries that you want to use are likely designed for CommonJS code and are not designed for use with ES modules (see https://github.com/sinonjs/sinon/issues/2168 and https://github.com/thlorenz/proxyquire/issues/217 for example).

damusix commented 2 months ago

@evanw Looks like it might be an impossibility even with modern tools: https://www.npmjs.com/package/esmock I'm using tsx for running typescript, which uses esbuild in the background.