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

feat(jest-worker): add JestWorkerFarm helper type #12753

Merged
merged 3 commits into from
Apr 27, 2022
Merged

feat(jest-worker): add JestWorkerFarm helper type #12753

merged 3 commits into from
Apr 27, 2022

Conversation

mrazauskas
Copy link
Contributor

Split from #12680

Summary

While working on #12680, I came up with JestWorkerFarm helper type. It is like jest.Mocked, but for worker farms.

It is necessary, because Worker class gets extended with exposed methods, but the type of the class does not know nothing about these. So for example, JestWorkerFarm<typeof import('./someWorker')> returns the correct type for new Worker(require.resolve('./someWorker')). Picking exposed method is supported by passing an object type (in this case, the helper makes sure that users do not override reserved keys).

Useful internally. I would be happy to implement it for one of my projects too.

Test plan

Type tests added.

Comment on lines +115 to +120
export type {
ChangeEvent,
HasteMap as HasteMapObject,
IModuleMap,
SerializableModuleMap,
} from './types';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just cleaned up type exports.

@@ -148,7 +152,7 @@ export default class CoverageReporter extends BaseReporter {
forkOptions: {serialization: 'json'},
maxRetries: 2,
numWorkers: this._globalConfig.maxWorkers,
});
}) as JestWorkerFarm<CoverageWorker>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Type casting is needed for now. Will work without casting after implementing createWorkerFarm().

OnCustomMessage,
OnEnd,
OnStart,
PromiseWithCustomMessage,
QueueChildMessage,
TaskQueue,
WorkerCallback,
WorkerFarmOptions,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This rename is not a breaking change, because the type was not exposed before.

Comment on lines +21 to +23
type Promisify<T extends FunctionLike> = ReturnType<T> extends Promise<infer R>
? (...args: Parameters<T>) => Promise<R>
: (...args: Parameters<T>) => Promise<ReturnType<T>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The exposed methods always return a Promise.

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.

very nice!

@SimenB SimenB merged commit a5a27f7 into jestjs:main Apr 27, 2022
@mrazauskas mrazauskas deleted the feat-add-JestWorkerFarm-type branch April 27, 2022 06:44
@SimenB
Copy link
Member

SimenB commented Apr 27, 2022

https://github.com/facebook/jest/releases/tag/v28.0.2

F3n67u pushed a commit to F3n67u/jest that referenced this pull request May 2, 2022
@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 28, 2022
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.

3 participants