-
Notifications
You must be signed in to change notification settings - Fork 156
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
chore: fix esm tests #1742
base: test/use-esm
Are you sure you want to change the base?
chore: fix esm tests #1742
Conversation
|
Thanks for the fixes and for explanations! I'll take a deeper look into this once I have time. |
Moving away from jest might sometimes be the way to go, at least that is what I experience once when migrating to node 18 and switching to vitest. |
For some reason the cli-package-test action which used to work in the original PR is failing (and some other actions too). That's strange because it passes locally (as far as I can judge from a quick test). It needs further investigation before merging. |
Right, I see that. I focused mainly on figuring out loading certain modules. Definitely it requires further investigation, it should probably be draft pr instead. |
NP, I'll convert it. |
Ok, looks like I fixed those cli tests, at least it runs for me locally. |
Thank you! I think I'll get rid of direct importing package.json as the approach causes more issues than resolves. Maybe I'll just read it from fs or fetch. |
Another approach could be having a specific, accessible during runtime, version const that would be auto-updated during build process. Something like: // auto-updated during build
export const appVersion = '1.0.0'; and in
But importing package.json directly with that |
Unfortunately it still fails in Docker env: https://github.com/Redocly/redocly-cli/actions/runs/11032319745/job/30642998846?pr=1742 😅 |
What/Why/How?
This PR attempts to solve issues encountered after ESM migration, mostly due to incompatibility of commonjs modules (that are being used) with type definitions and they way how ESM is accessing them.
Identified problems:
moduleNameMapper
to jest config to map these modulesnode-fetch
default import causing missinterpretation of module structure by typescript leading to use ofThis probably happens because there is an old version of node-fetch (2.6.1) which is a commonjs module, that uses its own type definitions. These definitions are incompatible with the global fetch types from @types/node. The
esModuleInterop
flag helps but doesn’t resolve the conflict caused by duplicate fetch definitions.I've bumped
node-fetch
to v3 which solves the issue, but requires slight adjustments to typings.ajv
lib (@redocly/ajv/dist/2020.js
), also commonjs module, that is being accessed the same way. In that case bumping version might not be an option, so it requires different approach (that I failed to figure out).transform
to ensure ts-jest correctly handles ESM and import assertions.colorette
- there are missing type declarations for this package which is outdated. Upgrading it to newer version would fix it but it introduces breaking change forcolorette.options
that don't exist anymore, but is used throughout the code. A way to walk-around this (and what I ended up doing) is to add type definition file manually.with
token usage that is breaking tests. My assumption is that it might be caused byimport packageJson from '../../package.json' with { type: 'json' }
, however I wasn't able to walk around it and not sure if there isn't some otherwith
occurrence that I missednode --experimental-vm-modules
Reference
Testing
Screenshots (optional)
Check yourself
Security