-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add hoisting of the "@jest/globals" import together with jest.mock #120
Comments
This comment was marked as spam.
This comment was marked as spam.
@kdy1 any plans on this? |
I don't see what that document is supposed to reveal here? |
I think you are talking about something different |
I think the easiest is to remove import {jest} from "@jest/globals"; completely since it is not needed for jest. If you check the repo I linked, tests work if you remove that import (breaks typescript types) but tests break if import is there |
It's a different issue from this. Please file a new one instead. |
I don't see how it is different |
He wants to mock ESMs, and it will never be supported. If your requirement is the same, it's also not planned. |
Ah it was the same issue. |
ok let me explain myself swc already hoists jest.mock by default that import is there purely for typescript, not for jest itself so what that import does is, it prevents swc from hoisting jest.mock properly as it was before If swc would move jest.mock under that import, or remove that import completely and hoist jest.mock as before, then the behavior would be preserved. |
Can be turned off via config. https://jestjs.io/docs/cli#--injectglobals A quick hack might be to just remove the import, run the transform/hoisting, then reinject the import afterwards. Would be wrong if people alias the imports, but still |
what I ended up doing is to write this kind of transfomer const { createTransformer } = require("@swc/jest");
/**
* Transformer for adapting imports from "@jest/globals" which only exist to provide strict types for jest api
* Before running swc we need to adapt them, because swc does not properly hoist jest.mock in case imports from
* `@jest/globals` are present
* We don't strip it, because we need to preserve original code for source maps to continue working, so we just prefix
* imports with `_`
*/
const removeJestGlobalsTransformer = {
process(src, filename) {
if (!src.includes("@jest/globals")) {
return src;
}
return src.replace(/import\s+{([\s\S]*?)}\s+from\s+['"]@jest\/globals['"];/gs, (match, p1) => {
// Prefix each imported name with an underscore while maintaining their original positions
const names = p1.split(",").map(name => "_" + name.trim());
const renamedImports = names.join(",\n ");
return `import {\n ${renamedImports}\n} from "@jest/globals";`;
});
}
};
const swcOptions = {
sourceMaps: "inline",
module: {
strict: false,
strictMode: false
},
jsc: {
target: "es5",
parser: {
syntax: "typescript",
dynamicImport: true
},
experimental: {
plugins: [["jest_workaround", {}]]
}
}
};
const swcTransformer = createTransformer(swcOptions);
const chainedTransformers = {
process(src, filename, config, transformOptions) {
const afterJestGlobalsRemoval = removeJestGlobalsTransformer.process(src, filename);
return swcTransformer.process(afterJestGlobalsRemoval, filename, config, transformOptions);
}
};
module.exports = chainedTransformers; I kept the injection of globals, but just slightly modified the imports so swc hoisting of jest.mock keeps working Regex might be needed to be adapted, by some better regex magic skills |
In the next example the hoisting of the
jest.mock
doesn't work:Is it possible to update swc AST to hoist
jest.mock
before themodule1.js
andmodule2.js
imports?The text was updated successfully, but these errors were encountered: