How to use assertCallsToChild method in Jest

Best JavaScript code snippet using jest

thread-integration.test.js

Source:thread-integration.test.js Github

copy

Full Screen

...16}17function replySuccess(i, result) {18 mockForkedProcesses[i].emit('message', [PARENT_MESSAGE_OK, result]);19}20function assertCallsToChild(childNum, ...calls) {21 expect(mockForkedProcesses[childNum].postMessage).toHaveBeenCalledTimes(22 calls.length + 1,23 );24 calls.forEach(([methodName, ...args], numCall) => {25 expect(26 mockForkedProcesses[childNum].postMessage.mock.calls[numCall + 1][0],27 ).toEqual([CHILD_MESSAGE_CALL, true, methodName, args]);28 });29}30describe('Jest Worker Process Integration', () => {31 beforeEach(() => {32 mockForkedProcesses = [];33 jest.mock('worker_threads', () => {34 const fakeClass = jest.fn(() => {35 const forkedProcess = mockBuildForkedProcess();36 mockForkedProcesses.push(forkedProcess);37 return forkedProcess;38 });39 return {40 Worker: fakeClass,41 __esModule: true,42 };43 });44 Farm = require('../index').Worker;45 });46 afterEach(() => {47 jest.resetModules();48 });49 it('calls a single method from the worker', async () => {50 const farm = new Farm('/tmp/baz.js', {51 enableWorkerThreads: true,52 exposedMethods: ['foo', 'bar'],53 numWorkers: 4,54 });55 const promise = farm.foo();56 replySuccess(0, 42);57 expect(await promise).toBe(42);58 });59 it('distributes sequential calls across child processes', async () => {60 const farm = new Farm('/tmp/baz.js', {61 enableWorkerThreads: true,62 exposedMethods: ['foo', 'bar'],63 numWorkers: 4,64 });65 // The first call will go to the first child process.66 const promise0 = farm.foo('param-0');67 assertCallsToChild(0, ['foo', 'param-0']);68 replySuccess(0, 'worker-0');69 expect(await promise0).toBe('worker-0');70 // The second call will go to the second child process.71 const promise1 = farm.foo(1);72 assertCallsToChild(1, ['foo', 1]);73 replySuccess(1, 'worker-1');74 expect(await promise1).toBe('worker-1');75 });76 it('schedules the task on the first available child processes if the scheduling policy is in-order', async () => {77 const farm = new Farm('/tmp/baz.js', {78 enableWorkerThreads: true,79 exposedMethods: ['foo', 'bar'],80 numWorkers: 4,81 workerSchedulingPolicy: 'in-order',82 });83 // The first call will go to the first child process.84 const promise0 = farm.foo('param-0');85 assertCallsToChild(0, ['foo', 'param-0']);86 // The second call will go to the second child process.87 const promise1 = farm.foo(1);88 // The first task on worker 0 completes89 replySuccess(0, 'worker-0');90 expect(await promise0).toBe('worker-0');91 // The second task on worker 1 completes92 assertCallsToChild(1, ['foo', 1]);93 replySuccess(1, 'worker-1');94 expect(await promise1).toBe('worker-1');95 // The third call will go to the first child process96 const promise2 = farm.foo('param-2');97 assertCallsToChild(0, ['foo', 'param-0'], ['foo', 'param-2']);98 replySuccess(0, 'worker-0');99 expect(await promise2).toBe('worker-0');100 });101 it('schedules the task on the first available child processes', async () => {102 const farm = new Farm('/tmp/baz.js', {103 enableWorkerThreads: true,104 exposedMethods: ['foo', 'bar'],105 numWorkers: 4,106 });107 // The first call will go to the first child process.108 const promise0 = farm.foo('param-0');109 assertCallsToChild(0, ['foo', 'param-0']);110 replySuccess(0, 'worker-0');111 expect(await promise0).toBe('worker-0');112 // The second call will go to the second child process.113 const promise1 = farm.foo(1);114 assertCallsToChild(1, ['foo', 1]);115 replySuccess(1, 'worker-1');116 expect(await promise1).toBe('worker-1');117 });118 it('distributes concurrent calls across child processes', async () => {119 const farm = new Farm('/tmp/baz.js', {120 enableWorkerThreads: true,121 exposedMethods: ['foo', 'bar'],122 numWorkers: 4,123 });124 // Do 3 calls to the farm in parallel.125 const promise0 = farm.foo('param-0');126 const promise1 = farm.foo('param-1');127 const promise2 = farm.foo('param-2');128 // Check that the method calls are sent to each separate child process.129 assertCallsToChild(0, ['foo', 'param-0']);130 assertCallsToChild(1, ['foo', 'param-1']);131 assertCallsToChild(2, ['foo', 'param-2']);132 // Send different responses from each child.133 replySuccess(0, 'worker-0');134 replySuccess(1, 'worker-1');135 replySuccess(2, 'worker-2');136 // Check137 expect(await promise0).toBe('worker-0');138 expect(await promise1).toBe('worker-1');139 expect(await promise2).toBe('worker-2');140 });141 it('sticks parallel calls to children', async () => {142 const farm = new Farm('/tmp/baz.js', {143 computeWorkerKey: () => '1234567890abcdef',144 enableWorkerThreads: true,145 exposedMethods: ['foo', 'bar'],146 numWorkers: 4,147 });148 // Do 3 calls to the farm in parallel.149 const promise0 = farm.foo('param-0');150 const promise1 = farm.foo('param-1');151 const promise2 = farm.foo('param-2');152 // Send different responses for each call (from the same child).153 replySuccess(0, 'worker-0');154 replySuccess(0, 'worker-1');155 replySuccess(0, 'worker-2');156 // Check that all the calls have been received by the same child).157 assertCallsToChild(158 0,159 ['foo', 'param-0'],160 ['foo', 'param-1'],161 ['foo', 'param-2'],162 );163 // Check that responses are correct.164 expect(await promise0).toBe('worker-0');165 expect(await promise1).toBe('worker-1');166 expect(await promise2).toBe('worker-2');167 });...

Full Screen

Full Screen

process-integration.test.js

Source:process-integration.test.js Github

copy

Full Screen

...16}17function replySuccess(i, result) {18 mockForkedProcesses[i].emit('message', [PARENT_MESSAGE_OK, result]);19}20function assertCallsToChild(childNum, ...calls) {21 expect(mockForkedProcesses[childNum].send).toHaveBeenCalledTimes(22 calls.length + 1,23 );24 calls.forEach(([methodName, ...args], numCall) => {25 expect(26 mockForkedProcesses[childNum].send.mock.calls[numCall + 1][0],27 ).toEqual([CHILD_MESSAGE_CALL, true, methodName, args]);28 });29}30jest.mock('worker_threads', () => {31 throw Error('Unsupported');32});33describe('Jest Worker Integration', () => {34 beforeEach(() => {35 mockForkedProcesses = [];36 jest.mock('child_process', () => ({37 fork() {38 const forkedProcess = mockBuildForkedProcess();39 mockForkedProcesses.push(forkedProcess);40 return forkedProcess;41 },42 }));43 Farm = require('../index').Worker;44 });45 afterEach(() => {46 jest.resetModules();47 });48 it('calls a single method from the worker', async () => {49 const farm = new Farm('/tmp/baz.js', {50 exposedMethods: ['foo', 'bar'],51 numWorkers: 4,52 });53 const promise = farm.foo();54 replySuccess(0, 42);55 expect(await promise).toBe(42);56 });57 it('distributes sequential calls across child processes', async () => {58 const farm = new Farm('/tmp/baz.js', {59 exposedMethods: ['foo', 'bar'],60 numWorkers: 4,61 });62 // The first call will go to the first child process.63 const promise0 = farm.foo('param-0');64 assertCallsToChild(0, ['foo', 'param-0']);65 replySuccess(0, 'worker-0');66 expect(await promise0).toBe('worker-0');67 // The second call will go to the second child process.68 const promise1 = farm.foo(1);69 assertCallsToChild(1, ['foo', 1]);70 replySuccess(1, 'worker-1');71 expect(await promise1).toBe('worker-1');72 });73 it('schedules the task on the first available child processes if the scheduling policy is in-order', async () => {74 const farm = new Farm('/tmp/baz.js', {75 exposedMethods: ['foo', 'bar'],76 numWorkers: 4,77 workerSchedulingPolicy: 'in-order',78 });79 // The first call will go to the first child process.80 const promise0 = farm.foo('param-0');81 assertCallsToChild(0, ['foo', 'param-0']);82 // The second call will go to the second child process.83 const promise1 = farm.foo(1);84 // The first task on worker 0 completes85 replySuccess(0, 'worker-0');86 expect(await promise0).toBe('worker-0');87 // The second task on worker 1 completes88 assertCallsToChild(1, ['foo', 1]);89 replySuccess(1, 'worker-1');90 expect(await promise1).toBe('worker-1');91 // The third call will go to the first child process92 const promise2 = farm.foo('param-2');93 assertCallsToChild(0, ['foo', 'param-0'], ['foo', 'param-2']);94 replySuccess(0, 'worker-0');95 expect(await promise2).toBe('worker-0');96 });97 it('distributes concurrent calls across child processes', async () => {98 const farm = new Farm('/tmp/baz.js', {99 exposedMethods: ['foo', 'bar'],100 numWorkers: 4,101 });102 // Do 3 calls to the farm in parallel.103 const promise0 = farm.foo('param-0');104 const promise1 = farm.foo('param-1');105 const promise2 = farm.foo('param-2');106 // Check that the method calls are sent to each separate child process.107 assertCallsToChild(0, ['foo', 'param-0']);108 assertCallsToChild(1, ['foo', 'param-1']);109 assertCallsToChild(2, ['foo', 'param-2']);110 // Send different responses from each child.111 replySuccess(0, 'worker-0');112 replySuccess(1, 'worker-1');113 replySuccess(2, 'worker-2');114 // Check115 expect(await promise0).toBe('worker-0');116 expect(await promise1).toBe('worker-1');117 expect(await promise2).toBe('worker-2');118 });119 it('sticks parallel calls to children', async () => {120 const farm = new Farm('/tmp/baz.js', {121 computeWorkerKey: () => '1234567890abcdef',122 exposedMethods: ['foo', 'bar'],123 numWorkers: 4,124 });125 // Do 3 calls to the farm in parallel.126 const promise0 = farm.foo('param-0');127 const promise1 = farm.foo('param-1');128 const promise2 = farm.foo('param-2');129 // Send different responses for each call (from the same child).130 replySuccess(0, 'worker-0');131 replySuccess(0, 'worker-1');132 replySuccess(0, 'worker-2');133 // Check that all the calls have been received by the same child).134 assertCallsToChild(135 0,136 ['foo', 'param-0'],137 ['foo', 'param-1'],138 ['foo', 'param-2'],139 );140 // Check that responses are correct.141 expect(await promise0).toBe('worker-0');142 expect(await promise1).toBe('worker-1');143 expect(await promise2).toBe('worker-2');144 });...

Full Screen

Full Screen

index-integration.test.js

Source:index-integration.test.js Github

copy

Full Screen

...16}17function replySuccess(i, result) {18 mockForkedProcesses[i].emit('message', [PARENT_MESSAGE_OK, result]);19}20function assertCallsToChild(childNum, ...calls) {21 expect(mockForkedProcesses[childNum].send).toHaveBeenCalledTimes(22 calls.length + 1,23 );24 calls.forEach(([methodName, ...args], numCall) => {25 expect(26 mockForkedProcesses[childNum].send.mock.calls[numCall + 1][0],27 ).toEqual([CHILD_MESSAGE_CALL, true, methodName, args]);28 });29}30beforeEach(() => {31 mockForkedProcesses = [];32 jest.mock('child_process', () => ({33 fork() {34 const forkedProcess = mockBuildForkedProcess();35 mockForkedProcesses.push(forkedProcess);36 return forkedProcess;37 },38 }));39 Farm = require('../index').default;40});41afterEach(() => {42 jest.resetModules();43});44it('calls a single method from the worker', async () => {45 const farm = new Farm('/tmp/baz.js', {46 exposedMethods: ['foo', 'bar'],47 numWorkers: 4,48 });49 const promise = farm.foo();50 replySuccess(0, 42);51 expect(await promise).toBe(42);52});53it('distributes sequential calls across child processes', async () => {54 const farm = new Farm('/tmp/baz.js', {55 exposedMethods: ['foo', 'bar'],56 numWorkers: 4,57 });58 // The first call will go to the first child process.59 const promise0 = farm.foo('param-0');60 assertCallsToChild(0, ['foo', 'param-0']);61 replySuccess(0, 'worker-0');62 expect(await promise0).toBe('worker-0');63 // The second call will go to the second child process.64 const promise1 = farm.foo(1);65 assertCallsToChild(1, ['foo', 1]);66 replySuccess(1, 'worker-1');67 expect(await promise1).toBe('worker-1');68});69it('distributes concurrent calls across child processes', async () => {70 const farm = new Farm('/tmp/baz.js', {71 exposedMethods: ['foo', 'bar'],72 numWorkers: 4,73 });74 // Do 3 calls to the farm in parallel.75 const promise0 = farm.foo('param-0');76 const promise1 = farm.foo('param-1');77 const promise2 = farm.foo('param-2');78 // Check that the method calls are sent to each separate child process.79 assertCallsToChild(0, ['foo', 'param-0']);80 assertCallsToChild(1, ['foo', 'param-1']);81 assertCallsToChild(2, ['foo', 'param-2']);82 // Send different responses from each child.83 replySuccess(0, 'worker-0');84 replySuccess(1, 'worker-1');85 replySuccess(2, 'worker-2');86 // Check87 expect(await promise0).toBe('worker-0');88 expect(await promise1).toBe('worker-1');89 expect(await promise2).toBe('worker-2');90});91it('sticks parallel calls to children', async () => {92 const farm = new Farm('/tmp/baz.js', {93 computeWorkerKey: () => '1234567890abcdef',94 exposedMethods: ['foo', 'bar'],95 numWorkers: 4,96 });97 // Do 3 calls to the farm in parallel.98 const promise0 = farm.foo('param-0');99 const promise1 = farm.foo('param-1');100 const promise2 = farm.foo('param-2');101 // Send different responses for each call (from the same child).102 replySuccess(0, 'worker-0');103 replySuccess(0, 'worker-1');104 replySuccess(0, 'worker-2');105 // Check that all the calls have been received by the same child).106 assertCallsToChild(107 0,108 ['foo', 'param-0'],109 ['foo', 'param-1'],110 ['foo', 'param-2'],111 );112 // Check that responses are correct.113 expect(await promise0).toBe('worker-0');114 expect(await promise1).toBe('worker-1');115 expect(await promise2).toBe('worker-2');...

Full Screen

Full Screen

Jest Testing Tutorial

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.

Chapters

  1. What is Jest Framework
  2. Advantages of Jest - Jest has 3,898,000 GitHub repositories, as mentioned on its official website. Learn what makes Jest special and why Jest has gained popularity among the testing and developer community.
  3. Jest Installation - All the prerequisites and set up steps needed to help you start Jest automation testing.
  4. Using Jest with NodeJS Project - Learn how to leverage Jest framework to automate testing using a NodeJS Project.
  5. Writing First Test for Jest Framework - Get started with code-based tutorial to help you write and execute your first Jest framework testing script.
  6. Jest Vocabulary - Learn the industry renowned and official jargons of the Jest framework by digging deep into the Jest vocabulary.
  7. Unit Testing with Jest - Step-by-step tutorial to help you execute unit testing with Jest framework.
  8. Jest Basics - Learn about the most pivotal and basic features which makes Jest special.
  9. Jest Parameterized Tests - Avoid code duplication and fasten automation testing with Jest using parameterized tests. Parameterization allows you to trigger the same test scenario over different test configurations by incorporating parameters.
  10. Jest Matchers - Enforce assertions better with the help of matchers. Matchers help you compare the actual output with the expected one. Here is an example to see if the object is acquired from the correct class or not. -

|<p>it('check_object_of_Car', () => {</p><p> expect(newCar()).toBeInstanceOf(Car);</p><p> });</p>| | :- |

  1. Jest Hooks: Setup and Teardown - Learn how to set up conditions which needs to be followed by the test execution and incorporate a tear down function to free resources after the execution is complete.
  2. Jest Code Coverage - Unsure there is no code left unchecked in your application. Jest gives a specific flag called --coverage to help you generate code coverage.
  3. HTML Report Generation - Learn how to create a comprehensive HTML report based on your Jest test execution.
  4. Testing React app using Jest Framework - Learn how to test your react web-application with Jest framework in this detailed Jest tutorial.
  5. Test using LambdaTest cloud Selenium Grid - Run your Jest testing script over LambdaTest cloud-based platform and leverage parallel testing to help trim down your test execution time.
  6. Snapshot Testing for React Front Ends - Capture screenshots of your react based web-application and compare them automatically for visual anomalies with the help of Jest tutorial.
  7. Bonus: Import ES modules with Jest - ES modules are also known as ECMAScript modules. Learn how to best use them by importing in your Jest testing scripts.
  8. Jest vs Mocha vs Jasmine - Learn the key differences between the most popular JavaScript-based testing frameworks i.e. Jest, Mocha, and Jasmine.
  9. Jest FAQs(Frequently Asked Questions) - Explore the most commonly asked questions around Jest framework, with their answers.

Run Jest automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful