-
-
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
Implement module.parent #4614
Implement module.parent #4614
Conversation
… `module.parent.require` is unavailable)
packages/jest-runtime/src/index.js
Outdated
@@ -496,6 +495,13 @@ class Runtime { | |||
localModule.parent = mockParentModule; | |||
localModule.paths = this._resolver.getModulePaths(dirname); | |||
localModule.require = this._createRequireImplementation(filename, options); | |||
localModule.parent = Object.assign({}, localModule.parent, { | |||
filename: lastExecutingModulePath, | |||
require: this._createRequireImplementation( |
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.
Couldn't this look up the parent module in jest-runtime
and use its require here, instead of creating a new require implementation?
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 I can access the parent module, then that'd be perfect (as that's what module.parent
is in node). I didn't find it though. Halp?
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.
Aren't we storing this in jest-runtime somewhere?
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 might be, I'll check
I did not know |
Not sure if |
7b0e219
to
b0fddc7
Compare
I tried to set The first test The second one makes me kinda sad though. It seems to be one layer to high in the hierarchy. Any ideas? |
Hmm, yeah I don't think I really want to support exposing a runtime for the parent module in Jest. If we wanna add this, could we make it a lazy getter, so that it is only executed when somebody actually accesses |
b0fddc7
to
074ad47
Compare
Wait, my analysis is off. Since I do |
It's already exposed: |
Yeah, but you are re-creating the runtime for the parent module in your diff, and I was proposing either not to do that and re-use the existing one, or creating it lazily. |
Do you mean to make |
Codecov Report
@@ Coverage Diff @@
## master #4614 +/- ##
=======================================
Coverage 56.17% 56.17%
=======================================
Files 194 194
Lines 6544 6544
Branches 3 3
=======================================
Hits 3676 3676
Misses 2867 2867
Partials 1 1
Continue to review full report at Codecov.
|
Yes |
I've confirmed using node that the behavior introduced in this PR is correct. $ cd packages/jest-runtime/src/__tests__/test_root
$ node -p "require('./inner_parent_module').outputString"
This should happen
$ node -p "require('./inner_parent_module').parentFileName"
/Users/simen/Development/jest/packages/jest-runtime/src/__tests__/test_root/inner_parent_module.js |
Actually one thing is wrong, test-file.js: console.log(module.parent); $ node test-file.js
null
$ node -p "require('./test-file')"
Module {
id: '[eval]',
exports: {},
parent: undefined,
filename: '/Users/simen/Development/jest/[eval]',
loaded: false,
children:
[ Module {
id: '/Users/simen/Development/jest/test-file.js',
exports: {},
parent: [Circular],
filename: '/Users/simen/Development/jest/test-file.js',
loaded: false,
children: [],
paths: [Array] } ],
paths:
[ '/Users/simen/Development/jest/node_modules',
'/Users/simen/Development/node_modules',
'/Users/simen/node_modules',
'/Users/node_modules',
'/node_modules' ] }
{} On a second look, if a file is required as an entrypoint, Two issues that I can see that is not related to this PR but to
EDIT: Opened a separate PR for those 2 last points: #4623 |
Nice work @SimenB. |
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
Fixes #4567.
This leaves outid
andexports
, but it implementsrequire
(which I need, and is my motivation for doing this) andfilename
/id
which should be enough forrequire-dir
to work.module.parent
is now fully implemented.Test plan
New tests added