Best JavaScript code snippet using fast-check-monorepo
CommandsArbitrary.ts
Source:CommandsArbitrary.ts
1import { Arbitrary } from '../../check/arbitrary/definition/Arbitrary';2import { Value } from '../../check/arbitrary/definition/Value';3import { ICommand } from '../../check/model/command/ICommand';4import { CommandsIterable } from '../../check/model/commands/CommandsIterable';5import { CommandWrapper } from '../../check/model/commands/CommandWrapper';6import { ReplayPath } from '../../check/model/ReplayPath';7import { Random } from '../../random/generator/Random';8import { makeLazy } from '../../stream/LazyIterableIterator';9import { Stream } from '../../stream/Stream';10import { oneof } from '../oneof';11import { restrictedIntegerArbitraryBuilder } from './builders/RestrictedIntegerArbitraryBuilder';12// eslint-disable-next-line @typescript-eslint/ban-types13type CommandsArbitraryContext<Model extends object, Real, RunResult, CheckAsync extends boolean> = {14 shrunkOnce: boolean;15 items: Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[];16};17/** @internal */18// eslint-disable-next-line @typescript-eslint/ban-types19export class CommandsArbitrary<Model extends object, Real, RunResult, CheckAsync extends boolean> extends Arbitrary<20 CommandsIterable<Model, Real, RunResult, CheckAsync>21> {22 readonly oneCommandArb: Arbitrary<CommandWrapper<Model, Real, RunResult, CheckAsync>>;23 readonly lengthArb: Arbitrary<number>;24 private replayPath: boolean[];25 private replayPathPosition: number;26 constructor(27 commandArbs: Arbitrary<ICommand<Model, Real, RunResult, CheckAsync>>[],28 maxGeneratedCommands: number,29 maxCommands: number,30 readonly sourceReplayPath: string | null,31 readonly disableReplayLog: boolean32 ) {33 super();34 this.oneCommandArb = oneof(...commandArbs).map((c) => new CommandWrapper(c));35 this.lengthArb = restrictedIntegerArbitraryBuilder(0, maxGeneratedCommands, maxCommands);36 this.replayPath = []; // updated at first shrink37 this.replayPathPosition = 0;38 }39 private metadataForReplay() {40 return this.disableReplayLog ? '' : `replayPath=${JSON.stringify(ReplayPath.stringify(this.replayPath))}`;41 }42 private buildValueFor(items: Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[], shrunkOnce: boolean) {43 const commands = items.map((item) => item.value_);44 const context: CommandsArbitraryContext<Model, Real, RunResult, CheckAsync> = { shrunkOnce, items };45 return new Value(new CommandsIterable(commands, () => this.metadataForReplay()), context);46 }47 generate(mrng: Random): Value<CommandsIterable<Model, Real, RunResult, CheckAsync>> {48 // For the moment, we fully ignore the bias on commands...49 const size = this.lengthArb.generate(mrng, undefined);50 const sizeValue = size.value;51 const items: Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[] = Array(sizeValue);52 for (let idx = 0; idx !== sizeValue; ++idx) {53 // ...even when generating the commands themselves54 const item = this.oneCommandArb.generate(mrng, undefined);55 items[idx] = item;56 }57 this.replayPathPosition = 0; // reset replay58 return this.buildValueFor(items, false);59 }60 canShrinkWithoutContext(value: unknown): value is CommandsIterable<Model, Real, RunResult, CheckAsync> {61 // Not supported yet on commands62 return false;63 }64 /** Filter commands based on the real status of the execution */65 private filterOnExecution(itemsRaw: Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[]) {66 const items: typeof itemsRaw = [];67 for (const c of itemsRaw) {68 if (c.value_.hasRan) {69 this.replayPath.push(true);70 items.push(c);71 } else this.replayPath.push(false);72 }73 return items;74 }75 /** Filter commands based on the internal replay state */76 private filterOnReplay(itemsRaw: Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[]) {77 return itemsRaw.filter((c, idx) => {78 const state = this.replayPath[this.replayPathPosition + idx];79 if (state === undefined) throw new Error(`Too short replayPath`);80 if (!state && c.value_.hasRan) throw new Error(`Mismatch between replayPath and real execution`);81 return state;82 });83 }84 /** Filter commands for shrinking purposes */85 private filterForShrinkImpl(itemsRaw: Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[]) {86 if (this.replayPathPosition === 0) {87 this.replayPath = this.sourceReplayPath !== null ? ReplayPath.parse(this.sourceReplayPath) : [];88 }89 const items =90 this.replayPathPosition < this.replayPath.length91 ? this.filterOnReplay(itemsRaw)92 : this.filterOnExecution(itemsRaw);93 this.replayPathPosition += itemsRaw.length;94 return items;95 }96 shrink(97 _value: CommandsIterable<Model, Real, RunResult, CheckAsync>,98 context: unknown99 ): Stream<Value<CommandsIterable<Model, Real, RunResult, CheckAsync>>> {100 if (context === undefined) {101 return Stream.nil<Value<CommandsIterable<Model, Real, RunResult, CheckAsync>>>();102 }103 const safeContext = context as CommandsArbitraryContext<Model, Real, RunResult, CheckAsync>;104 const shrunkOnce = safeContext.shrunkOnce;105 const itemsRaw = safeContext.items;106 const items = this.filterForShrinkImpl(itemsRaw); // filter out commands that have not been executed107 if (items.length === 0) {108 return Stream.nil<Value<CommandsIterable<Model, Real, RunResult, CheckAsync>>>();109 }110 // The shrinker of commands have to keep the last item111 // because it is the one causing the failure112 const rootShrink: Stream<Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[]> = shrunkOnce113 ? Stream.nil()114 : new Stream([[]][Symbol.iterator]());115 // If the resulting shrinkable was simply built by joining the streams one by one,116 // > stream[n] = stream[n-1].join(nextFor[n]) -- with nextFor[-1] = rootShrink117 // we would run into stack overflows when calling next to get back the 1st element (for huge n).118 // Indeed calling stream[n].next() would require to call stream[n-1].next() and so on...119 // Instead of that we define stream[n] = rootShrink.join(nextFor[0], ..., nextFor[n])120 // So that calling next on stream[n] will not have to run too many recursions121 const nextShrinks: IterableIterator<Value<CommandWrapper<Model, Real, RunResult, CheckAsync>>[]>[] = [];122 // keep fixed number commands at the beginning123 // remove items in remaining part except the last one124 for (let numToKeep = 0; numToKeep !== items.length; ++numToKeep) {125 nextShrinks.push(126 makeLazy(() => {127 const fixedStart = items.slice(0, numToKeep);128 return this.lengthArb129 .shrink(items.length - 1 - numToKeep, undefined)130 .map((l) => fixedStart.concat(items.slice(items.length - (l.value + 1))));131 })132 );133 }134 // shrink one by one135 for (let itemAt = 0; itemAt !== items.length; ++itemAt) {136 nextShrinks.push(137 makeLazy(() =>138 this.oneCommandArb139 .shrink(items[itemAt].value_, items[itemAt].context)140 .map((v) => items.slice(0, itemAt).concat([v], items.slice(itemAt + 1)))141 )142 );143 }144 return rootShrink.join(...nextShrinks).map((shrinkables) => {145 return this.buildValueFor(146 shrinkables.map((c) => new Value(c.value_.clone(), c.context)),147 true148 );149 });150 }...
commands.ts
Source:commands.ts
1import { Arbitrary } from '../check/arbitrary/definition/Arbitrary';2import { AsyncCommand } from '../check/model/command/AsyncCommand';3import { Command } from '../check/model/command/Command';4import { ICommand } from '../check/model/command/ICommand';5import { CommandsContraints } from '../check/model/commands/CommandsContraints';6import { CommandsArbitrary } from './_internals/CommandsArbitrary';7import {8 maxGeneratedLengthFromSizeForArbitrary,9 MaxLengthUpperBound,10} from './_internals/helpers/MaxLengthFromMinLength';11/**12 * For arrays of {@link AsyncCommand} to be executed by {@link asyncModelRun}13 *14 * This implementation comes with a shrinker adapted for commands.15 * It should shrink more efficiently than {@link array} for {@link AsyncCommand} arrays.16 *17 * @param commandArbs - Arbitraries responsible to build commands18 * @param constraints - Constraints to be applied when generating the commands (since 1.11.0)19 *20 * @remarks Since 1.5.021 * @public22 */23function commands<Model extends object, Real, CheckAsync extends boolean>(24 commandArbs: Arbitrary<AsyncCommand<Model, Real, CheckAsync>>[],25 constraints?: CommandsContraints26): Arbitrary<Iterable<AsyncCommand<Model, Real, CheckAsync>>>;27/**28 * For arrays of {@link Command} to be executed by {@link modelRun}29 *30 * This implementation comes with a shrinker adapted for commands.31 * It should shrink more efficiently than {@link array} for {@link Command} arrays.32 *33 * @param commandArbs - Arbitraries responsible to build commands34 * @param constraints - Constraints to be applied when generating the commands (since 1.11.0)35 *36 * @remarks Since 1.5.037 * @public38 */39function commands<Model extends object, Real>(40 commandArbs: Arbitrary<Command<Model, Real>>[],41 constraints?: CommandsContraints42): Arbitrary<Iterable<Command<Model, Real>>>;43function commands<Model extends object, Real, RunResult, CheckAsync extends boolean>(44 commandArbs: Arbitrary<ICommand<Model, Real, RunResult, CheckAsync>>[],45 constraints: CommandsContraints = {}46): Arbitrary<Iterable<ICommand<Model, Real, RunResult, CheckAsync>>> {47 const { size, maxCommands = MaxLengthUpperBound, disableReplayLog = false, replayPath = null } = constraints;48 const specifiedMaxCommands = constraints.maxCommands !== undefined;49 const maxGeneratedCommands = maxGeneratedLengthFromSizeForArbitrary(size, 0, maxCommands, specifiedMaxCommands);50 return new CommandsArbitrary(commandArbs, maxGeneratedCommands, maxCommands, replayPath, disableReplayLog);51}...
Using AI Code Generation
1import * as fc from 'fast-check';2import { maxGeneratedCommands } from 'fast-check-monorepo';3const arb = fc.oneof(fc.constant(1), fc.constant(2));4console.log(maxGeneratedCommands(arb, 100));5[tsl] ERROR in /Users/abraham/development/fast-check-monorepo/test3.js(4,19)6If you are using the .js file, you need to use the following syntax to import the method:7const { maxGeneratedCommands } = require('fast-check-monorepo');8If you are using the .d.ts file, you need to use the following syntax to import the method:9import { maxGeneratedCommands } from 'fast-check-monorepo';
Using AI Code Generation
1const { maxGeneratedCommands } = require('fast-check-monorepo');2console.log(maxGeneratedCommands());3const { maxGeneratedCommands } = require('fast-check-monorepo');4console.log(maxGeneratedCommands());5const { maxGeneratedCommands } = require('fast-check-monorepo');6console.log(maxGeneratedCommands());7const { maxGeneratedCommands } = require('fast-check-monorepo');8console.log(maxGeneratedCommands());9const { maxGeneratedCommands } = require('fast-check-monorepo');10console.log(maxGeneratedCommands());11const { maxGeneratedCommands } = require('fast-check-monorepo');12console.log(maxGeneratedCommands());13const { maxGeneratedCommands } = require('fast-check-monorepo');14console.log(maxGeneratedCommands());15const { maxGeneratedCommands } = require('fast-check-monorepo');16console.log(maxGeneratedCommands());17const { maxGeneratedCommands } = require('fast-check-monorepo');18console.log(maxGenerated
Using AI Code Generation
1import * as fc from "fast-check";2const maxGeneratedCommands = fc.maxGeneratedCommands(5, fc.integer());3console.log(maxGeneratedCommands);4import * as fc from "fast-check";5const maxRunTime = fc.maxRunTime(5, fc.integer());6console.log(maxRunTime);7import * as fc from "fast-check";8const maxSkips = fc.maxSkips(5, fc.integer());9console.log(maxSkips);10import * as fc from "fast-check";11const maxShrinks = fc.maxShrinks(5, fc.integer());12console.log(maxShrinks);13import * as fc from "fast-check";14const maxTries = fc.maxTries(5, fc.integer());15console.log(maxTries);16import * as fc from "fast-check";17const maxTriesPerProperty = fc.maxTriesPerProperty(5, fc.integer());18console.log(maxTriesPerProperty);19import * as fc from "fast-check";20const maxTriesWithoutSuccess = fc.maxTriesWithoutSuccess(5, fc.integer());21console.log(maxTriesWithoutSuccess);22import * as fc from "fast-check";23const maxTriesWithoutSuccessPerProperty = fc.maxTriesWithoutSuccessPerProperty(5, fc.integer());24console.log(maxTriesWithoutSuccessPerProperty);25import * as fc from "fast-check";26const maxTriesWithoutSuccessPerProperty1 = fc.maxTriesWithoutSuccessPerProperty(5, fc.integer());27console.log(maxTriesWithoutSuccessPerProperty
Using AI Code Generation
1const fc = require('fast-check');2const { maxGeneratedCommands } = require('fast-check/lib/check/runner/Runner.js');3const { instance: { _settings: { seed } } } = fc;4const { instance: { _settings: { numRuns } } } = fc;5const { instance: { _settings: { numShrinksPerRun } } } = fc;6const { instance: { _settings: { path } } } = fc;7const { instance: { _settings: { endOnFailure } } } = fc;8const { instance: { _settings: { verbose } } } = fc;9const { instance: { _settings: { interruptAfterTimeLimit } } } = fc;10const { instance: { _settings: { interruptAfterTimeLimitMs } } } = fc;11const { instance: { _settings: { interruptAfterNumberOfTests } } } = fc;12const { instance: { _settings: { interruptAfterNumberOfTestsThreshold } } } = fc;13const { instance: { _settings: { interruptAfterNumberOfShrinks } } } = fc;14const { instance: { _settings: { interruptAfterNumberOfShrinksThreshold } } } = fc;15const { instance: { _settings: { interruptAfterNumberOfDiscardedTests } } } = fc;16const { instance: { _settings: { interruptAfterNumberOfDiscardedTestsThreshold } } } = fc;17const { instance: { _settings: { interruptAfterNumberOfSkippedTests } } } = fc;18const { instance: { _settings: { interruptAfterNumberOfSkippedTestsThreshold } } } = fc;19const { instance: { _settings: { interruptAfterNumberOfReports } } } = fc;20const { instance: { _settings: { interruptAfterNumberOfReportsThreshold } } } = fc;21const { instance: { _settings: { interruptAfterNumberOfReportedTests } } } = fc;22const { instance: { _settings: { interruptAfterNumberOfReportedTestsThreshold } } } = fc;23const { instance: { _settings: { interruptAfterNumberOfReportedShrinks } } } = fc;24const { instance: { _settings: { interruptAfterNumberOfReportedShrinksThreshold } } } = fc;25const { instance: { _settings: { interruptAfterNumberOfReportedDiscardedTests } } } =
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!!