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
Comments
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 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). |
@evanw Looks like it might be an impossibility even with modern tools: https://www.npmjs.com/package/esmock |
I have read https://esbuild.github.io/api/#platform and am aware that it clearly states:
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?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.The text was updated successfully, but these errors were encountered: