-
Notifications
You must be signed in to change notification settings - Fork 455
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
More safely resolve 'jest-config'. #853
More safely resolve 'jest-config'. #853
Conversation
Thanks for fixing this @iclanton! |
Just curious, why isn't |
Making |
Pull Request Test Coverage Report for Build 2173
💛 - Coveralls |
@iclanton thanks for the PR. I'll try to spend some time on it this Sun/Mon. Tho next time, don't bypass the commit hooks (it should have fail committing since your commit messages are not following the conventional commit format we are using) ;-) |
I couldn't figure out what the hook's expected commit format was, so I assumed the hook wasn't working correctly. Here's the output:
I can edit the commit messages and force push my branch. What is the format supposed to be? |
@iclanton that would be awesome. Also if you could rename any var/exported symbol which is |
21fae44
to
3aafaf3
Compare
3aafaf3
to
8ffef83
Compare
Done :) |
LGTM, but do you have some tests for this ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are many spots where you could just let your variable types be inferred from the return type of function calls on initialisation.
Other than that and the few spots where I left an inline review, LGTM.
src/config/create-jest-preset.ts
Outdated
const logger = rootLogger.child({ namespace: 'jest-preset' }) | ||
|
||
const jestConfigPkg: typeof jestConfigType = getJestConfigPkg(logger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use to start a type with an uppercase, and prefix with a T when it's a package export, so here I guess it should be named TJestConfigPkg
Anyway, here the getJestConfigPkg should be defined in a way so that the returned type is inferred here (I yet did not look at it in the jest-config-resolver-file). Long story short, you shouldn't have to import * as jestConfigType
and here should only be const jestConfigPkg = getJestConfigPkg(logger)
. Type will be inferred from function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, just saw now, the type IS returned from getJestConfig
, so simply remove the import and replace that line with what I wrote you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type isn't implied because the getJestConfig
is generic. I'll change it to not be generic and use the TJestConfigPkg
name you suggested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I remember why I did this the way I did it - giving getJestConfigPkg
a non-generic return type and returning the real type (typeof TJestConfigPkg
), the jest-config-resolver.d.ts
declaration file takes a dependency on jest-config
, which means that ts-jest
can't be referenced by other TypeScript projects that don't have a flattened node_modules
. I'm going to put it back the way I had it before to avoid this issue if that's okay with you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, good point! Tho if the type you are using is just defined for this file, move it out of types.ts and into this file.
@huafu It is much more valuable for code to be easy to read than easy to write: When other developers have to make extra effort to understand a file, it's more difficult for them to do a thorough code review, or to figure out how to contribute fixes. VS Code has a really nice IntelliSense feature that shows the inferred types, but often the person reading the code may be using a simple viewer such as GitHub or a visual diff/merge tool. So, I'd suggest not to rely toooo heavily on type inference, as it hides information that is often not obvious when reading someone else's code (even though it was probably completely obvious to the original author.) |
I agree, but for what I said the function name must be chosen so that it's clear enough to infer from the name. If not, one must change everything, as for readability it is also important to have the code written with same style everywhere. |
Do you think this can be merged in and released soon? We have some work that's blocked behind this. |
@huafu after a PR is already approved, what else is required to get the PR merged? |
ts-jest
seems to assume that consumers are using npm or yarn, which both flatten thenode_modules
folder and allow packages to import dependencies that are not declared in theirpackage.json
. The flattenednode_modules
folder assumption doesn't hold true for other package managers like pnpm and yarn's plug-and-play feature.jest-config
is imported on this line, butjest-config
isn't declared explicitly ints-jest
'spackage.json
. This wasn't a problem in version 22. This PR changes the resolution algorithm forjest-config
to find the specific version that jest itself is using.We also considered adding a peerDependency on
jest-config
, but that requires that the consuming package needs to take a direct dependency onjest-config
, and coordinate its version ofjest-config
to be the same as the version used byjest
.We aren't using the
ts-jest
preset, so it would actually be more efficient for our toolchain to reference an entrypoint ints-jest
that only exports the transform and nothing else in the module graph. If you're open to that change, I'll submit a PR that creates an entrypoint that only exports the transform.