-
-
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
Add option to call initialize and end methods in workers #7014
Conversation
891451e
to
c6a4e52
Compare
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.
Should be documented in the readme as well.
What about setup
and teardown
? They feel more symmetrical than initialize
and end
. or onStart
onEnd
packages/jest-worker/src/child.js
Outdated
// $FlowFixMe: This has to be a dynamic require. | ||
const main = require(file); | ||
|
||
if (!main['end']) { |
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.
main.end
packages/jest-worker/src/child.js
Outdated
return; | ||
} | ||
|
||
execFunction(main['end'], main, [], () => process.exit(0), () => {}); |
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.
main.end
packages/jest-worker/src/child.js
Outdated
ctx = main; | ||
} | ||
|
||
if (!initializeArgsForFlow || initialized || !main['initialize']) { |
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.
main.initialize
packages/jest-worker/src/child.js
Outdated
initialized = true; | ||
|
||
execFunction( | ||
main['initialize'], |
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.
main.initialize
👍 |
c6a4e52
to
2f07ac8
Compare
I've renamed the methods to |
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.
LGTM; just minor fixes required.
CHANGELOG.md
Outdated
@@ -4,6 +4,7 @@ | |||
|
|||
- `[pretty-format]` Option to not escape strings in diff messages ([#5661](https://github.com/facebook/jest/pull/5661)) | |||
- `[jest-haste-map]` Add `getFileIterator` to `HasteFS` for faster file iteration ([#7010](https://github.com/facebook/jest/pull/7010)). | |||
- `[jest-worker]` Add `initializeArgs` option to call an `initialize` method in the worker before the first call. Call `end` method in each worker when ending the farm ([#7014](https://github.com/facebook/jest/pull/7014)). |
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.
Rename to setup
and teardown
.
packages/jest-worker/src/child.js
Outdated
return; | ||
} | ||
|
||
execFunction(main.teardown, main, [], () => process.exit(0), () => {}); |
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.
Also do the process.exit(0)
for errors.
packages/jest-worker/src/child.js
Outdated
} | ||
|
||
function execFunction( | ||
fn: 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.
fn: (...args: $ReadOnlyArray<mixed>) => mixed
packages/jest-worker/src/child.js
Outdated
function execFunction( | ||
fn: Function, | ||
ctx: any, | ||
args: $ReadOnlyArray<any>, |
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.
mixed
, not any
.
packages/jest-worker/src/child.js
Outdated
fn: Function, | ||
ctx: any, | ||
args: $ReadOnlyArray<any>, | ||
onResult: (result: any) => void, |
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.
Same.
packages/jest-worker/src/child.js
Outdated
@@ -61,7 +65,7 @@ function reportSuccess(result: any) { | |||
process.send([PARENT_MESSAGE_OK, result]); | |||
} | |||
|
|||
function reportError(error: Error) { | |||
function reportError(error: Error, type?: number = PARENT_MESSAGE_ERROR) { |
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.
In line 129, you do reportError(error, PARENT_MESSAGE_INITIALIZE_ERROR)
; however PARENT_MESSAGE_INITIALIZE_ERROR
is incompatible with PARENT_MESSAGE_ERROR
.
Also, type
must not be optional.
34cec31
to
deb30be
Compare
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.
LGTM! Great job!
deb30be
to
724f8a7
Compare
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
There is no way to make
jest-worker
call aninitialize
method before calling a method in the worker for the first time.This PR adds the possibility to call an
initialize
method if it exists in the worker and aninitializeArgs
option has been passed (I've opted to not call by default this method to not make this PR a breaking change, but I can change this easily).Also, to make the behaviour consistent, I've added some logic to call an
end
method in the worker if it exists when the Worker farm is being ended.Test plan
I've created unit tests to verify the behaviour