Open damusix opened 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).
@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.
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.