-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Cannot redefine property
error when using jest.spyOn
after v1.2.206
#5059
Comments
Duplicate of #3843? |
I don't believe it is. This is an issue introduced specifically by v1.2.206 from a couple days ago, while the issue you're referencing dates back to 2021 and doesn't seem to actually throw an error. |
No it's duplicate. Aligning to ES specificarion is always a bugfux |
If you think this is not a duplicate, please provide a repro which use ESM |
@kdy1 thanks for looking into this. I'm trying to understand what the issue is and how to resolve it. I believe #3843 is about stubbing an exported function that is called within its module. This is not what I'm doing here. Instead, I'm trying to stub an exported function that is called outside of its module, and it errors out on the Our codebase is typescript transpiling to commonjs and we don't use ESM. This is why I don't understand your comment about aligning to ES specification, since this is a commonjs output. Are you saying that SWC no longer supports stubbing functions exported by commonjs modules, as of v1.2.206, and should not going forward? |
It's basically the same thing, and #3843 is not the only one about wrong usage of exports. |
If you want cjs behavior, you can use common js instead of ESM imports, or use |
In ESM mode, you can never write anything on the exports from a module. |
Unfortunately, In CJS world, you cannot import an ESM module, you will never run into this issue. If I remember correctly, node can natively interoperate between ESM and CJS . |
Oh, I see., thanks! |
Just to clarify again: none of these modules (either jest.setup.ts or cache.ts) is ESM. They are typescript files that are transpiled to commonjs modules. I set them up so that they compile to commonjs, and I expect Is the new behavior to transpile a module to ESM if it is required/imported using |
@ldiqual If you do not expect an esm behaviour, do not use esm syntax. use CJS module.exports, or CTS export synatx. function getRedis() { }
export = {
getRedis,
foo,
bar
} |
Running into the same thing. Worked fine in v1.2.204 After upgrading and getting the error, I tried downgrading and still ran into the same issue in my local machine / environment, so for some reason, it's not downgrading correctly either. |
@shahmirn This is not a bug. |
@kdy1 I agree that the new behavior may not be a bug, but rather is fixing a previous bug, and the change results in a stricter / more correct adherence to the esm specification, but an argument can be made that this is a breaking change, and hence warrants a major version bump. Furthermore, as @ldiqual said, I'm also using typescript, and our tsconfig.json has module set to CommonJS, module resolution to node, and target as es2020, so I'm not sure how esm is even coming into the picture. |
Nope, I don't think breaking code depending on wrong behavior is a breaking change. It's a bugfix. About ESM part, you used ESM syntax |
When I set It is If the user expects But what if the user really wants |
@shahmirn - downgrading requires you to clear your jest cache. swc-jest does not create a cache key using the version of swc/core, and therefore the cache doesn't invalidate between version upgrades / downgrades. |
If this is truly a bugfix and not a bug, you should update the intro text here: https://swc.rs/docs/usage/jest This is no longer a drop in replacement for ts-jest as reverting to ts-jest reverts to the previous behavior |
@a88zach |
Okay. I decided not to change the documentation because it's a still drop-in replacement if you don't do wrong thing |
One workaround that I've found it so use
It'll a bit slower, but if there aren't many tests requiring spying functionality, it might be acceptable. |
Is this nuance/issue being left as it is or being worked on? I know there is some debate as to whether it should actually be If it is not being worked on I think we need to remove the line about |
I don't think breaking an already-broken code is wrong |
This issue will never be worked on, as a compiler should not fix wrong code |
@kdy1 thanks for clarifying I just wanted to check so I knew as, philosophical debates aside, Other than putting pretty much every dependant function into its own module and then mocking it, do you have any other solutions/recommendations regarding this issue? |
I think See #5205 |
I locked comments because #5151 is the correct thread to discuss about this |
Describe the bug
SWC version v1.2.206 is causing our jest setup file to error out when calling
jest.spyOn
. v1.2.205 does not error out.Input code
Config
Playground link
No response
Expected behavior
jest.spyOn
should stubcache.getRedis
Actual behavior
jest.spyOn
fails with the following error:Version
v1.2.206+
Additional context
No response
The text was updated successfully, but these errors were encountered: