Best JavaScript code snippet using fast-check-monorepo
SchedulerArbitrary.spec.ts
Source:SchedulerArbitrary.spec.ts
...20 const fakeScheduler = {} as SchedulerImplemMock.SchedulerImplem<unknown>;21 const SchedulerImplem = jest.spyOn(SchedulerImplemMock, 'SchedulerImplem');22 SchedulerImplem.mockReturnValue(fakeScheduler);23 // Act24 const arb = new SchedulerArbitrary(act);25 const g = arb.generate(mrng, undefined);26 // Assert27 expect(g.value).toBe(fakeScheduler);28 expect(SchedulerImplem).toHaveBeenCalledTimes(1);29 expect(SchedulerImplem).toHaveBeenCalledWith(30 act,31 expect.objectContaining({ clone: expect.any(Function), nextTaskIndex: expect.any(Function) })32 );33 expect(clone).toHaveBeenCalledTimes(1);34 expect(clone1).toHaveBeenCalledTimes(1);35 });36 it('should build a taskScheduler pulling random values out of the cloned instance of Random', () => {37 // Arrange38 const act = jest.fn();39 const scheduledTasks = [{}, {}, {}, {}, {}, {}, {}, {}] as ScheduledTask<unknown>[];40 const { instance: mrng, clone } = fakeRandom();41 const { instance: mrng1, clone: clone1, nextInt } = fakeRandom();42 const { instance: mrng2 } = fakeRandom();43 clone.mockReturnValueOnce(mrng1);44 clone1.mockReturnValueOnce(mrng2);45 const fakeScheduler = {} as SchedulerImplemMock.SchedulerImplem<unknown>;46 const SchedulerImplem = jest.spyOn(SchedulerImplemMock, 'SchedulerImplem');47 SchedulerImplem.mockReturnValue(fakeScheduler);48 const arb = new SchedulerArbitrary(act);49 arb.generate(mrng, undefined);50 // Act51 const taskScheduler = SchedulerImplem.mock.calls[0][1];52 taskScheduler.nextTaskIndex(scheduledTasks);53 // Assert54 expect(nextInt).toHaveBeenCalledTimes(1);55 expect(nextInt).toHaveBeenCalledWith(0, scheduledTasks.length - 1);56 });57 it('should build a taskScheduler that can be cloned and create the same values', () => {58 // Arrange59 const act = jest.fn();60 const scheduledTasks = [{}, {}, {}, {}, {}, {}, {}, {}] as ScheduledTask<unknown>[];61 const { instance: mrng, clone } = fakeRandom();62 const { instance: mrng1, clone: clone1, nextInt } = fakeRandom();63 const { instance: mrng2, clone: clone2, nextInt: nextIntBis } = fakeRandom();64 const { instance: mrng3 } = fakeRandom();65 clone.mockReturnValueOnce(mrng1);66 clone1.mockImplementationOnce(() => {67 expect(nextInt).not.toHaveBeenCalled(); // if we pulled values clone is not a clone of the source68 return mrng2;69 });70 clone2.mockImplementationOnce(() => {71 expect(nextIntBis).not.toHaveBeenCalled(); // if we pulled values clone is not a clone of the source72 return mrng3;73 });74 nextInt.mockReturnValueOnce(5).mockReturnValueOnce(2);75 nextIntBis.mockReturnValueOnce(5).mockReturnValueOnce(2);76 const fakeScheduler = {} as SchedulerImplemMock.SchedulerImplem<unknown>;77 const SchedulerImplem = jest.spyOn(SchedulerImplemMock, 'SchedulerImplem');78 SchedulerImplem.mockReturnValue(fakeScheduler);79 const arb = new SchedulerArbitrary(act);80 arb.generate(mrng, undefined);81 // Act82 const taskScheduler = SchedulerImplem.mock.calls[0][1];83 const v1 = taskScheduler.nextTaskIndex(scheduledTasks);84 const v2 = taskScheduler.nextTaskIndex(scheduledTasks);85 const taskScheduler2 = taskScheduler.clone();86 const u1 = taskScheduler2.nextTaskIndex(scheduledTasks);87 const u2 = taskScheduler2.nextTaskIndex(scheduledTasks);88 // Assert89 expect(nextInt).toHaveBeenCalledTimes(2);90 expect(nextInt).toHaveBeenCalledWith(0, scheduledTasks.length - 1);91 expect(nextIntBis).toHaveBeenCalledTimes(2);92 expect(nextIntBis).toHaveBeenCalledWith(0, scheduledTasks.length - 1);93 expect(v1).toBe(u1);94 expect(v2).toBe(u2);95 });96 });97 describe('canShrinkWithoutContext', () => {98 it('should return false for any Scheduler received without any context (even for SchedulerImplem)', () => {99 // Arrange100 const act = jest.fn();101 // Act102 const arb = new SchedulerArbitrary(act);103 const out = arb.canShrinkWithoutContext(104 new SchedulerImplemMock.SchedulerImplem(act, { clone: jest.fn(), nextTaskIndex: jest.fn() })105 );106 // Assert107 expect(out).toBe(false);108 });109 it('should return false even for its own values', () => {110 // Arrange111 const act = jest.fn();112 const { instance: mrng, clone } = fakeRandom();113 const { instance: mrng1, clone: clone1 } = fakeRandom();114 const { instance: mrng2 } = fakeRandom();115 clone.mockReturnValueOnce(mrng1);116 clone1.mockReturnValueOnce(mrng2);117 // Act118 const arb = new SchedulerArbitrary(act);119 const g = arb.generate(mrng, undefined);120 const out = arb.canShrinkWithoutContext(g.value);121 // Assert122 expect(out).toBe(false);123 });124 });125 describe('shrink', () => {126 it('should always shrink to nil', () => {127 // Arrange128 const act = jest.fn();129 const { instance: mrng, clone } = fakeRandom();130 const { instance: mrng1, clone: clone1 } = fakeRandom();131 const { instance: mrng2 } = fakeRandom();132 clone.mockReturnValueOnce(mrng1);133 clone1.mockReturnValueOnce(mrng2);134 // Act135 const arb = new SchedulerArbitrary(act);136 const { value, context } = arb.generate(mrng, undefined);137 const shrinks = [...arb.shrink(value, context)];138 // Assert139 expect(shrinks).toHaveLength(0);140 });141 });...
scheduler.spec.ts
Source:scheduler.spec.ts
1import { scheduler, Scheduler, schedulerFor } from '../../../src/arbitrary/scheduler';2import { fakeArbitrary } from './__test-helpers__/ArbitraryHelpers';3import * as BuildSchedulerForMock from '../../../src/arbitrary/_internals/helpers/BuildSchedulerFor';4import * as SchedulerArbitraryMock from '../../../src/arbitrary/_internals/SchedulerArbitrary';5function beforeEachHook() {6 jest.resetModules();7 jest.restoreAllMocks();8}9beforeEach(beforeEachHook);10describe('scheduler', () => {11 it('should instantiate a SchedulerArbitrary with defaulted act (if not provided)', () => {12 // Arrange13 const { instance } = fakeArbitrary<Scheduler<unknown>>();14 const SchedulerArbitrary = jest.spyOn(SchedulerArbitraryMock, 'SchedulerArbitrary');15 SchedulerArbitrary.mockReturnValue(instance as SchedulerArbitraryMock.SchedulerArbitrary<unknown>);16 // Act17 const s = scheduler();18 // Assert19 expect(s).toBe(instance);20 expect(SchedulerArbitrary).toHaveBeenCalledWith(expect.any(Function));21 });22 it('should pass a defaulted act that calls the received function', () => {23 // Arrange24 const { instance } = fakeArbitrary<Scheduler<unknown>>();25 const SchedulerArbitrary = jest.spyOn(SchedulerArbitraryMock, 'SchedulerArbitrary');26 SchedulerArbitrary.mockReturnValue(instance as SchedulerArbitraryMock.SchedulerArbitrary<unknown>);27 const outF = new Promise<unknown>(() => {});28 const f = jest.fn().mockReturnValue(outF);29 // Act30 scheduler();31 const receivedAct = SchedulerArbitrary.mock.calls[0][0];32 const out = receivedAct(f);33 // Assert34 expect(f).toHaveBeenCalledTimes(1);35 expect(out).toBe(outF);36 });37 it('should instantiate a SchedulerArbitrary with received act', () => {38 // Arrange39 const { instance } = fakeArbitrary<Scheduler<unknown>>();40 const SchedulerArbitrary = jest.spyOn(SchedulerArbitraryMock, 'SchedulerArbitrary');41 SchedulerArbitrary.mockReturnValue(instance as SchedulerArbitraryMock.SchedulerArbitrary<unknown>);42 const act = () => Promise.resolve();43 // Act44 const s = scheduler({ act });45 // Assert46 expect(s).toBe(instance);47 expect(SchedulerArbitrary).toHaveBeenCalledWith(act);48 });49});50describe('schedulerFor', () => {51 it('should build proper scheduler when using template string from error logs', async () => {52 // Arrange53 const instance = {} as Scheduler<unknown>;54 const buildSchedulerFor = jest.spyOn(BuildSchedulerForMock, 'buildSchedulerFor');55 buildSchedulerFor.mockReturnValue(instance);56 // Act57 const s = schedulerFor()`58 -> [task${3}] promise rejected with value "pz"59 -> [task${1}] promise resolved with value "px"60 -> [task${2}] promise rejected with value "py"`;61 // Assert62 expect(s).toBe(instance);63 expect(buildSchedulerFor).toHaveBeenCalledWith(expect.any(Function), [3, 1, 2]);64 });65 it('should build proper scheduler when using custom template string', async () => {66 // Arrange67 const instance = {} as Scheduler<unknown>;68 const buildSchedulerFor = jest.spyOn(BuildSchedulerForMock, 'buildSchedulerFor');69 buildSchedulerFor.mockReturnValue(instance);70 // Act71 const s = schedulerFor()`72 This scheduler will resolve task ${2} first73 followed by ${3} and only then task ${1}`;74 // Assert75 expect(s).toBe(instance);76 expect(buildSchedulerFor).toHaveBeenCalledWith(expect.any(Function), [2, 3, 1]);77 });78 it('should build proper scheduler when using ordering array', async () => {79 // Arrange80 const instance = {} as Scheduler<unknown>;81 const buildSchedulerFor = jest.spyOn(BuildSchedulerForMock, 'buildSchedulerFor');82 buildSchedulerFor.mockReturnValue(instance);83 // Act84 const s = schedulerFor([2, 3, 1]);85 // Assert86 expect(s).toBe(instance);87 expect(buildSchedulerFor).toHaveBeenCalledWith(expect.any(Function), [2, 3, 1]);88 });...
scheduler.ts
Source:scheduler.ts
1import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';2import { Scheduler } from './_internals/interfaces/Scheduler';3import { buildSchedulerFor } from './_internals/helpers/BuildSchedulerFor';4import { SchedulerArbitrary } from './_internals/SchedulerArbitrary';5export { Scheduler, SchedulerReportItem, SchedulerSequenceItem } from './_internals/interfaces/Scheduler';6/**7 * Constraints to be applied on {@link scheduler}8 * @remarks Since 2.2.09 * @public10 */11export interface SchedulerConstraints {12 /**13 * Ensure that all scheduled tasks will be executed in the right context (for instance it can be the `act` of React)14 * @remarks Since 1.21.015 */16 act: (f: () => Promise<void>) => Promise<unknown>;17}18/**19 * For scheduler of promises20 * @remarks Since 1.20.021 * @public22 */23export function scheduler<TMetaData = unknown>(constraints?: SchedulerConstraints): Arbitrary<Scheduler<TMetaData>> {24 const { act = (f: () => Promise<void>) => f() } = constraints || {};25 return new SchedulerArbitrary<TMetaData>(act);26}27/**28 * For custom scheduler with predefined resolution order29 *30 * Ordering is defined by using a template string like the one generated in case of failure of a {@link scheduler}31 *32 * It may be something like:33 *34 * @example35 * ```typescript36 * fc.schedulerFor()`37 * -> [task\${2}] promise pending38 * -> [task\${3}] promise pending39 * -> [task\${1}] promise pending40 * `41 * ```42 *43 * Or more generally:44 * ```typescript45 * fc.schedulerFor()`46 * This scheduler will resolve task ${2} first47 * followed by ${3} and only then task ${1}48 * `49 * ```50 *51 * WARNING:52 * Custom scheduler will53 * neither check that all the referred promises have been scheduled54 * nor that they resolved with the same status and value.55 *56 *57 * WARNING:58 * If one the promises is wrongly defined it will fail - for instance asking to resolve 5 while 5 does not exist.59 *60 * @remarks Since 1.25.061 * @public62 */63function schedulerFor<TMetaData = unknown>(64 constraints?: SchedulerConstraints65): (_strs: TemplateStringsArray, ...ordering: number[]) => Scheduler<TMetaData>;66/**67 * For custom scheduler with predefined resolution order68 *69 * WARNING:70 * Custom scheduler will not check that all the referred promises have been scheduled.71 *72 *73 * WARNING:74 * If one the promises is wrongly defined it will fail - for instance asking to resolve 5 while 5 does not exist.75 *76 * @param customOrdering - Array defining in which order the promises will be resolved.77 * Id of the promises start at 1. 1 means first scheduled promise, 2 second scheduled promise and so on.78 *79 * @remarks Since 1.25.080 * @public81 */82function schedulerFor<TMetaData = unknown>(83 customOrdering: number[],84 constraints?: SchedulerConstraints85): Scheduler<TMetaData>;86function schedulerFor<TMetaData = unknown>(87 customOrderingOrConstraints: number[] | SchedulerConstraints | undefined,88 constraintsOrUndefined?: SchedulerConstraints89): Scheduler<TMetaData> | ((_strs: TemplateStringsArray, ...ordering: number[]) => Scheduler<TMetaData>) {90 // Extract passed constraints91 const { act = (f: () => Promise<void>) => f() } = Array.isArray(customOrderingOrConstraints)92 ? constraintsOrUndefined || {}93 : customOrderingOrConstraints || {};94 if (Array.isArray(customOrderingOrConstraints)) {95 return buildSchedulerFor(act, customOrderingOrConstraints);96 }97 return function (_strs: TemplateStringsArray, ...ordering: number[]) {98 return buildSchedulerFor(act, ordering);99 };100}...
Using AI Code Generation
1const { SchedulerArbitrary } = require('fast-check/lib/arbitrary/scheduler/SchedulerArbitrary');2const { Scheduler } = require('fast-check/lib/arbitrary/scheduler/Scheduler');3const scheduler = SchedulerArbitrary().generate(mrng(0)).value;4console.log(scheduler);5const scheduler2 = new Scheduler();6console.log(scheduler2);7I have a project that uses fast-check, and I want to use the SchedulerArbitrary() method, but I get the following error:8I have imported the SchedulerArbitrary method as shown above, but it still does not work. I have also tried importing it in the following way:9import { SchedulerArbitrary } from 'fast-check/lib/arbitrary/scheduler/SchedulerArbitrary';10I have also tried importing it in the following way:11import { SchedulerArbitrary } from 'fast-check';12I have also tried importing it in the following way:13import { SchedulerArbitrary } from 'fast-check/lib';14I have also tried importing it in the following way:15import { SchedulerArbitrary } from 'fast-check/lib/arbitrary';16I have also tried importing it in the following way:17import { SchedulerArbitrary } from 'fast-check/lib/arbitrary/scheduler';18I have also tried importing it in the following way:19import { SchedulerArbitrary } from 'fast-check/lib/arbitrary/scheduler/Scheduler';20I have also tried importing it in the following way:21import { SchedulerArbitrary } from 'fast-check/lib/arbitrary/scheduler/SchedulerArbitrary';22I have also tried importing it in the following way:23import { SchedulerArbitrary } from 'fast-check/lib/arbitrary/scheduler/SchedulerArbitrary';24I have also tried importing it in the following way:25import { SchedulerArbitrary } from 'fast-check/lib/arbitrary/scheduler
Using AI Code Generation
1const SchedulerArbitrary = require('fast-check/lib/types/scheduler/SchedulerArbitrary').SchedulerArbitrary;2const Scheduler = require('fast-check/lib/types/scheduler/Scheduler').Scheduler;3const { configureGlobal } = require('fast-check/lib/check/runner/configuration/GlobalParameters').configureGlobal;4const scheduler = SchedulerArbitrary().generate(0).value;5const s = new Scheduler(scheduler);6s.schedule(0, () => console.log('first'));7s.schedule(1, () => console.log('second'));8s.schedule(2, () => console.log('third'));9s.flushUntil(2);10s.flushUntil(3);11configureGlobal({ interruptAfterTimeLimit: 1000 });12s.flushAll();
Using AI Code Generation
1const fc = require("fast-check");2const { SchedulerArbitrary } = require("fast-check/lib/types/arbitrary/SchedulerArbitrary");3const fc = require("fast-check");4const { SchedulerArbitrary } = require("fast-check/lib/types/arbitrary/SchedulerArbitrary");5fc.assert(6 fc.property(SchedulerArbitrary(), (scheduler) => {7 })8);9const { SchedulerArbitrary } = require("fast-check/lib/types/arbitrary/SchedulerArbitrary");10const { Scheduler } = require("fast-check/lib/types/Scheduler");11const scheduler = new Scheduler();12fc.assert(13 fc.property(SchedulerArbitrary(), (s) => {14 })15);16fc.assert(17 fc.property(SchedulerArbitrary(), (s) => {18 })19);20fc.assert(21 fc.property(SchedulerArbitrary(), (s) => {22 })23);24fc.assert(25 fc.property(SchedulerArbitrary(), (s) => {26 })27);28fc.assert(29 fc.property(SchedulerArbitrary(), (s) => {30 })31);32fc.assert(33 fc.property(SchedulerArbitrary(), (s) => {34 })35);36fc.assert(37 fc.property(SchedulerArbitrary(), (s) => {38 })39);40fc.assert(41 fc.property(SchedulerArbitrary(), (s) => {42 })43);44fc.assert(45 fc.property(SchedulerArbitrary(), (s) => {46 })47);48fc.assert(49 fc.property(SchedulerArbitrary(), (s) => {
Using AI Code Generation
1import { SchedulerArbitrary } from 'fast-check/lib/esm/check/arbitrary/SchedulerArbitrary.js';2import { scheduler } from 'fast-check/lib/esm/check/arbitrary/SchedulerArbitrary.js';3import { integer } from 'fast-check/lib/esm/check/arbitrary/IntegerArbitrary.js';4import { tuple } from 'fast-check/lib/esm/check/arbitrary/TupleArbitrary.js';5import { constantFrom } from 'fast-check/lib/esm/check/arbitrary/ConstantArbitrary.js';6import { stringOf } from 'fast-check/lib/esm/check/arbitrary/StringArbitrary.js';7import { array } from 'fast-check/lib/esm/check/arbitrary/ArrayArbitrary.js';8import { oneof } from 'fast-check/lib/esm/check/arbitrary/OneOfArbitrary.js';9import { record } from 'fast-check/lib/esm/check/arbitrary/RecordArbitrary.js';10import { set } from 'fast-check/lib/esm/check/arbitrary/SetArbitrary.js';11import { dictionary } from 'fast-check/lib/esm/check/arbitrary/DictionaryArbitrary.js';12import { option } from 'fast-check/lib/esm/check/arbitrary/OptionArbitrary.js';13import { anything } from 'fast-check/lib/esm/check/arbitrary/AnythingArbitrary.js';14import { cloneMethod } from 'fast-check/lib/esm/check/symbols.js';15import { cloneMethod as cloneMethod$1 } from 'fast-check/lib/esm/check/symbols.js';16import { cloneMethod as cloneMethod$2 } from 'fast-check/lib/esm/check/symbols.js';17import { cloneMethod as cloneMethod$3 } from 'fast-check/lib/esm/check/symbols.js';18import { cloneMethod as cloneMethod$4 } from 'fast-check/lib/esm/check/symbols.js';19import { cloneMethod as cloneMethod$5 } from 'fast-check/lib/esm/check/symbols.js';20import { cloneMethod as cloneMethod$6 } from 'fast-check/lib/esm/check/symbols.js';21import { cloneMethod as cloneMethod$7 } from 'fast-check/lib/esm/check/symbols.js';22import { cloneMethod as cloneMethod$8 } from 'fast-check/lib/esm/check/symbols.js';23import { cloneMethod as cloneMethod$9 } from 'fast-check/lib/esm/check/symbols.js';24import { cloneMethod as cloneMethod$10 } from 'fast-check/lib/esm/check/s
Using AI Code Generation
1const { SchedulerArbitrary } = require("fast-check");2const { Scheduler } = require("fast-check/lib/types/scheduler/Scheduler");3const { scheduler } = require("fast-check/lib/types/scheduler/Scheduler");4const { schedulerWithTime } = require("fast-check/lib/types/scheduler/SchedulerWithTime");5const { schedulerWithTimeAndCounters } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCounters");6const { schedulerWithTimeAndCountersAndHooks } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooks");7const { schedulerWithTimeAndCountersAndHooksAndClock } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClock");8const { schedulerWithTimeAndCountersAndHooksAndClockAndState } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClockAndState");9const { schedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandom } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandom");10const { schedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraints } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraints");11const { schedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraintsAndRecorded } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraintsAndRecorded");12const { schedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraintsAndRecordedAndTime } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraintsAndRecordedAndTime");13const { schedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraintsAndRecordedAndTimeAndState } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraintsAndRecordedAndTimeAndState");14const { schedulerWithTimeAndCountersAndHooksAndClockAndStateAndRandomAndConstraintsAndRecordedAndTimeAndStateAndRandom } = require("fast-check/lib/types/scheduler/SchedulerWithTimeAndCountersAndHooksAndClockAndState
Using AI Code Generation
1const SchedulerArbitrary = require('fast-check/lib/arbitrary/SchedulerArbitrary.js');2const fc = require('fast-check');3const { it } = require('mocha');4it('should run', () => {5 fc.assert(6 fc.property(SchedulerArbitrary(), (scheduler) => {7 return true;8 })9 );10});11{12 "scripts": {13 },14 "dependencies": {15 },16 "devDependencies": {17 }18}19const { SchedulerArbitrary } = require('fast-check/lib/arbitrary/SchedulerArbitrary.js');20const fc = require('fast-check');21const { it } = require('mocha');22it('should run', () => {23 fc.assert(24 fc.property(SchedulerArbitrary(), (scheduler) => {25 return true;26 })27 );28});29{30 "scripts": {31 },32 "dependencies": {33 },34 "devDependencies": {35 }36}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!