-
-
Notifications
You must be signed in to change notification settings - Fork 366
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
Ky typescript, jest and es modules + temp solution #170
Comments
I would try discussing this on the Jest support forums instead. Ky is just a normal ESM module. The problem lies in how complex Jest and Lerna are. |
Same problem with Vue CLI 4 configured as Typescript/Jest/ESLint combo.
|
The most likely reason that I think what you should do is just use CommonJS-style imports with the UMD build everywhere that you depend on Ky, since it's having trouble with the ESM syntax. I don't see a good reason to re-write the path in your config like that. It would be better to use that path in your app and in your tests directly, in my opinion. So just do this: const ky = require('ky/umd'); ... Instead of ... import ky from 'ky'; ... or any other combination you may be using. |
Agree... the reason: you'll loose the typings. Would love to have a better option though. |
Well, so IMO, The jest way of fixing this is through
Yes but some popular ones: Regarding ky. I would just add a note in the doc or keep this issue as closed, so people would not spend time looking through SO or jest issues. The trick I posted
might be and unsexy overkill but does not affect your bundle and works with lerna too (that's where I spent time). But I would not document use of require, neither suggest to use |
I found to get this working with typescript and I want to write this code:
So I configured jest with:
As the
Lot of effort but at least its inline with how the webpack bundle operates. |
this seems to work for me |
@riccardo-forina Thanks for this simple solution! however... @sindresorhus v0.26.0 broke it (because #315 - drop UMD). |
Never mind, solved it with:
(Explanation to whoever stumbles upon this thread like me: By default jest ignores Thanks for a great package! |
Using transformIgnorePatterns didn't work for me |
Don't forget to remove For anyone who still can't get this to work, I suggest linking to a GitHub repo or CodeSandbox with a minimal reproducible example where we can all take a look and collaborate on the solution. |
This is not working for me either. I removed
And If I modify this to:
Then it won't work, no matter what order the lines appear. However, if I replace it with this:
Then it works. I'll have to try it but I don't think replacing the entire config like this is an option for most CRA users. |
I'm happy to take a deeper look if someone posts a link to a minimal reproducible example. :) |
It does work with a
Here's a link to a sample repo. https://github.com/robcaldecott/ky-jest Run |
|
@bugzpodder what environment is that error from? |
@sholladay I'm using latest CRA 4.0.1 + React 17 + yarn 2 (berry) + pnp + ky 0.36. EDIT: hmm i just created a fresh repo using the versions above and everything seem to work fine. weird. |
ok i figured it out, i am using node 10 and ky requires node >= 12, the globalThis error is from node 10. |
@bugzpodder I'm glad you got it working! Is your setup different than the repo that @robcaldecott posted in #170 (comment)? If so, would you be willing to put up an example repo we can link to for people who are having trouble? |
It should be the same. Use node 12 and the globalThis error should be gone. |
For anyone else coming across this when using Jest + TypeScript via Note the inclusion of // jest.config.js
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
transformIgnorePatterns: ['/node_modules/(?!(ky))'],
} You will need to polyfill // my-test-file.test.ts
import 'cross-fetch/polyfill'
test('my test', () => {
// ...
}) |
@angeloashmore we have ky-universal to take care of polyfilling for you. I'd strongly recommend using that instead of polyfilling yourself, because there's more than just |
@sholladay Thanks, I did try ky-universal but using it is not straightforward, at least with the combination of Jest, TS, and the version of Node (v14.15.3) I am using. ky-universal shouldn't be transpiled, as recommended by you in another thread (sorry, not exactly sure where I read that earlier today). By transpiling using
Not transpiling leaves in
The only difference between the two runs is singling out module.exports = {
preset: 'ts-jest/presets/js-with-ts',
- transformIgnorePatterns: ['/node_modules/(?!(ky))'],
+ transformIgnorePatterns: ['/node_modules/(?!(ky/))'],
} I don't doubt there's some combination of config that allows this, but I kind of gave up. Happy to provide more details if you're able to guide a correct setup. |
@angeloashmore your You seemed to get a bit closer with the second error where it said "Jest encountered an unexpected token". That error links to docs on using ESM: https://jestjs.io/docs/en/ecmascript-modules Based on those docs, it sounds like they want your app to also be in ESM mode ( |
I managed to get it working by pasting the following into my "jest": {
"transformIgnorePatterns": [
"/node_modules/(?!(ky))"
],
"moduleNameMapper": {
"\\.(css|less)$": "identity-obj-proxy"
}
},
Although I'm not sure if this will break other tests down the road.. definitely a temporary solution for me |
* add ["@babel/plugin-proposal-private-methods", \{ "loose": true }] to Babel plugins (due to Babel's warning) * adjust mocking Ky (see sindresorhus/ky#170 and sindresorhus/ky#340) * add TS transformation
Hi there, I've tried the solutions on this issue but I can't seem to get it working. I'm stuck on the top-level await issue as @angeloashmore commented. My const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');
module.exports = {
roots: ['<rootDir>/src'],
transform: {
'^.+\\.(ts|tsx|js|jsx)$': 'babel-jest',
'^.+\\.svg$': '<rootDir>/svgTransform.js',
},
globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json',
diagnostics: false,
isolatedModules: true,
},
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
testEnvironment: 'jest-environment-jsdom',
setupFilesAfterEnv: ['<rootDir>/setupTests.ts'],
collectCoverage: true,
moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'jsx'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' }),
testPathIgnorePatterns: ['<rootDir>[/\\\\](node_modules|.next)[/\\\\]'],
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(ts|tsx)$', '/node_modules/(?!(@fullcalendar|ky|ky-universal)/)'],
}; As you can see, I already had a EDIT: I've got the tests running with this, I just changed the |
@alessandrojcm my understanding is that if you follow these instructions, then |
For the people using CRA, the option of |
Update from Ky team: Please see Jest's ESM instructions
Spend few hours trying to make typescript, jest, lerna and ky to play well together. Here's something that might help in case of:
With Jest you can simply add it to the
transformIgnorePatterns
, but the way lerna works your node_modules will probably end up in the top levelnode_modules
. So here's something that seems to works in every cases:This way, jest choose the umd build instead of es one.
Warning, this assumes there will always be an umd.js build packaged with ky.
There might be better solutions, feel free to let me know yours
The text was updated successfully, but these errors were encountered: