-
Notifications
You must be signed in to change notification settings - Fork 18
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
Unable to get Typescript working with many ES modules #16
Comments
I was able to get it to work by doing |
Hey @therealpaulgg these types of issues come up a lot but they're always related to Jest, generally with how Jest resolves modules and transforms them. Jest only uses Anyway ... that was to help you potentially debug in the future but in your case you're using TypeScript to resolve modules so ... the first thing I'd say is you can remove transform: {
// ...
"^.+\\.(js|ts)$": "ts-jest"
} The second is you should configure TypeScript to use the "compilerOptions": {
// ...
"moduleResolution": "node"
} Try these changes, if it doesn't still work let me know if you're receiving any new errors or the same. |
Still having the same issue. I've changed my config (removed the extends svelte tsconfig portion, wasnt sure if that was messing with compilerOptions): tsconfig.json: {
"include": ["src/**/*", "src/types/*"],
"exclude": ["node_modules/*", "__sapper__/*", "public/*"],
"compilerOptions": {
"types": ["jest", "svelte"],
"allowSyntheticDefaultImports": true,
"target": "ES2019",
"moduleResolution": "node",
"allowJs": true,
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
jest.config.js: module.exports = {
preset: "ts-jest",
testEnvironment: "jsdom",
transform: {
"^.+\\.svelte$": [
"svelte-jester",
{
preprocess: true
}
],
"^.+\\.(js|ts)$": "ts-jest"
},
moduleFileExtensions: ["js", "ts", "svelte"],
setupFilesAfterEnv: ["./jestSetup.ts"],
transformIgnorePatterns: [
"node_modules/(?!(svelte-typewriter|svelte-flatpickr)/)"
],
moduleNameMapper: {
"\\.(css|less|scss)$": "identity-obj-proxy"
},
globals: {
"ts-jest": {
isolatedModules: true
}
},
testPathIgnorePatterns: ["/lib/", "/node_modules/"]
} Also worth noting I guess, since it's weird, I am using flatpickr as well for something using the format
|
Oh there's few more steps to configure |
If you also want the original Svelte files when using Jest then it's a good idea to use |
Same deal 😕 also not totally how to use jest-svelte-resolver but it gave me an error basically immediately when parsing file:
|
That really sucks @therealpaulgg, so we don't go back and forth too much, when I have some time today I'll set up a project just like yours, and I'll see if I can get it working. |
Hey @therealpaulgg I tried to recreate the issue but I can't. Everything works on my end. Can you give me an example of some file where you're trying the things that are failing? Some demo Svelte component with whatever you're importing and using would be awesome. |
Yes in fact I can send you the entire project setup I'm using... My test in |
Okay so first thing we now know is that Now need to figure out why |
So Jest always resolves based on the |
Finally, |
Overall here's what you can do:
{
"extends": "@tsconfig/svelte/tsconfig.json",
"include": ["src/**"],
"exclude": ["node_modules/**"],
"compilerOptions": {
"types": ["jest"],
"allowJs": true,
"allowSyntheticDefaultImports": true,
}
}
module.exports = {
preset: "ts-jest/presets/js-with-ts",
globals: {
'ts-jest': {
// Let's avoid type checking during tests (performance boost).
diagnostics: false
}
},
transform: {
"^.+\\.svelte": [
"svelte-jester",
{
preprocess: true
}
],
"^.+\\.(js|ts)": "ts-jest"
},
moduleFileExtensions: ["svelte", "js", "ts"],
setupFilesAfterEnv: ["./jestSetup.ts"],
transformIgnorePatterns: [
"node_modules/(?!svelte-typewriter|svelte-flatpickr)/",
],
moduleNameMapper: {
"\\.(css|less|scss)$": "identity-obj-proxy"
}
}
|
Ok, thanks! tried all that. I am now getting |
Ah it might be, I didn't check it and I cleaned up the project. Does |
same problem as moment, my whole project complains when i do any import like that |
That hack I linked earlier might do the trick then if import * as dayjs from "dayjs";
const dayjs = require("dayjs").default || require("dayjs"); |
Hey guys, is there any update on this? I'm also experiencing the same issue when I try to use the |
@ngavinsir unfortunately I don't recall this ever being solved for me. I have not worked on my project that required svelte in a while. Not sure if it is indicative of a problem with svelte-jester or something else. |
Ah, okay, thanks for your response 👍 |
Hi @mihar-22, I have the same issue as @therealpaulgg (except I use but, the solution about dayjs does not work: Do you have any workaround for dayjs? thanks |
A nasty workaround for
|
Can you please retry with the latest version and jest 27+? |
Hi! I had the same problem, the hack I came up with is: In my test setup script ( setupFilesAfterEnv: ["./jestSetup.ts"] in jest.config.js)
Hope it helps |
That's the only solution which works for me. I use it for my troubles with Flatpickr:
and then you make a new file ''/src/flatpickr.jest.js:
Thank you @wgrabias |
It seems like TypeScript isn't the real culprit here. @sebastianrothe I've created a reproduction repository with the latest versions of Jest and svelte-jester to demonstrate that the issue persists. What's confusing about this issue is that if one uses |
We have some tests with |
@sebastianrothe never mind, turns out it's an issue with However, I would personally prefer not using the experimental ES module support. Is there any chance this could be fixed inside of |
We do ship both versions (CJS and ESM). You may have to manually override the transformer in your
|
This configuration didn't resolve the issue in the reproduction repository:
Did I understand you correctly? |
Yes, thats correct. But, I saw that your project is ESM (https://github.com/illright/jest-clsx-repro/blob/main/package.json#L21). Jest is the culprit, sorry. Jest needs the experimental flag to work in ESM mode(https://jestjs.io/docs/ecmascript-modules). Node understands ESM natively. |
I have a typescript Svelte component that makes use of moment.JS, importing like so:
import moment from "moment
It works with my rollup compilation and development server flawlessly. However, when using svelte-jester and using preprocessing, it says that 'moment is not a function'.
Here is my jest configuration:
jestSetup.ts:
tsconfig.json (have tried many variants):
test output:
This isn't the first time I have had trouble with transpiling. There have been issues with getting other Svelte components to work, problems with
flatpickr
, etc.Please let me know if there is something I am doing wrong.
The text was updated successfully, but these errors were encountered: