-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Updates babel-jest resolution #5932
Conversation
@@ -684,7 +684,9 @@ describe('babel-jest', () => { | |||
beforeEach(() => { | |||
Resolver = require('jest-resolve'); | |||
Resolver.findNodeModule = jest.fn( | |||
name => path.sep + 'node_modules' + path.sep + name, | |||
name => { |
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.
can you remove the curly brace + return again? Doesn't seem necessary here and Jest's code style is to not use these when it's a single expression.
@@ -684,7 +684,9 @@ describe('babel-jest', () => { | |||
beforeEach(() => { | |||
Resolver = require('jest-resolve'); | |||
Resolver.findNodeModule = jest.fn( | |||
name => path.sep + 'node_modules' + path.sep + name, | |||
name => { | |||
return name.indexOf('babel-jest') === -1 ? path.sep + 'node_modules' + path.sep + name : name; |
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.
Shouldn't this be name === 'babel-jest'
?
@@ -113,24 +113,20 @@ const setupBabelJest = (options: InitialOptions) => { | |||
}); | |||
|
|||
if (customJSPattern) { | |||
const jsTransformer = Resolver.findNodeModule( |
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.
You are removing the resolution for packages that are not babel-jest
, like ts-jest
or ./my-custom-file
or my-custom-transformer
. You still need to support those as well.
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.
Hm, I see what you mean. Do you think it would be reasonable to have the same findNodeModule
call as a fallback to avoid breaking the existing, but encourage people to use require.resolve
in their configuration?
module.exports = {
transform: {
"*.ts": require.resolve('ts-jest')
}
};
An issue is that it wouldn't work as-is in the package.json configuration, since functions can't be called here, so it would have to be kept inside a js configuration file.
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.
An alternative would be to defer resolving those paths until after the configuration has been completed, and then use the right resolver - but the same issue will occur for the resolver
option, which ironically will need to be manually resolved by the user through require.resolve
.
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.
What if in Jest we call Resolver.findNodeModule(value)
. If it returns null, we call Resolver.findNodeModule(require.resolve(value))
(or just require.resolve(value)
itself if possible). That way it doesn't affect users.
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.
But then it will resolve them relative to jest-config, not the user project. So ts-jest
for example could fail because it would not be listed as a dependency of jest-config
(whereas a require.resolve
from the configuration file would always work, because the configuration file would be in the toplevel project).
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 right, yeah this is a bit of a bummer :( I'd prefer not to demand people to rewrite their config with JS.
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 just read the code again and I don't think this PR prevents external transformers from working - setupBabelJest
is only used to detect babel jest and, if it can be found, automatically inject regenerator
. Any other loader will be resolved and required later, through the regular resolver (cf here).
I left a few comments. I think this will break resolution of custom transformers which we'll need to support. Can you fix that? |
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.
Yes, you are right. Assuming all tests are passing, this is looks good to me actually :) Thanks!
d2681a8
to
d6d9fc8
Compare
The failing tests are driving me crazy - if I update the snapshots they break in node 6, but if I don't they break in 8/9 :/ They don't look related to my diff either ... what do you think? |
Are you rebased on master? We use sourcemaps (provided by babel through babel-jest) to make the stacks correct, seems like it's off somehow on different nodes? |
@SimenB Yep, rebased on master. My theory is that the current codebase has an issue with Node 8/9 that was hidden in this test until now. The problem is on the following condition: https://github.com/facebook/jest/pull/5932/files#diff-8ee186ad0e07dceb57c11f7c7b5d072aR121 Before my commit, Jest was sometimes failing to locate babel-jest, probably because the tests were running inside a temporary directory. Due to this, the transpilation wasn't actually being ran at all, and the correct results were being returned on Node 8/9. Starting from my commit, babel-jest is now always resolved (by using the one that Jest depends on), so the transpilation always run, and we apparently always hit the bug where the sourcemaps aren't correctly computed on Node 8/9. What do you think? |
d6d9fc8
to
22f605f
Compare
22f605f
to
76adee3
Compare
Codecov Report
@@ Coverage Diff @@
## master #5932 +/- ##
==========================================
+ Coverage 64.33% 64.34% +0.01%
==========================================
Files 217 217
Lines 8286 8286
Branches 4 3 -1
==========================================
+ Hits 5331 5332 +1
+ Misses 2954 2953 -1
Partials 1 1
Continue to review full report at Codecov.
|
It's green now 🤷♂️ Edit: ah, you removed the line and column. Not sure if that's the correct fix... (Might be, have to give it a thought or two) |
Yeah, we discussed it with @mjesun and figured it would be good enough
until we get to make another PR to fix the sourcemap thing (since it seems
caused by something outside of the PR, and only affect projects that don't
have babel-jest available, which I'm not sure can happen under normal
circumstances), but if you feel otherwise let me know!
…On Tue, Apr 10, 2018, 12:56 PM Simen Bekkhus ***@***.***> wrote:
It's green now 🤷♂️
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5932 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AA_WaxraZt_yxK5cEe7ggwRrLlMAmLvzks5tnQ5egaJpZM4TI25k>
.
|
I'm not comfortable moving away from working sourcemaps, even though they might be working through a happy accident (working by accident is better than not working :D) If you can tell me how the current approach is wrong with regards to sourcemaps, I'll be happy to remove my objection (to the degree it matters) |
@SimenB this PR doesn't actually break the sourcemaps - they're already broken (to some extent). To reproduce, just create a new project with it('it', () => {});
it('it foo');
test('test bar'); I think the issue simply comes from babel just keeping track of the rows instead of rows+columns. |
Is it just broken in the alphas (meaning master) or in the stable 22 releases as well? |
This comment has been minimized.
This comment has been minimized.
@SimenB Not sure. Before the 23.0.0, not entering the test definition wasn't a hard error (it was just skipping the test altogether), and regular errors thrown from within the test definition don't seem to suffer from this issue in both 22.X and 23.X. |
Might mess up #5889 as well, then |
Possibly. Are you ok with this PR then?
…On Wed, Apr 11, 2018, 12:18 AM Simen Bekkhus ***@***.***> wrote:
Might mess up #5889 <#5889> as well,
then
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5932 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AA_Waw51GDgQ7U2m8Uc46uycY3ik0VLCks5tna4tgaJpZM4TI25k>
.
|
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.
If this does mess up the stack traces, we might want to revert. But the change itself is nice, so I think we should try 🙂
👍 |
This does indeed mess up #5889 (I've rebased it), but just for the globals test (same one you had to strip out the line and column info in this PR). I think I'll have to dig into why this change makes us lose column information |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
The
babel-jest
default transform is currently located throughResolver.findNodeModules
. This has the inconvenient that when using a custom resolver, this new custom resolver will be completely bypassed.Given that
require.resolve
is the standard and safe way to obtain the requirable path to a package, I think we should use it whenever possible, at least until the custom resolver has been properly setup (ie after the config has been normalized).Two notes:
This might be a slight breaking change if users were specifying different version of
babel-jest
than the default. I'm not sure it's often the case, so it doesn't feel too problematic.The
regenerator
package is still located usingResolver.findNodeModules
. It would be nice to fix this as well, but since this package is meant to be added by the user, only the user can userequire.resolve
to obtain its path (we can't useprocess.main
because it points to the Jest cli). Since it's quite more complex, it can be done later on.Test plan
I've updated the tests to match the behavior (ie mostly checked that the resulting configuration was matching whatever is returned by
require.resolve
rather than an hardcodednode_modules/babel-jest
).