You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Don't try to make WebSocket tests work in two environments
After struggling for a long while with trying to "shim" isomorphic-ws
to work normally in a jest environment, it is easier to just run this
test only in a Node environment.
Here are the issues why this test fails in JSDOM:
1. Imports fail
The statement `import WebSocket from 'isomorphic-ws';` is passed to the
Jest module resolver, which utilises `ts-jest-resolver` (in our case) to
find the right file to load. Sadly, it will always load the "main"
module of a node package.
2. isomorphic-ws "browser" option is ESM for some reason
If we use jest.config.cjs "resolver" field [1] to define a custom script,
we will be able to modify the "main" field by patching package.json in
memory. This works fine, however isomorphic-ws's browser version is an
ES Module for some reason, even if the package.json > "type" is
"commonjs".
3. Something inside jest doesn't want me to hack it to support ESM
If I override the package.json even harder to also change the "type" to
"module" just for isomorphic-ws, Jest doesn't respect it
4. ts-jest doesn't want to change the ESM imports to CJS imports
Since... we told it to not change ESM imports to CJS imports, it will
not.
[1]: https://jestjs.io/docs/configuration#resolver-string
0 commit comments