Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
damusix opened this issue Apr 25, 2024 · 2 comments
Open

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

damusix opened this issue Apr 25, 2024 · 2 comments

Comments

@damusix
Copy link

damusix commented Apr 25, 2024

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
Copy link
Owner

evanw commented Apr 25, 2024

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 sinonjs/sinon#2168 and thlorenz/proxyquire#217 for example).

@damusix
Copy link
Author

damusix commented Apr 29, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants