transformed CJS
compatibility with jest
#5151
Replies: 6 comments 10 replies
-
Hmm... I think it's acceptable, considering the amount of requests |
Beta Was this translation helpful? Give feedback.
-
I don't have strong opinions of the change itself, just curious if this could be done in our jest plugin and let plugin owns config option. Having this option in top level swcrc sounds somewhat awkward and opens up potential abuse. |
Beta Was this translation helpful? Give feedback.
-
I think this will force many of swc users that use it as jest transformer to stay locked on 205 version, at least until jest supports esm properly |
Beta Was this translation helpful? Give feedback.
-
I published a swc plugin. This plugin will transform local exports with usage: {
"$schema": "http://json.schemastore.org/swcrc",
"jsc": {
"experimental": {
"plugins": [["swc_mut_cjs_exports", {}]]
}
},
"module": {
"type": "commonjs"
}
} Make sure that Transform example: export const child = () => {
console.log("Hello World!");
};
export const callChild = () => {
child();
}; export {};
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name],
configurable: true
});
}
_export(exports, {
child: ()=>child,
callChild: ()=>callChild
});
const child = ()=>{
console.log("Hello World!");
};
const callChild = ()=>{
(0, exports.child)();
}; This plugin is only for jest workaround. Not recommended for other uses. |
Beta Was this translation helpful? Give feedback.
-
We have a pinned issue already, but I pinned this as well to make #5151 (comment) more visible. |
Beta Was this translation helpful? Give feedback.
-
Can you give a status for this? |
Beta Was this translation helpful? Give feedback.
-
There are some issues about compatibility between
common_js
pass and jest.There are some cases.
ESM
environment withESM
test files. tracked by Meta: Native support for ES Modules jestjs/jest#9430CJS
environment withCJS
test files. everything works well.CJS
environment withESM
test files or the opposite. Node-based interop. Does anyone do this kind of thing?CJS
environment withCJS
test files which are transformed fromESM
. This is what we are talking about.The correct way is to turn case 4 into case 1 or case 2.
Because we follow ESM behaviour, exports are immutable. But we can't stop users from doing the wrong thing.
Users may want ESM output, but because of the current situation of case 1, They need jest in CJS environment.
So, can we provide a hack?
It's something like pnpm's shamefully-hoist
It's wrong, but it can help users get out of trouble and makes it
Just works
.We can add a
mutableExports
to CJS config.I think its role is similar to
allowTopLevelThis
from babel, since top level this in ESM should always be converted to undefined.I tested locally, and simply adding a
configurable: true
can get jest working again (Although it shouldn't).Related issues.
Cannot redefine property
error when usingjest.spyOn
after v1.2.206 #5059Beta Was this translation helpful? Give feedback.
All reactions