How to use workerReplyEnd method in Jest

Best JavaScript code snippet using jest

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Farm.test.js

Source: Farm.test.js Github

copy
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10import Farm from '../Farm';
11
12let mockWorkerCalls;
13let callback;
14
15function workerReplyStart(i) {
16  mockWorkerCalls[i].onStart({getWorkerId: () => mockWorkerCalls[i].workerId});
17}
18
19function workerReplyEnd(i, error, result) {
20  mockWorkerCalls[i].onEnd(error, result);
21}
22
23function workerReply(i, error, result) {
24  workerReplyStart(i);
25  workerReplyEnd(i, error, result);
26}
27
28describe('Farm', () => {
29  beforeEach(() => {
30    mockWorkerCalls = [];
31    callback = jest.fn((...args) => {
32      mockWorkerCalls.push({
33        onEnd: args[3],
34        onStart: args[2],
35        passed: args[1],
36        workerId: args[0],
37      });
38    });
39  });
40
41  it('sends a request to one worker', () => {
42    const farm = new Farm(4, callback);
43
44    farm.doWork('foo', 42);
45
46    expect(callback).toHaveBeenCalledTimes(1);
47    expect(callback).toHaveBeenCalledWith(
48      0,
49      [1, true, 'foo', [42]],
50      expect.any(Function),
51      expect.any(Function),
52    );
53  });
54
55  it('sends four requests to four unique workers', () => {
56    const farm = new Farm(4, callback);
57
58    farm.doWork('foo', 42);
59    farm.doWork('foo1', 43);
60    farm.doWork('foo2', 44);
61    farm.doWork('foo3', 45);
62
63    expect(callback).toHaveBeenCalledTimes(4);
64    expect(callback).toHaveBeenNthCalledWith(
65      1,
66      0, // first worker
67      [1, true, 'foo', [42]],
68      expect.any(Function),
69      expect.any(Function),
70    );
71    expect(callback).toHaveBeenNthCalledWith(
72      2,
73      1, // second worker
74      [1, true, 'foo1', [43]],
75      expect.any(Function),
76      expect.any(Function),
77    );
78    expect(callback).toHaveBeenNthCalledWith(
79      3,
80      2, // third worker
81      [1, true, 'foo2', [44]],
82      expect.any(Function),
83      expect.any(Function),
84    );
85    expect(callback).toHaveBeenNthCalledWith(
86      4,
87      3, // fourth worker
88      [1, true, 'foo3', [45]],
89      expect.any(Function),
90      expect.any(Function),
91    );
92  });
93
94  it('handles null computeWorkerKey, sending to first worker', async () => {
95    const computeWorkerKey = jest.fn(() => null);
96
97    const farm = new Farm(4, callback, computeWorkerKey);
98
99    const p0 = farm.doWork('foo', 42);
100    workerReply(0);
101    await p0;
102
103    expect(computeWorkerKey).toBeCalledTimes(1);
104    expect(computeWorkerKey).toHaveBeenNthCalledWith(1, 'foo', 42);
105
106    expect(callback).toHaveBeenCalledTimes(1);
107    expect(callback).toHaveBeenNthCalledWith(
108      1,
109      0, // first worker
110      [1, true, 'foo', [42]],
111      expect.any(Function),
112      expect.any(Function),
113    );
114  });
115
116  it('sends the same worker key to the same worker', async () => {
117    const computeWorkerKey = jest
118      .fn(() => {})
119      .mockReturnValueOnce('one')
120      .mockReturnValueOnce('two')
121      .mockReturnValueOnce('one');
122
123    const farm = new Farm(4, callback, computeWorkerKey);
124
125    const p0 = farm.doWork('foo', 42);
126    workerReply(0);
127    await p0;
128
129    const p1 = farm.doWork('foo1', 43);
130    workerReply(1);
131    await p1;
132
133    const p2 = farm.doWork('foo2', 44);
134    workerReply(2);
135    await p2;
136
137    expect(computeWorkerKey).toBeCalledTimes(3);
138    expect(computeWorkerKey).toHaveBeenNthCalledWith(1, 'foo', 42);
139    expect(computeWorkerKey).toHaveBeenNthCalledWith(2, 'foo1', 43);
140    expect(computeWorkerKey).toHaveBeenNthCalledWith(3, 'foo2', 44);
141
142    expect(callback).toHaveBeenCalledTimes(3);
143    expect(callback).toHaveBeenNthCalledWith(
144      1,
145      0, // first worker
146      [1, true, 'foo', [42]],
147      expect.any(Function),
148      expect.any(Function),
149    );
150    expect(callback).toHaveBeenNthCalledWith(
151      2,
152      1, // second worker
153      [1, true, 'foo1', [43]],
154      expect.any(Function),
155      expect.any(Function),
156    );
157    expect(callback).toHaveBeenNthCalledWith(
158      3,
159      0, // first worker again
160      [1, true, 'foo2', [44]],
161      expect.any(Function),
162      expect.any(Function),
163    );
164  });
165
166  it('returns the result if the call worked', async () => {
167    const farm = new Farm(4, callback);
168
169    const promise = farm.doWork('car', 'plane');
170
171    workerReply(0, null, 34);
172    const result = await promise;
173
174    expect(result).toEqual(34);
175  });
176
177  it('throws if the call failed', async () => {
178    const farm = new Farm(4, callback);
179
180    const promise = farm.doWork('car', 'plane');
181    let error = null;
182
183    workerReply(0, new TypeError('Massively broken'));
184
185    try {
186      await promise;
187    } catch (err) {
188      error = err;
189    }
190
191    expect(error).not.toBe(null);
192    expect(error).toBeInstanceOf(TypeError);
193  });
194
195  it('checks that once a sticked task finishes, next time is sent to that worker', async () => {
196    const farm = new Farm(4, callback, () => '1234567890abcdef');
197
198    // Worker 1 successfully replies with "17" as a result.
199    const p0 = farm.doWork('car', 'plane');
200    workerReply(0, null, 17);
201    await p0;
202
203    // Note that the stickiness is not created by the method name or the arguments
204    // it is solely controlled by the provided "computeWorkerKey" method, which in
205    // the test example always returns the same key, so all calls should be
206    // redirected to worker 1 (which is the one that resolved the first call).
207    const p1 = farm.doWork('foo', 'bar');
208    workerReply(1, null, 17);
209    await p1;
210
211    // The first time, a call with a "1234567890abcdef" hash had never been done
212    // earlier ("foo" call), so it got queued to all workers. Later, since the one
213    // that resolved the call was the one in position 1, all subsequent calls are
214    // only redirected to that worker.
215    expect(callback).toHaveBeenCalledTimes(2); // Only "foo".
216    expect(callback).toHaveBeenNthCalledWith(
217      1,
218      0, // first worker
219      [1, true, 'car', ['plane']],
220      expect.any(Function),
221      expect.any(Function),
222    );
223    expect(callback).toHaveBeenNthCalledWith(
224      2,
225      0, // first worker
226      [1, true, 'foo', ['bar']],
227      expect.any(Function),
228      expect.any(Function),
229    );
230  });
231
232  it('checks that even before a sticked task finishes, next time is sent to that worker', async () => {
233    const farm = new Farm(4, callback, () => '1234567890abcdef');
234
235    // Note that the worker is sending a start response synchronously.
236    const p0 = farm.doWork('car', 'plane');
237    workerReplyStart(0);
238
239    // Note that the worker is sending a start response synchronously.
240    const p1 = farm.doWork('foo', 'bar');
241
242    // The first call is sent the worker, the second is queued
243    expect(callback).toHaveBeenCalledTimes(1);
244
245    // Flush the queue
246    workerReplyEnd(0, null, 17);
247    await p0;
248    workerReply(1, null, 17);
249    await p1;
250
251    // Both requests are send to the same worker
252    // The first time, a call with a "1234567890abcdef" hash had never been done
253    // earlier ("foo" call), so it got queued to all workers. Later, since the one
254    // that resolved the call was the one in position 1, all subsequent calls are
255    // only redirected to that worker.
256    expect(callback).toHaveBeenCalledTimes(2);
257    expect(callback).toHaveBeenNthCalledWith(
258      1,
259      0, // first worker
260      [1, true, 'car', ['plane']],
261      expect.any(Function),
262      expect.any(Function),
263    );
264    expect(callback).toHaveBeenNthCalledWith(
265      2,
266      0, // first worker
267      [1, true, 'foo', ['bar']],
268      expect.any(Function),
269      expect.any(Function),
270    );
271  });
272});
273
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)