Skip to content

Commit 201b833

Browse files
authored
refactor(jest-worker): convert part of test into TypeScript (#13441)
1 parent dea530f commit 201b833

File tree

7 files changed

+266
-190
lines changed

7 files changed

+266
-190
lines changed

packages/jest-worker/src/__tests__/Farm.test.js renamed to packages/jest-worker/src/__tests__/Farm.test.ts

+51-34
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,59 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
'use strict';
9-
108
import Farm from '../Farm';
11-
12-
let mockWorkerCalls;
13-
let callback;
14-
15-
function workerReplyStart(i) {
16-
mockWorkerCalls[i].onStart({getWorkerId: () => mockWorkerCalls[i].workerId});
9+
import type {
10+
ChildMessage,
11+
OnCustomMessage,
12+
OnEnd,
13+
OnStart,
14+
WorkerCallback,
15+
WorkerInterface,
16+
} from '../types';
17+
18+
let mockWorkerCalls: Array<{
19+
onCustomMessage: OnCustomMessage;
20+
onEnd: OnEnd;
21+
onStart: OnStart;
22+
passed: ChildMessage;
23+
workerId: number;
24+
}>;
25+
26+
let callback: WorkerCallback;
27+
28+
function workerReplyStart(i: number) {
29+
mockWorkerCalls[i].onStart({
30+
getWorkerId: () => mockWorkerCalls[i].workerId,
31+
} as WorkerInterface);
1732
}
1833

19-
function workerReplyEnd(i, error, result) {
34+
function workerReplyEnd(i: number, error: Error | null, result?: unknown) {
2035
mockWorkerCalls[i].onEnd(error, result);
2136
}
2237

23-
function workerReply(i, error, result) {
38+
function workerReply(i: number, error: Error | null = null, result?: unknown) {
2439
workerReplyStart(i);
2540
workerReplyEnd(i, error, result);
2641
}
2742

28-
function workerReplyCustomMessage(i, message) {
43+
function workerReplyCustomMessage(i: number, message: unknown) {
2944
mockWorkerCalls[i].onCustomMessage(message);
3045
}
3146

3247
describe('Farm', () => {
3348
beforeEach(() => {
3449
mockWorkerCalls = [];
35-
callback = jest.fn((...args) => {
36-
mockWorkerCalls.push({
37-
onCustomMessage: args[4],
38-
onEnd: args[3],
39-
onStart: args[2],
40-
passed: args[1],
41-
workerId: args[0],
42-
});
43-
});
50+
callback = jest.fn<WorkerCallback>(
51+
(workerId, request, onStart, onEnd, onCustomMessage) => {
52+
mockWorkerCalls.push({
53+
onCustomMessage,
54+
onEnd,
55+
onStart,
56+
passed: request,
57+
workerId,
58+
});
59+
},
60+
);
4461
});
4562

4663
it('sends a request to one worker', () => {
@@ -127,7 +144,7 @@ describe('Farm', () => {
127144

128145
it('sends the same worker key to the same worker', async () => {
129146
const computeWorkerKey = jest
130-
.fn(() => {})
147+
.fn<() => string>()
131148
.mockReturnValueOnce('one')
132149
.mockReturnValueOnce('two')
133150
.mockReturnValueOnce('one');
@@ -296,23 +313,23 @@ describe('Farm', () => {
296313

297314
it('checks that locking works, and jobs are never lost', async () => {
298315
const hash = jest
299-
.fn()
316+
.fn<() => string>()
300317
// This will go to both queues, but picked by the first worker.
301-
.mockReturnValueOnce(0)
318+
.mockReturnValueOnce('0')
302319
// This will go to both queues too, but picked by the second worker.
303-
.mockReturnValueOnce(1)
304-
// This will go to worker 0, now only assigned to it.
305-
.mockReturnValueOnce(0)
306-
// This will go to worker 1, now only assigned to it.
307-
.mockReturnValueOnce(1)
320+
.mockReturnValueOnce('1')
321+
// This will go to worker '0', now only assigned to it.
322+
.mockReturnValueOnce('0')
323+
// This will go to worker '1', now only assigned to it.
324+
.mockReturnValueOnce('1')
308325
// This will go to both queues too, but will wait, since workers are busy.
309-
.mockReturnValueOnce(2)
326+
.mockReturnValueOnce('2')
310327
// This will only go to the first queue.
311-
.mockReturnValueOnce(0)
328+
.mockReturnValueOnce('0')
312329
// This will be gone if the queue implementation is wrong.
313-
.mockReturnValueOnce(0)
330+
.mockReturnValueOnce('0')
314331
// Push onto the second queue; potentially wiping the earlier job.
315-
.mockReturnValueOnce(1);
332+
.mockReturnValueOnce('1');
316333

317334
const farm = new Farm(2, callback, {computeWorkerKey: hash});
318335

@@ -364,11 +381,11 @@ describe('Farm', () => {
364381
const p0 = farm.doWork('work-0');
365382
const p1 = farm.doWork('work-1');
366383

367-
const unsubscribe = p0.UNSTABLE_onCustomMessage(message => {
384+
const unsubscribe = p0.UNSTABLE_onCustomMessage!(message => {
368385
expect(message).toEqual({key: 0, message: 'foo'});
369386
});
370387

371-
p1.UNSTABLE_onCustomMessage(message => {
388+
p1.UNSTABLE_onCustomMessage!(message => {
372389
expect(message).toEqual({key: 1, message: 'bar'});
373390
});
374391

packages/jest-worker/src/__tests__/FifoQueue.test.js renamed to packages/jest-worker/src/__tests__/FifoQueue.test.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
'use strict';
9-
108
import FifoQueue from '../FifoQueue';
11-
import {CHILD_MESSAGE_CALL} from '../types';
9+
import {
10+
CHILD_MESSAGE_CALL,
11+
ChildMessageCall,
12+
QueueChildMessage,
13+
} from '../types';
1214

1315
it('returns the shared tasks in FIFO ordering', () => {
1416
const queue = new FifoQueue();
@@ -82,8 +84,8 @@ it('maintains global FIFO ordering between worker specific and shared tasks', ()
8284
expect(queue.dequeue(2)).toBeNull();
8385
});
8486

85-
function createQueueChildMessage(...args) {
86-
const request = [CHILD_MESSAGE_CALL, false, 'test', args];
87+
function createQueueChildMessage(...args: Array<unknown>): QueueChildMessage {
88+
const request: ChildMessageCall = [CHILD_MESSAGE_CALL, false, 'test', args];
8789

8890
return {
8991
onCustomMessage: () => {},

packages/jest-worker/src/__tests__/PriorityQueue.test.js renamed to packages/jest-worker/src/__tests__/PriorityQueue.test.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
'use strict';
9-
108
import PriorityQueue from '../PriorityQueue';
11-
import {CHILD_MESSAGE_CALL} from '../types';
9+
import {
10+
CHILD_MESSAGE_CALL,
11+
ChildMessageCall,
12+
QueueChildMessage,
13+
} from '../types';
1214

1315
it('returns the tasks in order', () => {
14-
const computePriority = (_method, task) => task.priority;
16+
const computePriority = (_method: string, task: unknown) =>
17+
(task as {priority: number}).priority;
1518
const queue = new PriorityQueue(computePriority);
1619
const priorities = [10, 3, 4, 8, 2, 9, 7, 1, 2, 6, 5];
1720

@@ -36,7 +39,8 @@ it('returns the task with the lowest priority value if inserted in reversed orde
3639
const mid = createQueueChildMessage({priority: 2});
3740
const first = createQueueChildMessage({priority: 1});
3841

39-
const computePriority = (_method, task) => task.priority;
42+
const computePriority = (_method: string, task: unknown) =>
43+
(task as {priority: number}).priority;
4044
const queue = new PriorityQueue(computePriority);
4145

4246
queue.enqueue(last, 1);
@@ -54,7 +58,8 @@ it('returns the task with the lowest priority value if inserted in correct order
5458
const mid = createQueueChildMessage({priority: 2});
5559
const last = createQueueChildMessage({priority: 3});
5660

57-
const computePriority = (_method, task) => task.priority;
61+
const computePriority = (_method: string, task: unknown) =>
62+
(task as {priority: number}).priority;
5863
const queue = new PriorityQueue(computePriority);
5964

6065
queue.enqueue(last, 1);
@@ -73,7 +78,8 @@ it('uses different queues for each worker', () => {
7378
const task1Worker2 = createQueueChildMessage({priority: 1});
7479
const task2Worker2 = createQueueChildMessage({priority: 3});
7580

76-
const computePriority = (_method, task) => task.priority;
81+
const computePriority = (_method: string, task: unknown) =>
82+
(task as {priority: number}).priority;
7783
const queue = new PriorityQueue(computePriority);
7884

7985
queue.enqueue(task2Worker1, 1);
@@ -89,7 +95,8 @@ it('uses different queues for each worker', () => {
8995
});
9096

9197
it('process task in the global and shared queue in order', () => {
92-
const computePriority = (_method, task) => task.priority;
98+
const computePriority = (_method: string, task: unknown) =>
99+
(task as {priority: number}).priority;
93100
const queue = new PriorityQueue(computePriority);
94101

95102
const sharedTask1 = createQueueChildMessage({priority: 1});
@@ -116,8 +123,8 @@ it('process task in the global and shared queue in order', () => {
116123
expect(queue.dequeue(2)).toBeNull();
117124
});
118125

119-
function createQueueChildMessage(...args) {
120-
const request = [CHILD_MESSAGE_CALL, false, 'test', args];
126+
function createQueueChildMessage(...args: Array<unknown>): QueueChildMessage {
127+
const request: ChildMessageCall = [CHILD_MESSAGE_CALL, false, 'test', args];
121128

122129
return {
123130
onCustomMessage: () => {},

packages/jest-worker/src/__tests__/WorkerPool.test.js renamed to packages/jest-worker/src/__tests__/WorkerPool.test.ts

+27-20
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import WorkerPool from '../WorkerPool';
9+
import type {ChildMessage, WorkerPoolOptions} from '../types';
910
import ChildProcessWorker from '../workers/ChildProcessWorker';
1011
import NodeThreadWorker from '../workers/NodeThreadsWorker';
1112

@@ -37,8 +38,8 @@ jest.mock('../workers/NodeThreadsWorker', () => {
3738

3839
describe('WorkerPool', () => {
3940
beforeEach(() => {
40-
ChildProcessWorker.mockClear();
41-
NodeThreadWorker.mockClear();
41+
jest.mocked(ChildProcessWorker).mockClear();
42+
jest.mocked(NodeThreadWorker).mockClear();
4243
});
4344

4445
it('should create a ChildProcessWorker and send to it', () => {
@@ -49,13 +50,14 @@ describe('WorkerPool', () => {
4950
forkOptions: {},
5051
maxRetries: 1,
5152
numWorkers: 1,
52-
workerId: 0,
53-
workerPath: '/path',
54-
});
53+
} as WorkerPoolOptions);
5554

55+
const request = {foo: 'bar'} as unknown as ChildMessage;
5656
const onStart = () => {};
5757
const onEnd = () => {};
58-
workerPool.send(0, {foo: 'bar'}, onStart, onEnd);
58+
const onCustomMessage = () => {};
59+
60+
workerPool.send(0, request, onStart, onEnd, onCustomMessage);
5961

6062
expect(ChildProcessWorker).toHaveBeenCalledWith({
6163
forkOptions: {},
@@ -64,11 +66,12 @@ describe('WorkerPool', () => {
6466
workerPath: '/path',
6567
});
6668
expect(NodeThreadWorker).not.toHaveBeenCalled();
69+
// @ts-expect-error: Testing internal method
6770
expect(workerPool._workers[0].send).toHaveBeenCalledWith(
68-
{foo: 'bar'},
71+
request,
6972
onStart,
7073
onEnd,
71-
undefined,
74+
onCustomMessage,
7275
);
7376
});
7477

@@ -79,13 +82,14 @@ describe('WorkerPool', () => {
7982
forkOptions: {},
8083
maxRetries: 1,
8184
numWorkers: 1,
82-
workerId: 0,
83-
workerPath: '/path',
84-
});
85+
} as WorkerPoolOptions);
8586

87+
const request = {foo: 'bar'} as unknown as ChildMessage;
8688
const onStart = () => {};
8789
const onEnd = () => {};
88-
workerPool.send(0, {foo: 'bar'}, onStart, onEnd);
90+
const onCustomMessage = () => {};
91+
92+
workerPool.send(0, request, onStart, onEnd, onCustomMessage);
8993

9094
expect(NodeThreadWorker).toHaveBeenCalledWith({
9195
forkOptions: {},
@@ -94,11 +98,12 @@ describe('WorkerPool', () => {
9498
workerPath: '/path',
9599
});
96100
expect(ChildProcessWorker).not.toHaveBeenCalled();
101+
// @ts-expect-error: Testing internal method
97102
expect(workerPool._workers[0].send).toHaveBeenCalledWith(
98-
{foo: 'bar'},
103+
request,
99104
onStart,
100105
onEnd,
101-
undefined,
106+
onCustomMessage,
102107
);
103108
});
104109

@@ -108,13 +113,14 @@ describe('WorkerPool', () => {
108113
forkOptions: {},
109114
maxRetries: 1,
110115
numWorkers: 1,
111-
workerId: 0,
112-
workerPath: '/path',
113-
});
116+
} as WorkerPoolOptions);
114117

118+
const request = {foo: 'bar'} as unknown as ChildMessage;
115119
const onStart = () => {};
116120
const onEnd = () => {};
117-
workerPool.send(0, {foo: 'bar'}, onStart, onEnd);
121+
const onCustomMessage = () => {};
122+
123+
workerPool.send(0, request, onStart, onEnd, onCustomMessage);
118124

119125
expect(ChildProcessWorker).toHaveBeenCalledWith({
120126
forkOptions: {},
@@ -123,11 +129,12 @@ describe('WorkerPool', () => {
123129
workerPath: '/path',
124130
});
125131
expect(NodeThreadWorker).not.toHaveBeenCalled();
132+
// @ts-expect-error: Testing internal method
126133
expect(workerPool._workers[0].send).toHaveBeenCalledWith(
127-
{foo: 'bar'},
134+
request,
128135
onStart,
129136
onEnd,
130-
undefined,
137+
onCustomMessage,
131138
);
132139
});
133140
});

0 commit comments

Comments
 (0)