Best JavaScript code snippet using jest
index.test.js
Source:index.test.js
...10let mockWorkers;11function workerReplyStart(i) {12 mockWorkers[i].send.mock.calls[0][1](mockWorkers[i]);13}14function workerReplyEnd(i, error, result) {15 mockWorkers[i].send.mock.calls[0][2](error, result);16}17function workerReply(i, error, result) {18 workerReplyStart(i);19 workerReplyEnd(i, error, result);20}21beforeEach(() => {22 mockWorkers = [];23 // The worker mock returns a worker with custom methods, plus it stores them24 // in a global list, so that they can be accessed later. This list is reset in25 // every test.26 jest.mock('../Worker', () => {27 const fakeClass = jest.fn(() => {28 const fakeWorker = {29 getStderr: () => ({once() {}, pipe() {}}),30 getStdout: () => ({once() {}, pipe() {}}),31 send: jest.fn(),32 };33 mockWorkers.push(fakeWorker);...
71index.test.js
Source:71index.test.js
...10let mockWorkers;11function workerReplyStart(i) {12 mockWorkers[i].send.mock.calls[0][1](mockWorkers[i]);13}14function workerReplyEnd(i, error, result) {15 mockWorkers[i].send.mock.calls[0][2](error, result);16}17function workerReply(i, error, result) {18 workerReplyStart(i);19 workerReplyEnd(i, error, result);20}21beforeEach(() => {22 mockWorkers = [];23 // The worker mock returns a worker with custom methods, plus it stores them24 // in a global list, so that they can be accessed later. This list is reset in25 // every test.26 jest.mock('../Worker', () => {27 const fakeClass = jest.fn(() => {28 const fakeWorker = {29 getStderr: () => ({once() {}, pipe() {}}),30 getStdout: () => ({once() {}, pipe() {}}),31 send: jest.fn(),32 };33 mockWorkers.push(fakeWorker);...
Farm.test.js
Source:Farm.test.js
...10let callback;11function workerReplyStart(i) {12 mockWorkerCalls[i].onStart({getWorkerId: () => mockWorkerCalls[i].workerId});13}14function workerReplyEnd(i, error, result) {15 mockWorkerCalls[i].onEnd(error, result);16}17function workerReply(i, error, result) {18 workerReplyStart(i);19 workerReplyEnd(i, error, result);20}21describe('Farm', () => {22 beforeEach(() => {23 mockWorkerCalls = [];24 callback = jest.fn((...args) => {25 mockWorkerCalls.push({26 onEnd: args[3],27 onStart: args[2],28 passed: args[1],29 workerId: args[0],30 });31 });32 });33 it('sends a request to one worker', () => {34 const farm = new Farm(4, callback);35 farm.doWork('foo', 42);36 expect(callback).toHaveBeenCalledTimes(1);37 expect(callback).toHaveBeenCalledWith(38 0,39 [1, true, 'foo', [42]],40 expect.any(Function),41 expect.any(Function),42 );43 });44 it('sends four requests to four unique workers', () => {45 const farm = new Farm(4, callback);46 farm.doWork('foo', 42);47 farm.doWork('foo1', 43);48 farm.doWork('foo2', 44);49 farm.doWork('foo3', 45);50 expect(callback).toHaveBeenCalledTimes(4);51 expect(callback).toHaveBeenNthCalledWith(52 1,53 0, // first worker54 [1, true, 'foo', [42]],55 expect.any(Function),56 expect.any(Function),57 );58 expect(callback).toHaveBeenNthCalledWith(59 2,60 1, // second worker61 [1, true, 'foo1', [43]],62 expect.any(Function),63 expect.any(Function),64 );65 expect(callback).toHaveBeenNthCalledWith(66 3,67 2, // third worker68 [1, true, 'foo2', [44]],69 expect.any(Function),70 expect.any(Function),71 );72 expect(callback).toHaveBeenNthCalledWith(73 4,74 3, // fourth worker75 [1, true, 'foo3', [45]],76 expect.any(Function),77 expect.any(Function),78 );79 });80 it('handles null computeWorkerKey, sending to first worker', async () => {81 const computeWorkerKey = jest.fn(() => null);82 const farm = new Farm(4, callback, computeWorkerKey);83 const p0 = farm.doWork('foo', 42);84 workerReply(0);85 await p0;86 expect(computeWorkerKey).toBeCalledTimes(1);87 expect(computeWorkerKey).toHaveBeenNthCalledWith(1, 'foo', 42);88 expect(callback).toHaveBeenCalledTimes(1);89 expect(callback).toHaveBeenNthCalledWith(90 1,91 0, // first worker92 [1, true, 'foo', [42]],93 expect.any(Function),94 expect.any(Function),95 );96 });97 it('sends the same worker key to the same worker', async () => {98 const computeWorkerKey = jest99 .fn(() => {})100 .mockReturnValueOnce('one')101 .mockReturnValueOnce('two')102 .mockReturnValueOnce('one');103 const farm = new Farm(4, callback, computeWorkerKey);104 const p0 = farm.doWork('foo', 42);105 workerReply(0);106 await p0;107 const p1 = farm.doWork('foo1', 43);108 workerReply(1);109 await p1;110 const p2 = farm.doWork('foo2', 44);111 workerReply(2);112 await p2;113 expect(computeWorkerKey).toBeCalledTimes(3);114 expect(computeWorkerKey).toHaveBeenNthCalledWith(1, 'foo', 42);115 expect(computeWorkerKey).toHaveBeenNthCalledWith(2, 'foo1', 43);116 expect(computeWorkerKey).toHaveBeenNthCalledWith(3, 'foo2', 44);117 expect(callback).toHaveBeenCalledTimes(3);118 expect(callback).toHaveBeenNthCalledWith(119 1,120 0, // first worker121 [1, true, 'foo', [42]],122 expect.any(Function),123 expect.any(Function),124 );125 expect(callback).toHaveBeenNthCalledWith(126 2,127 1, // second worker128 [1, true, 'foo1', [43]],129 expect.any(Function),130 expect.any(Function),131 );132 expect(callback).toHaveBeenNthCalledWith(133 3,134 0, // first worker again135 [1, true, 'foo2', [44]],136 expect.any(Function),137 expect.any(Function),138 );139 });140 it('returns the result if the call worked', async () => {141 const farm = new Farm(4, callback);142 const promise = farm.doWork('car', 'plane');143 workerReply(0, null, 34);144 const result = await promise;145 expect(result).toEqual(34);146 });147 it('throws if the call failed', async () => {148 const farm = new Farm(4, callback);149 const promise = farm.doWork('car', 'plane');150 let error = null;151 workerReply(0, new TypeError('Massively broken'));152 try {153 await promise;154 } catch (err) {155 error = err;156 }157 expect(error).not.toBe(null);158 expect(error).toBeInstanceOf(TypeError);159 });160 it('checks that once a sticked task finishes, next time is sent to that worker', async () => {161 const farm = new Farm(4, callback, () => '1234567890abcdef');162 // Worker 1 successfully replies with "17" as a result.163 const p0 = farm.doWork('car', 'plane');164 workerReply(0, null, 17);165 await p0;166 // Note that the stickiness is not created by the method name or the arguments167 // it is solely controlled by the provided "computeWorkerKey" method, which in168 // the test example always returns the same key, so all calls should be169 // redirected to worker 1 (which is the one that resolved the first call).170 const p1 = farm.doWork('foo', 'bar');171 workerReply(1, null, 17);172 await p1;173 // The first time, a call with a "1234567890abcdef" hash had never been done174 // earlier ("foo" call), so it got queued to all workers. Later, since the one175 // that resolved the call was the one in position 1, all subsequent calls are176 // only redirected to that worker.177 expect(callback).toHaveBeenCalledTimes(2); // Only "foo".178 expect(callback).toHaveBeenNthCalledWith(179 1,180 0, // first worker181 [1, true, 'car', ['plane']],182 expect.any(Function),183 expect.any(Function),184 );185 expect(callback).toHaveBeenNthCalledWith(186 2,187 0, // first worker188 [1, true, 'foo', ['bar']],189 expect.any(Function),190 expect.any(Function),191 );192 });193 it('checks that even before a sticked task finishes, next time is sent to that worker', async () => {194 const farm = new Farm(4, callback, () => '1234567890abcdef');195 // Note that the worker is sending a start response synchronously.196 const p0 = farm.doWork('car', 'plane');197 workerReplyStart(0);198 // Note that the worker is sending a start response synchronously.199 const p1 = farm.doWork('foo', 'bar');200 // The first call is sent the worker, the second is queued201 expect(callback).toHaveBeenCalledTimes(1);202 // Flush the queue203 workerReplyEnd(0, null, 17);204 await p0;205 workerReply(1, null, 17);206 await p1;207 // Both requests are send to the same worker208 // The first time, a call with a "1234567890abcdef" hash had never been done209 // earlier ("foo" call), so it got queued to all workers. Later, since the one210 // that resolved the call was the one in position 1, all subsequent calls are211 // only redirected to that worker.212 expect(callback).toHaveBeenCalledTimes(2);213 expect(callback).toHaveBeenNthCalledWith(214 1,215 0, // first worker216 [1, true, 'car', ['plane']],217 expect.any(Function),...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!