-
Notifications
You must be signed in to change notification settings - Fork 36
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
ES import of typescript with .js extension fails #64
Comments
The @swc/jest repo, which exists specifically for jest, even mentions using Jest with ESM, however that simply won't work once I actually want to type-check my code (Which I would still need tsc for) Right now my best bet is to actually keep the explicit .ts imports, which I prefer anyways, and to fork tsc to disable that .ts file import error, similar to how Deno does it. |
This seems to be related: swc-project/swc#3043 |
What does your config look like? You might have to tell module.exports = {
extensionsToTreatAsEsm: ['.ts', '.tsx'],
transformIgnorePatterns: [],
testEnvironment: 'node',
transform: {
'^.+\\.(t|j)sx?$': '@swc/jest',
},
}; |
Thanks for the suggestion @ewized. What fails is the import of my actual source files which are about to be tested if they are .ts files and .js is specified in the import ( As demanded by Typescript for now🙄). |
I have exactly the same problem. Everything works with SWC and imports specified with the additional |
In case you stumble upon this. I could fix the issue with the help of the ts-jest docs. You need to add module.exports = {
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
} to your |
Thanks @Mad-Kat, will give that a try! |
I also had to add this. Can this get added to the README and/or the docs? |
Related issue at Storybook: storybookjs/storybook#15962 Just to clarify, having ".js" extension in a TypeScript barrel file (an index that reexport internal modules) sounds weird but seems a normal pattern when working with ESM. So if Storybook or Jest doesn't accept that, the solution is indeed to tweak Storybook or Jest configuration, not the source code of the app or library. @Mad-Kat approach seems to work also with pure Jest, not using SWC |
Hey @Mad-Kat, how did you made SWC imports specified with the additional I keep getting a "Module not found error" whenever I add a Thank you 🙏🏻 Here's my config:
UPDATE: Found out that my issue is related to how Webpack handles import with file extensions (I am using |
The below works for me in my Node.js backend project. The rule applies to relative imports starting with a period (.) or double periods (..) "moduleNameMapper": {
"@src/(.*)": "<rootDir>/src/$1",
"^(\\./.+|\\../.+).js$": "$1"
} OR "moduleNameMapper": {
"@src/(.*)": "<rootDir>/src/$1",
"^(..?/.+).js?$": "$1"
}, |
I'm using swc/node in ts-node to strip typings from my nodejs codebase using their
ts-node/esm
loader.To keep
tsc
happy while also following the esm spec to keep the extension of all my imports, all my imports end as .js despite the source files being ts.While this works for both
tsc
andts-node
withswc
, all Jest tests fail due to imports of.ts
files with.js
that can't be resolved. I took the necessary steps to make Jest work with ESM, which used to work until I introduced TS there. Likewise, the tests work if I specify the ts extension for each of these imports, which would however break tsc.I'm currently failing to fit the pieces together and have a hard time finding documentation or maybe a simple example that utilizes Node with ESM, SWC and imports ts files in a test suite.
I would also be happy to follow the Deno approach and explicitely keep the actual .ts extension on imports - However, I don’t know how I would typecheck my code then without tsc.
The text was updated successfully, but these errors were encountered: