How to use mockWorker method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

worker-executor.test.js

Source:worker-executor.test.js Github

copy

Full Screen

1/* global expect, jest */2import createWorker from './worker-executor';3function mockWorker({ init, postMessage, terminate } = {}) {4 global.Worker = jest.fn(function() {5 setImmediate(6 (init && init(this)) ||7 (() =>8 this.onmessage && this.onmessage({ data: { type: 'contentLoaded' } }))9 );10 this.onmessage = null;11 this.postMessage =12 postMessage ||13 function(data) {14 setImmediate(15 () => this.onmessage && this.onmessage({ data: `${data} processed` })16 );17 };18 this.terminate = terminate || (() => {});19 return this;20 });21}22afterEach(() => {23 delete global.Worker;24});25it('Worker executor should successfully execute one task', async () => {26 const terminateHandler = jest.fn();27 mockWorker({ terminate: terminateHandler });28 const testWorker = createWorker('test');29 expect(testWorker).not.toBeUndefined();30 const task = testWorker.execute('test');31 expect(task).not.toBeUndefined();32 expect(task.done).not.toBeUndefined();33 const handler = jest.fn();34 task.on('done', handler);35 const errorHandler = jest.fn();36 task.on('error', errorHandler);37 await expect(task.done).resolves.toBe('test processed');38 expect(handler).toBeCalledTimes(1);39 expect(handler).toBeCalledWith('test processed');40 expect(errorHandler).not.toBeCalled();41 expect(terminateHandler).not.toBeCalled();42 expect(global.Worker).toBeCalledTimes(1);43 expect(global.Worker).toBeCalledWith('/js/test.js');44});45// eslint-disable-next-line max-len46it('Worker executor should successfully execute two tasks in parallel', async () => {47 const terminateHandler = jest.fn();48 mockWorker({ terminate: terminateHandler });49 const testWorker = createWorker('test');50 const task1 = testWorker.execute('test1');51 const handler1 = jest.fn();52 task1.on('done', handler1);53 const errorHandler1 = jest.fn();54 task1.on('error', errorHandler1);55 const task2 = testWorker.execute('test2');56 const handler2 = jest.fn();57 task2.on('done', handler2);58 const errorHandler2 = jest.fn();59 task2.on('error', errorHandler2);60 await expect(Promise.all([task1.done, task2.done])).resolves.toEqual([61 'test1 processed',62 'test2 processed'63 ]);64 expect(handler1).toBeCalledTimes(1);65 expect(handler1).toBeCalledWith('test1 processed');66 expect(errorHandler1).not.toBeCalled();67 expect(handler2).toBeCalledTimes(1);68 expect(handler2).toBeCalledWith('test2 processed');69 expect(errorHandler2).not.toBeCalled();70 expect(terminateHandler).not.toBeCalled();71 expect(global.Worker).toBeCalledTimes(2);72});73// eslint-disable-next-line max-len74it('Worker executor should successfully execute 3 tasks in parallel and use two workers', async () => {75 mockWorker();76 const testWorker = createWorker('test');77 const task1 = testWorker.execute('test1');78 const task2 = testWorker.execute('test2');79 const task3 = testWorker.execute('test3');80 await expect(81 Promise.all([task1.done, task2.done, task3.done])82 ).resolves.toEqual(['test1 processed', 'test2 processed', 'test3 processed']);83 expect(global.Worker).toBeCalledTimes(2);84});85// eslint-disable-next-line max-len86it('Worker executor should successfully execute 3 tasks, use 3 workers and terminate each worker', async () => {87 const terminateHandler = jest.fn();88 mockWorker({ terminate: terminateHandler });89 const testWorker = createWorker('test', { terminateWorker: true });90 const task1 = testWorker.execute('test1');91 const task2 = testWorker.execute('test2');92 const task3 = testWorker.execute('test3');93 await expect(94 Promise.all([task1.done, task2.done, task3.done])95 ).resolves.toEqual(['test1 processed', 'test2 processed', 'test3 processed']);96 expect(terminateHandler).toBeCalledTimes(3);97 expect(global.Worker).toBeCalledTimes(3);98});99// eslint-disable-next-line max-len100it('Worker executor should successfully execute 3 tasks in parallel and use 3 workers', async () => {101 mockWorker();102 const testWorker = createWorker('test', { concurrency: 3 });103 const task1 = testWorker.execute('test1');104 const task2 = testWorker.execute('test2');105 const task3 = testWorker.execute('test3');106 await expect(107 Promise.all([task1.done, task2.done, task3.done])108 ).resolves.toEqual(['test1 processed', 'test2 processed', 'test3 processed']);109 expect(global.Worker).toBeCalledTimes(3);110});111// eslint-disable-next-line max-len112it('Worker executor should successfully execute 3 tasks and use 1 worker', async () => {113 mockWorker();114 const testWorker = createWorker('test', { concurrency: 1 });115 const task1 = testWorker.execute('test1');116 const task2 = testWorker.execute('test2');117 const task3 = testWorker.execute('test3');118 await expect(119 Promise.all([task1.done, task2.done, task3.done])120 ).resolves.toEqual(['test1 processed', 'test2 processed', 'test3 processed']);121 expect(global.Worker).toBeCalledTimes(1);122});123it('Worker executor should reject task', async () => {124 const error = { message: 'Error on init worker' };125 mockWorker({126 init: () => {127 throw error;128 }129 });130 const testWorker = createWorker('test');131 const task = testWorker.execute('test');132 const errorHandler = jest.fn();133 task.on('error', errorHandler);134 await expect(task.done).rejects.toBe(error.message);135 expect(errorHandler).toBeCalledTimes(1);136 expect(errorHandler).toBeCalledWith(error);137});138it('Worker executor should emit LOG events', async () => {139 mockWorker({140 postMessage: function(data) {141 setImmediate(() => {142 for (let i = 0; i < 3; i++) {143 // eslint-disable-next-line no-unused-expressions144 this.onmessage && this.onmessage({ data: { type: 'LOG', data: i } });145 }146 // eslint-disable-next-line no-unused-expressions147 this.onmessage && this.onmessage({ data: `${data} processed` });148 setImmediate(149 () =>150 this.onmessage && this.onmessage({ data: { type: 'LOG', data: 3 } })151 );152 });153 }154 });155 const testWorker = createWorker('test');156 const task = testWorker.execute('test');157 const handler = jest.fn();158 task.on('done', handler);159 const errorHandler = jest.fn();160 task.on('error', errorHandler);161 const logHandler = jest.fn();162 task.on('LOG', logHandler);163 await expect(task.done).resolves.toBe('test processed');164 expect(handler).toBeCalledTimes(1);165 expect(handler).toBeCalledWith('test processed');166 expect(errorHandler).not.toBeCalled();167 expect(logHandler).toBeCalledTimes(3);168 for (let i = 0; i < 3; i++) {169 expect(logHandler.mock.calls[i][0]).toBe(i);170 }171});172it('Worker executor should reject task on timeout', async () => {173 const terminateHandler = jest.fn();174 mockWorker({175 postMessage: () => {},176 terminate: terminateHandler177 });178 const testWorker = createWorker('test');179 const task = testWorker.execute('test', 0);180 const errorHandler = jest.fn();181 task.on('error', errorHandler);182 await expect(task.done).rejects.toBe('timeout');183 expect(errorHandler).toBeCalledTimes(1);184 expect(errorHandler.mock.calls[0][0]).toEqual({ message: 'timeout' });185 expect(terminateHandler).toBeCalledTimes(1);186});187it('Worker executor should get worker from specified location', async () => {188 mockWorker();189 const testWorker = createWorker('test', { location: '/other/location/' });190 const task = testWorker.execute('test');191 await expect(task.done).resolves.toBe('test processed');192 expect(global.Worker).toBeCalledTimes(1);193 expect(global.Worker).toBeCalledWith('/other/location/test.js');...

Full Screen

Full Screen

search.service.spec.ts

Source:search.service.spec.ts Github

copy

Full Screen

1import { ReflectiveInjector, NgZone } from '@angular/core';2import { fakeAsync, tick } from '@angular/core/testing';3import { of } from 'rxjs';4import { SearchService } from './search.service';5import { WebWorkerClient } from 'app/shared/web-worker';6describe('SearchService', () => {7 let injector: ReflectiveInjector;8 let service: SearchService;9 let sendMessageSpy: jasmine.Spy;10 let mockWorker: WebWorkerClient;11 beforeEach(() => {12 sendMessageSpy = jasmine.createSpy('sendMessage').and.returnValue(of({}));13 mockWorker = { sendMessage: sendMessageSpy } as any;14 spyOn(WebWorkerClient, 'create').and.returnValue(mockWorker);15 injector = ReflectiveInjector.resolveAndCreate([16 SearchService,17 { provide: NgZone, useFactory: () => new NgZone({ enableLongStackTrace: false }) }18 ]);19 service = injector.get(SearchService);20 });21 describe('initWorker', () => {22 it('should create the worker and load the index after the specified delay', fakeAsync(() => {23 service.initWorker('some/url', 100);24 expect(WebWorkerClient.create).not.toHaveBeenCalled();25 expect(mockWorker.sendMessage).not.toHaveBeenCalled();26 tick(100);27 expect(WebWorkerClient.create).toHaveBeenCalledWith('some/url', jasmine.any(NgZone));28 expect(mockWorker.sendMessage).toHaveBeenCalledWith('load-index');29 }));30 });31 describe('search', () => {32 beforeEach(() => {33 // We must initialize the service before calling connectSearches34 service.initWorker('some/url', 1000);35 // Simulate the index being ready so that searches get sent to the worker36 (service as any).ready = of(true);37 });38 it('should trigger a `loadIndex` synchronously (not waiting for the delay)', () => {39 expect(mockWorker.sendMessage).not.toHaveBeenCalled();40 service.search('some query').subscribe();41 expect(mockWorker.sendMessage).toHaveBeenCalledWith('load-index');42 });43 it('should send a "query-index" message to the worker', () => {44 service.search('some query').subscribe();45 expect(mockWorker.sendMessage).toHaveBeenCalledWith('query-index', 'some query');46 });47 it('should push the response to the returned observable', () => {48 const mockSearchResults = { results: ['a', 'b'] };49 let actualSearchResults: any;50 (mockWorker.sendMessage as jasmine.Spy).and.returnValue(of(mockSearchResults));51 service.search('some query').subscribe(results => actualSearchResults = results);52 expect(actualSearchResults).toEqual(mockSearchResults);53 });54 });...

Full Screen

Full Screen

initWorkers.test.ts

Source:initWorkers.test.ts Github

copy

Full Screen

1import initWorkers from "./initWorkers";2import { Worker } from "./types";3jest.useFakeTimers();4jest.spyOn(global, "setInterval");5describe("initWorkers", () => {6 async function sleep(ms: number) {7 return new Promise((resolve) => {8 setTimeout(resolve, ms);9 });10 }11 test("run", () => {12 const mockWorker: Worker = {13 async execute() {},14 };15 jest.spyOn(mockWorker, "execute");16 initWorkers([mockWorker]);17 expect(mockWorker.execute).toHaveBeenCalledTimes(1);18 jest.runOnlyPendingTimers();19 expect(mockWorker.execute).toHaveBeenCalledTimes(2);20 });21 test("run after recover from error", () => {22 const mockWorker: Worker = {23 async execute() {24 await sleep(20);25 throw "error";26 },27 };28 jest.spyOn(mockWorker, "execute");29 initWorkers([mockWorker]);30 expect(mockWorker.execute).toHaveBeenCalledTimes(1);31 jest.runOnlyPendingTimers();32 expect(mockWorker.execute).toHaveBeenCalledTimes(2);33 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { mockWorker } = require("fast-check-monorepo");2const { add } = require("./add");3describe("add", () => {4 it("should add two numbers", () => {5 mockWorker(add, 1, 2).then(result => {6 expect(result).toEqual(3);7 });8 });9});10const add = (a, b) => a + b;11module.exports = {12};13{14 "scripts": {15 },16 "devDependencies": {17 }18}19module.exports = {20};21{22}23{24 "env": {25 }26}27{28}

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const mockWorker = require('fast-check-monorepo').mockWorker;3const arb = fc.integer(0, 10);4const mock = mockWorker(arb);5mock.run(1000).then((result) => {6 console.log(result);7});8const fc = require('fast-check');9const mockWorker = require('fast-check-monorepo').mockWorker;10describe('test', () => {11 it('should work', () => {12 const arb = fc.integer(0, 10);13 const mock = mockWorker(arb);14 return mock.run(1000).then((result) => {15 console.log(result);16 });17 });18});19import * as fc from 'fast-check';20import { mockWorker } from 'fast-check-monorepo';21describe('test', () => {22 it('should work', () => {23 const arb = fc.integer(0, 10);24 const mock = mockWorker(arb);25 return mock.run(1000).then((result) => {26 console.log(result);27 });28 });29});30import * as fc from 'fast-check';31import { mockWorker } from 'fast-check-monorepo';32const arb = fc.integer(0, 10);33const mock = mockWorker(arb);34mock.run(1000).then((result) => {35 console.log(result);36});37import * as fc from 'fast-check';38import { mockWorker } from 'fast-check-monorepo';39const arb = fc.integer(0, 10);40const mock = mockWorker(arb);41mock.run(1000).then((result) => {42 console.log(result);43});44import * as fc from 'fast-check';45import { mockWorker } from 'fast-check-monorepo';46const arb = fc.integer(0, 10);47const mock = mockWorker(arb);48mock.run(1000).then((result) => {49 console.log(result);50});51import * as fc from 'fast-check';52import { mockWorker } from 'fast-check-monorepo';53const arb = fc.integer(0, 10);54const mock = mockWorker(arb);55mock.run(1000).then((result) => {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { mockWorker, runWorker } = require("fast-check-monorepo");2const run = async () => {3 const worker = await mockWorker();4 worker.terminate();5};6run();7const { runWorker } = require("fast-check-monorepo");8const run = async () => {9 const worker = await runWorker();10 worker.terminate();11};12run();13const { runWorker } = require("fast-check");14const run = async () => {15 const worker = await runWorker();16 worker.terminate();17};18run();19const { mockWorker } = require("fast-check");20const run = async () => {21 const worker = await mockWorker();22 worker.terminate();23};24run();25const { runWorker } = require("fast-check");26const run = async () => {27 const worker = await runWorker();28 worker.terminate();29};30run();31const { mockWorker } = require("fast-check");32const run = async () => {33 const worker = await mockWorker();34 worker.terminate();35};36run();37const { runWorker } = require("fast-check");38const run = async () => {39 const worker = await runWorker();40 worker.terminate();41};42run();43const { mockWorker } = require("fast-check");44const run = async () => {45 const worker = await mockWorker();46 worker.terminate();47};48run();49const { runWorker } = require("fast-check");50const run = async () => {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { mockWorker } = require('fast-check');2const { worker } = require('./worker');3const mock = mockWorker(worker);4mock('Hello').then((result) => {5console.log('result', result);6});7const worker = (input) => {8return new Promise((resolve, reject) => {9setTimeout(() => {10resolve(input);11}, 1000);12});13};14module.exports = { worker };15How to import mockWorker method?16How to import worker method?17How to import worker method?18How to import mockWorker method?19How to import worker method?20How to import worker method?21How to import mockWorker method?

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const mockWorker = require('fast-check-monorepo').mockWorker;3const mockWorkerFunction = mockWorker(function (a) {4 return a + 1;5});6const result = mockWorkerFunction(1);7const fc = require('fast-check');8const mockWorker = require('fast-check-monorepo').mockWorker;9const mockWorkerFunction = mockWorker(function (a) {10 return a + 1;11});12const result = mockWorkerFunction(1);13const fc = require('fast-check');14const mockWorker = require('fast-check-monorepo').mockWorker;15const mockWorkerFunction = mockWorker(function (a) {16 return a + 1;17});18const result = mockWorkerFunction(1);19const fc = require('fast-check');20const mockWorker = require('fast-check-monorepo').mockWorker;21const mockWorkerFunction = mockWorker(function (a) {22 return a + 1;23});24const result = mockWorkerFunction(1);25const fc = require('fast-check');26const mockWorker = require('fast-check-monorepo').mockWorker;

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fast-check-monorepo 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