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

Add support for workers to send custom messages to parent in jest-worker #9662

Closed

Conversation

rogeliog
Copy link
Contributor

@rogeliog rogeliog commented Mar 12, 2020

Summary

This PR part of an effort to break down PR #9001, so that we can eventually have #6616

It adds jest-worker the ability for workers to send "custom messages" to their parent while they are still running. This will eventually allow us to report per test case updates to the reporter.

Test plan

This should not break the API, and I added multiple tests for it. I should also add an e2e test for it, but that is still pending.

I intentionally kept it undocumented given that I'm still not 100% sure that this is the right interface. Currently it uses a PromiseWithCustomMessage interface, that allows to call p.onCustomMessage(listener) to a thenable.

export interface PromiseWithCustomMessage<T> extends Promise<T> {
  onCustomMessage?: (listener: OnCustomMessage) => () => void;
}

I would love feedback on that interface or potential new ideas for it.

@rogeliog rogeliog changed the title Jest worker support custom messages jest-worker add support for custom message to parent Mar 12, 2020
@rogeliog rogeliog changed the title jest-worker add support for custom message to parent Add support for workers to send custom messages to parent in jest-worker Mar 12, 2020
@rogeliog rogeliog requested review from SimenB, jeysal and rickhanlonii and removed request for SimenB and jeysal March 12, 2020 17:16
@SimenB
Copy link
Member

SimenB commented Apr 16, 2020

🙀 I missed this PR @rogeliog, sorry!

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

This LGTM, thanks @rogeliog! Sorry about missing it 😬 Could you rebase when adressing the feedback? I think some TS changes are needed after #9693

PARENT_MESSAGE_OK,
} from '../../types';
import expectExport from 'expect';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import expectExport from 'expect';

try {
const {isMainThread, parentPort} = require('worker_threads');
return !isMainThread && parentPort;
} catch (_) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} catch (_) {
} catch {

but what's this guard for? The fact the module is missing in node 8? If so, would this code path ever be called - wouldn't earlier guards catch it?

@@ -228,6 +234,8 @@ export default class ChildProcessWorker implements WorkerInterface {
return onProcessEnd(...args);
};

this._onCustomMessage = (...arg) => onCustomMessage(...arg);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
this._onCustomMessage = (...arg) => onCustomMessage(...arg);
this._onCustomMessage = onCustomMessage;

or is it somehow different?

@@ -56,6 +59,10 @@ export type PoolExitResult = {
forceExited: boolean;
};

export interface PromiseWithCustomMessage<T> extends Promise<T> {
onCustomMessage?: (listener: OnCustomMessage) => () => void;
Copy link
Member

Choose a reason for hiding this comment

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

this feels slightly wonky, but if it works I'm all for it. maybe

Suggested change
onCustomMessage?: (listener: OnCustomMessage) => () => void;
unstable_onCustomMessage?: (listener: OnCustomMessage) => () => void;

just so people know not to use it if they find it? Leaves us with some wiggle-room

@@ -19,6 +19,7 @@ export const CHILD_MESSAGE_END: 2 = 2;
export const PARENT_MESSAGE_OK: 0 = 0;
export const PARENT_MESSAGE_CLIENT_ERROR: 1 = 1;
export const PARENT_MESSAGE_SETUP_ERROR: 2 = 2;
export const PARENT_MESSAGE_CUSTOM: -1 = -1;
Copy link
Member

Choose a reason for hiding this comment

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

is it cleaner with as const? maybe not

};
};

const onCustomMessage: OnCustomMessage = (message: any) => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const onCustomMessage: OnCustomMessage = (message: any) => {
const onCustomMessage: OnCustomMessage = message => {

OnCustomMessage should make sure this can be inferred, no?

@jeysal
Copy link
Contributor

jeysal commented May 25, 2020

I can't give large PRs like this a proper review atm, only just managed to get back below ~30 Jest notifications after months. But if there are parts around how workers exit, which I've dealt with somewhat recently, happy to take a look at specifics.

@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 11, 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.

4 participants