-
-
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
Provide require.main
property
#5618
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5618 +/- ##
==========================================
+ Coverage 60.89% 61.73% +0.83%
==========================================
Files 215 213 -2
Lines 7319 7176 -143
Branches 4 3 -1
==========================================
- Hits 4457 4430 -27
+ Misses 2861 2745 -116
Partials 1 1
Continue to review full report at Codecov.
|
moduleRequire.requireMock = this.requireMock.bind(this, from.filename); | ||
moduleRequire.resolve = moduleName => | ||
this._resolveModule(from.filename, moduleName); | ||
Object.defineProperty( |
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.
Instead of a traversal, couldn't we add the main module as an instance variable to the runtime instance and then just return it directly?
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.
This would require a to pass some flag in 3rd argument in other packages as well: https://github.com/facebook/jest/blob/master/packages/jest-circus/src/legacy_code_todo_rewrite/jest_adapter.js#L70 and https://github.com/facebook/jest/blob/master/packages/jest-jasmine2/src/index.js#L154
Since we cannot just assume that the first required module is main due to setup files.
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.
Got it, yeah I can live with this then.
Cool! This might help some with #5241 as well, as it rides on some of the same assumptions as |
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.
Changes LGTM.
Changelog? 🙂
In tests `require.main` will be set to module from which the test are being executed.
@SimenB I updated changelog, but looks like there's some problem with CI itself. |
require.main is provided by jestjs/jest#5618.
mainModule is blacklisted by jestjs/jest#4955 and require.main is provided by jestjs/jest#5618, but require.main can't be accessed on electron.
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. |
Closes #2150
Summary
Expose property
require.main
when testing and set it to module from which the tests are being executed. Currently this property is not set, which is inconsistent with default Node behaviour (Node ref) and the use case was present in #2150.Test plan
Appropriate unit and integration test cases were added to test if the property is provided in concrete modules and mocks.
Note 1: Module with test suite has to be manually added to
_moduleRegistry
, since the runtime is created manually instead of using the one from thejest-runtime
in which the test are being run.Note 2: This implementation assumes, that
module.parent
is set correctly and in order to findmain
module it traverse parents untilmodule.id
andmodule.parent.id
are equal, in which case it would be test suite module.