Skip to content
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

fix(jest-runtime): make sure a module can never be its own parent #5241

Closed
wants to merge 2 commits into from

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Jan 6, 2018

Summary
Fixes #5235.

I'm not sure if this is the correct fix. Technically the parent of the test files is something internal to jest. And the condition I've added seems to be true for lots of different files, and I don't really understand why...

Test plan
Integration test added. Would love some help adding a unit test.

@codecov-io
Copy link

codecov-io commented Jan 6, 2018

Codecov Report

Merging #5241 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5241      +/-   ##
==========================================
+ Coverage   61.18%   61.19%   +<.01%     
==========================================
  Files         202      202              
  Lines        6771     6767       -4     
  Branches        4        4              
==========================================
- Hits         4143     4141       -2     
+ Misses       2627     2625       -2     
  Partials        1        1
Impacted Files Coverage Δ
packages/jest-runtime/src/index.js 73.81% <100%> (+0.19%) ⬆️
packages/jest-config/src/utils.js 85.41% <0%> (-2.59%) ⬇️
packages/jest-config/src/normalize.js 93.1% <0%> (ø) ⬆️
packages/jest-cli/src/search_source.js 43.05% <0%> (+0.95%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a9a3df0...cd0533e. Read the comment docs.

@cpojer
Copy link
Member

cpojer commented Jan 6, 2018

I think this is the right fix. Test files, or the first module required in a context, are to be considered entry points.

However, I'm wondering if you are seeing module.parent being null more often than not because the real path doesn't match a symlinked file name? Let's make sure we aren't breaking module.parent in other places.

@SimenB
Copy link
Member Author

SimenB commented Jan 6, 2018

Any cases in particular you've got in mind beyond symlinks? And ideas for a test I can add to that effect?

What if the path is to a virtual mock, I can't do realpath on it then, can I?

@@ -509,6 +509,10 @@ class Runtime {
({
enumerable: true,
get() {
if (localModule.filename === from) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally had moduleRegistry[from] === localModule, but I think it's the same thing in practice

@cpojer
Copy link
Member

cpojer commented Jan 7, 2018

Before merging, can you make a list here with the modules that don't have a parent after this change?

For virtual mocks, the parent should be the module that first requires the mock.

@SimenB
Copy link
Member Author

SimenB commented Jan 7, 2018

Before merging, can you make a list here with the modules that don't have a parent after this change?

Sure!


@jdalton said this in the original issue:

The module.parent is the first module to require it. So I'd guess some jest file.

Which I sorta agree with, and how I expected it to work. I think I have to dig a bit deeper into it.

@cpojer
Copy link
Member

cpojer commented Jan 7, 2018

I don't agree with that. Test files should not have a parent because they are entry points. The fact that they are run via Jest shouldn't matter. Basically, it should be "the same" as doing node path/to/test.js.

@SimenB
Copy link
Member Author

SimenB commented Jan 7, 2018

That makes sense. I'll see if I can get some flag or something indicating if a file is a test file or not, and explicitly set parent to null for them.

@cpojer
Copy link
Member

cpojer commented Jan 7, 2018

Keep in mind that jest-runtime may be used standalone with things other than tests, but what you are saying makes sense. You can theoretically use a flag that you set after the first file has been loaded in the context. Note that "first" file here means user file, all the other test related code is loaded using the internal require functions. Maybe we can just set this automatically after using the first non-internal require call on runtime?

@cpojer
Copy link
Member

cpojer commented Feb 23, 2018

Closing this, but we still need to fix it.

@SimenB
Copy link
Member Author

SimenB commented Feb 23, 2018

Agreed. Should be easier with #5618 merged. I'll try to find the time 🙂

stieg added a commit to stieg/jest that referenced this pull request Apr 12, 2018
cpojer pushed a commit that referenced this pull request Apr 15, 2018
* Do not include `from` information when its not valid (#5235)

If you include the information then when the _execModule routine loads the
module without a from context then it will incorrectly setup a circular
dependency by declaring the parent is itself. By checking for a module name
and not including that information when passed in, the issue is avoided.

* Copy in integrations tests from pull #5241

Done at the request of @SimenB from PR #5972
@SimenB SimenB deleted the module-parent-in-test branch April 17, 2018 09:36
@github-actions
Copy link

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

module.parent is incorrectly circular in ^22.
4 participants