How to use testJestWorker 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.

test.js

Source: 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
10// eslint-disable-next-line import/no-extraneous-dependencies
11const workerFarm = require('worker-farm');
12const assert = require('assert');
13const JestWorker = require('../../build').default;
14
15// Typical tests: node --expose-gc test.js empty 100000
16//                node --expose-gc test.js loadTest 10000
17assert(process.argv[2], 'Pass a child method name');
18assert(process.argv[3], 'Pass the number of iteratitons');
19
20const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
21const method = process.argv[2];
22const calls = +process.argv[3];
23const threads = 6;
24
25function testWorkerFarm() {
26  return new Promise(async (resolve, reject) => {
27    const startTime = Date.now();
28    let count = 0;
29
30    async function countToFinish() {
31      if (++count === calls) {
32        workerFarm.end(api);
33        const endTime = Date.now();
34
35        // Let all workers go down.
36        await sleep(2000);
37
38        resolve({
39          globalTime: endTime - startTime - 2000,
40          processingTime: endTime - startProcess,
41        });
42      }
43    }
44
45    const api = workerFarm(
46      {
47        autoStart: true,
48        maxConcurrentCallsPerWorker: 1,
49        maxConcurrentWorkers: threads,
50      },
51      require.resolve('./workers/worker_farm'),
52      [method],
53    );
54
55    // Let all workers come up.
56    await sleep(2000);
57
58    const startProcess = Date.now();
59
60    for (let i = 0; i < calls; i++) {
61      const promisified = new Promise((resolve, reject) => {
62        api[method]((err, result) => {
63          if (err) {
64            reject(err);
65          } else {
66            resolve(result);
67          }
68        });
69      });
70
71      promisified.then(countToFinish);
72    }
73  });
74}
75
76function testJestWorker() {
77  return new Promise(async (resolve, reject) => {
78    const startTime = Date.now();
79    let count = 0;
80
81    async function countToFinish() {
82      if (++count === calls) {
83        farm.end();
84        const endTime = Date.now();
85
86        // Let all workers go down.
87        await sleep(2000);
88
89        resolve({
90          globalTime: endTime - startTime - 2000,
91          processingTime: endTime - startProcess,
92        });
93      }
94    }
95
96    const farm = new JestWorker(require.resolve('./workers/jest_worker'), {
97      exposedMethods: [method],
98      forkOptions: {execArgv: []},
99      workers: threads,
100    });
101
102    farm.getStdout().pipe(process.stdout);
103    farm.getStderr().pipe(process.stderr);
104
105    // Let all workers come up.
106    await sleep(2000);
107
108    const startProcess = Date.now();
109
110    for (let i = 0; i < calls; i++) {
111      const promisified = farm[method]();
112
113      promisified.then(countToFinish);
114    }
115  });
116}
117
118function profile(x) {
119  console.profile(x);
120}
121
122function profileEnd(x) {
123  console.profileEnd(x);
124}
125
126async function main() {
127  if (!global.gc) {
128    console.log('GC not present');
129  }
130
131  const wFResults = [];
132  const jWResults = [];
133
134  for (let i = 0; i < 10; i++) {
135    console.log('-'.repeat(75));
136
137    profile('worker farm');
138    const wF = await testWorkerFarm();
139    profileEnd('worker farm');
140    await sleep(3000);
141    // eslint-disable-next-line no-undef
142    global.gc && gc();
143
144    profile('jest worker');
145    const jW = await testJestWorker();
146    profileEnd('jest worker');
147    await sleep(3000);
148    // eslint-disable-next-line no-undef
149    global.gc && gc();
150
151    wFResults.push(wF);
152    jWResults.push(jW);
153
154    console.log('jest-worker:', jW);
155    console.log('worker-farm:', wF);
156  }
157
158  let wFGT = 0;
159  let wFPT = 0;
160  let jWGT = 0;
161  let jWPT = 0;
162
163  for (let i = 0; i < 10; i++) {
164    wFGT += wFResults[i].globalTime;
165    wFPT += wFResults[i].processingTime;
166
167    jWGT += jWResults[i].globalTime;
168    jWPT += jWResults[i].processingTime;
169  }
170
171  console.log('-'.repeat(75));
172  console.log('total worker-farm:', {wFGT, wFPT});
173  console.log('total jest-worker:', {jWGT, jWPT});
174
175  console.log('-'.repeat(75));
176  console.log(
177    `% improvement over ${calls} calls (global time):`,
178    (100 * (wFGT - jWGT)) / wFGT,
179  );
180
181  console.log(
182    `% improvement over ${calls} calls (processing time):`,
183    (100 * (wFPT - jWPT)) / wFPT,
184  );
185}
186
187main();
188
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)