Best JavaScript code snippet using jest
PriorityQueue.test.js
Source:PriorityQueue.test.js
...11 const computePriority = (_method, task) => task.priority;12 const queue = new PriorityQueue(computePriority);13 const priorities = [10, 3, 4, 8, 2, 9, 7, 1, 2, 6, 5];14 for (const priority of priorities) {15 queue.enqueue(createQueueChildMessage({priority}));16 }17 priorities.sort((a, b) => a - b);18 for (const priority of priorities) {19 expect(queue.dequeue(0)).toEqual(20 expect.objectContaining({21 request: [CHILD_MESSAGE_CALL, false, 'test', [{priority}]],22 }),23 );24 }25 expect(queue.dequeue(0)).toBeNull();26});27it('returns the task with the lowest priority value if inserted in reversed order', () => {28 const last = createQueueChildMessage({priority: 3});29 const mid = createQueueChildMessage({priority: 2});30 const first = createQueueChildMessage({priority: 1});31 const computePriority = (_method, task) => task.priority;32 const queue = new PriorityQueue(computePriority);33 queue.enqueue(last, 1);34 queue.enqueue(first, 1);35 queue.enqueue(mid, 1);36 expect(queue.dequeue(1)).toBe(first);37 expect(queue.dequeue(1)).toBe(mid);38 expect(queue.dequeue(1)).toBe(last);39 expect(queue.dequeue(1)).toBeNull();40});41it('returns the task with the lowest priority value if inserted in correct order', () => {42 const first = createQueueChildMessage({priority: 1});43 const mid = createQueueChildMessage({priority: 2});44 const last = createQueueChildMessage({priority: 3});45 const computePriority = (_method, task) => task.priority;46 const queue = new PriorityQueue(computePriority);47 queue.enqueue(last, 1);48 queue.enqueue(first, 1);49 queue.enqueue(mid, 1);50 expect(queue.dequeue(1)).toBe(first);51 expect(queue.dequeue(1)).toBe(mid);52 expect(queue.dequeue(1)).toBe(last);53 expect(queue.dequeue(1)).toBeNull();54});55it('uses different queues for each worker', () => {56 const task1Worker1 = createQueueChildMessage({priority: 1});57 const task2Worker1 = createQueueChildMessage({priority: 3});58 const task1Worker2 = createQueueChildMessage({priority: 1});59 const task2Worker2 = createQueueChildMessage({priority: 3});60 const computePriority = (_method, task) => task.priority;61 const queue = new PriorityQueue(computePriority);62 queue.enqueue(task2Worker1, 1);63 queue.enqueue(task1Worker1, 1);64 queue.enqueue(task2Worker2, 2);65 queue.enqueue(task1Worker2, 2);66 expect(queue.dequeue(1)).toBe(task1Worker1);67 expect(queue.dequeue(1)).toBe(task2Worker1);68 expect(queue.dequeue(2)).toBe(task1Worker2);69 expect(queue.dequeue(2)).toBe(task2Worker2);70 expect(queue.dequeue(1)).toBeNull();71});72it('process task in the global and shared queue in order', () => {73 const computePriority = (_method, task) => task.priority;74 const queue = new PriorityQueue(computePriority);75 const sharedTask1 = createQueueChildMessage({priority: 1});76 const sharedTask2 = createQueueChildMessage({priority: 3});77 queue.enqueue(sharedTask1);78 queue.enqueue(sharedTask2);79 const worker1Task1 = createQueueChildMessage({priority: 0});80 const worker1Task2 = createQueueChildMessage({priority: 2});81 queue.enqueue(worker1Task1, 1);82 queue.enqueue(worker1Task2, 1);83 const worker2Task1 = createQueueChildMessage({priority: 3});84 queue.enqueue(worker2Task1, 2);85 expect(queue.dequeue(1)).toBe(worker1Task1);86 expect(queue.dequeue(1)).toBe(sharedTask1);87 expect(queue.dequeue(1)).toBe(worker1Task2);88 expect(queue.dequeue(2)).toBe(worker2Task1);89 expect(queue.dequeue(2)).toBe(sharedTask2);90 expect(queue.dequeue(1)).toBeNull();91 expect(queue.dequeue(2)).toBeNull();92});93function createQueueChildMessage(...args) {94 const request = [CHILD_MESSAGE_CALL, false, 'test', args];95 return {96 onCustomMessage: () => {},97 onEnd: () => {},98 onStart: () => {},99 request,100 };...
FifoQueue.test.js
Source:FifoQueue.test.js
...8import FifoQueue from '../FifoQueue';9import {CHILD_MESSAGE_CALL} from '../types';10it('returns the shared tasks in FIFO ordering', () => {11 const queue = new FifoQueue();12 const task1 = createQueueChildMessage();13 const task2 = createQueueChildMessage();14 const task3 = createQueueChildMessage();15 queue.enqueue(task1);16 queue.enqueue(task2);17 queue.enqueue(task3);18 expect(queue.dequeue(1)).toBe(task1);19 expect(queue.dequeue(2)).toBe(task2);20 expect(queue.dequeue(3)).toBe(task3);21 expect(queue.dequeue(1)).toBeNull();22 expect(queue.dequeue(2)).toBeNull();23 expect(queue.dequeue(3)).toBeNull();24});25it('returns the worker specific tasks in FIFO ordering', () => {26 const queue = new FifoQueue();27 const task1 = createQueueChildMessage();28 const task2 = createQueueChildMessage();29 const task3 = createQueueChildMessage();30 queue.enqueue(task1, 1);31 queue.enqueue(task2, 1);32 queue.enqueue(task3, 1);33 expect(queue.dequeue(1)).toBe(task1);34 expect(queue.dequeue(1)).toBe(task2);35 expect(queue.dequeue(1)).toBe(task3);36 expect(queue.dequeue(1)).toBeNull();37});38it('maintains global FIFO ordering between worker specific and shared tasks', () => {39 const queue = new FifoQueue();40 const sharedTask1 = createQueueChildMessage({name: 'sharedTask1'});41 const sharedTask2 = createQueueChildMessage({name: 'sharedTask2'});42 const sharedTask3 = createQueueChildMessage({name: 'sharedTask3'});43 const worker1Task1 = createQueueChildMessage({name: 'worker1Task1'});44 const worker1Task2 = createQueueChildMessage({name: 'worker1Task2'});45 const worker2Task2 = createQueueChildMessage({name: 'worker2Task1'});46 queue.enqueue(worker1Task1, 1);47 queue.enqueue(sharedTask1);48 queue.enqueue(sharedTask2);49 queue.enqueue(worker1Task2, 1);50 queue.enqueue(worker2Task2, 2);51 queue.enqueue(sharedTask3);52 expect(queue.dequeue(1)).toBe(worker1Task1);53 expect(queue.dequeue(2)).toBe(sharedTask1);54 sharedTask1.request[1] = true;55 expect(queue.dequeue(1)).toBe(sharedTask2);56 sharedTask2.request[1] = true;57 expect(queue.dequeue(1)).toBe(worker1Task2);58 expect(queue.dequeue(1)).toBe(sharedTask3);59 sharedTask3.request[1] = true;60 expect(queue.dequeue(2)).toBe(worker2Task2);61 expect(queue.dequeue(1)).toBeNull();62 expect(queue.dequeue(2)).toBeNull();63});64function createQueueChildMessage(...args) {65 const request = [CHILD_MESSAGE_CALL, false, 'test', args];66 return {67 onCustomMessage: () => {},68 onEnd: () => {},69 onStart: () => {},70 request,71 };...
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!!