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

chore: add worker build on CI #7637

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions jest.config.ci.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ export default {
'jest-junit',
{outputDirectory: 'reports/junit', outputName: 'js-test-results.xml'},
],
[
'jest-silent-reporter',
{showPaths: true, showWarnings: true, useDots: true},
],
'default',
'summary',
],
};
2 changes: 1 addition & 1 deletion packages/jest-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function hello(param) {

Node shipped with [`worker_threads`](https://nodejs.org/api/worker_threads.html), a "threading API" that uses `SharedArrayBuffers` to communicate between the main process and its child threads. This feature can significantly improve the communication time between parent and child processes in `jest-worker`.

To use `worker_threads` instead of default `child_process` you have to pass `enableWorkerThreads: true` when instantiating the worker.
To disable the default `worker_threads` and instead use `child_process` you have to pass `enableWorkerThreads: false` when instantiating the worker.

## API

Expand Down
6 changes: 4 additions & 2 deletions packages/jest-worker/src/WorkerPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {
WorkerOptions,
WorkerPoolInterface,
} from './types';
import ChildProcessWorker from './workers/ChildProcessWorker';
import NodeThreadsWorker from './workers/NodeThreadsWorker';

class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface {
send(
Expand All @@ -30,9 +32,9 @@ class WorkerPool extends BaseWorkerPool implements WorkerPoolInterface {
override createWorker(workerOptions: WorkerOptions): WorkerInterface {
let Worker;
if (this._options.enableWorkerThreads) {
Worker = require('./workers/NodeThreadsWorker').default;
Worker = NodeThreadsWorker;
} else {
Worker = require('./workers/ChildProcessWorker').default;
Worker = ChildProcessWorker;
}

return new Worker(workerOptions);
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-worker/src/__tests__/WorkerPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('WorkerPool', () => {
it('should create a NodeThreadWorker and send to it', () => {
jest.mock('worker_threads', () => 'Defined');
const workerPool = new WorkerPool('/path', {
enableWorkerThreads: true,
forkOptions: {},
maxRetries: 1,
numWorkers: 1,
Expand Down Expand Up @@ -107,9 +106,10 @@ describe('WorkerPool', () => {
);
});

it('should avoid NodeThreadWorker if not passed enableWorkerThreads', () => {
it('should use ChildProcessWorker if passed enableWorkerThreads: false', () => {
jest.mock('worker_threads', () => 'Defined');
const workerPool = new WorkerPool('/path', {
enableWorkerThreads: false,
forkOptions: {},
maxRetries: 1,
numWorkers: 1,
Expand Down
4 changes: 0 additions & 4 deletions packages/jest-worker/src/__tests__/thread-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ describe('Jest Worker Process Integration', () => {

it('calls a single method from the worker', async () => {
const farm = new WorkerFarm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
}) as JestWorkerFarm<{foo(): void}>;
Expand All @@ -81,7 +80,6 @@ describe('Jest Worker Process Integration', () => {

it('distributes sequential calls across child processes', async () => {
const farm = new WorkerFarm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
}) as JestWorkerFarm<{foo(a: unknown): void}>;
Expand Down Expand Up @@ -152,7 +150,6 @@ describe('Jest Worker Process Integration', () => {

it('distributes concurrent calls across child processes', async () => {
const farm = new WorkerFarm('/tmp/baz.js', {
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
}) as JestWorkerFarm<{foo(a: unknown): void}>;
Expand Down Expand Up @@ -181,7 +178,6 @@ describe('Jest Worker Process Integration', () => {
it('sticks parallel calls to children', async () => {
const farm = new WorkerFarm('/tmp/baz.js', {
computeWorkerKey: () => '1234567890abcdef',
enableWorkerThreads: true,
exposedMethods: ['foo', 'bar'],
numWorkers: 4,
}) as JestWorkerFarm<{foo(a: unknown): void}>;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class Worker {
}

const workerPoolOptions: WorkerPoolOptions = {
enableWorkerThreads: this._options.enableWorkerThreads ?? false,
enableWorkerThreads: this._options.enableWorkerThreads ?? true,
forkOptions: this._options.forkOptions ?? {},
idleMemoryLimit: this._options.idleMemoryLimit,
maxRetries: this._options.maxRetries ?? 3,
Expand Down