magic-akari / swc_mut_cjs_exports

[SWC plugin] mutable CJS exports
https://www.npmjs.com/package/swc_mut_cjs_exports
MIT License
57 stars 16 forks source link

Feat Request: add option to change getter to value #15

Open zyf0330 opened 2 years ago

zyf0330 commented 2 years ago

To be compatible with sinon.stub(obj, "method"), else this code must be changed to sinon.stub(obj, "method").get(sinon.stub())

AndyOGo commented 1 year ago

I have the same problem with stubbing dynamic imports with sinon.

fatso83 commented 1 year ago

It is true that Sinon by itself cannot modify exports that immutable.

Since SWC is often setup to use CommonJS as the module system (which is the case here) for the transpiled code, you can easily achieve stubbing modules using Quibble or any other module loader hooks for CJS, and do stubbing in combination with Sinon, as described on the Sinon homepage. I even have an example for SWC using Quibble.

If you choose not to use a module loader, there are manual workarounds such as described here: https://github.com/sinonjs/sinon/issues/2403

zyf0330 commented 1 year ago

Thanks all of you.

fatso83 commented 1 year ago

@zyf0330 You did not have to close it, it still makes sense IMHO. The current way it works makes SWC work differently from how ts-node works, for instance, and as such tests need to be rewritten. I just mentioned some workarounds.