Best JavaScript code snippet using fast-check-monorepo
NoStackOverflowOnShrink.spec.ts
Source:NoStackOverflowOnShrink.spec.ts
1import * as fc from '../../src/fast-check';2import { seed } from './seed';3import * as prand from 'pure-rand';4const computeMaximalStackSize = () => {5 // Compute the maximal call stack size6 let depth = 0;7 const f = () => {8 ++depth;9 f();10 };11 try {12 f();13 } catch (_err) {14 // throws 'RangeError: Maximum call stack size exceeded'15 }16 return depth;17};18const callStackSize = computeMaximalStackSize();19const callStackSizeWithMargin = 2 * callStackSize;20// Configure arbitraries and provide them with a maximal length much greater than the default one21// This value is hardcoded in order to avoid variations from one env to another and ease replays in case of problem22const maxDepthForArrays = 40000;23// Not all the shrunk values of a given generated value will be asked24// The aim is to check if asking for the first maxShrinksToAsk might trigger unwanted stack overflows25const maxShrinksToAsk = 100;26describe(`NoStackOverflowOnShrink (seed: ${seed})`, () => {27 const iterateOverShrunkValues = <T>(arb: fc.Arbitrary<T>, v: fc.Value<T>) => {28 const it = arb.shrink(v.value_, v.context).take(maxShrinksToAsk)[Symbol.iterator]();29 let cur = it.next();30 while (!cur.done) {31 cur = it.next();32 }33 };34 it('should not run into stack overflow during very deep shrink tasks', () => {35 // We expect the depth used by this test to be greater than36 // the maximal depth we computed before reaching a stack overflow37 expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);38 class InfiniteShrinkingDepth extends fc.Arbitrary<number> {39 generate(_mrng: fc.Random): fc.Value<number> {40 return new fc.Value(0, undefined);41 }42 canShrinkWithoutContext(value: unknown): value is number {43 return false;44 }45 shrink(value: number): fc.Stream<fc.Value<number>> {46 if (value <= -maxDepthForArrays) {47 return fc.Stream.nil();48 }49 return fc.Stream.of(new fc.Value(value - 1, undefined));50 }51 }52 const out = fc.check(53 fc.property(new InfiniteShrinkingDepth(), (_n) => false),54 { seed }55 );56 expect(out.failed).toBe(true);57 expect(out.counterexamplePath).toBe([...Array(maxDepthForArrays + 1)].map(() => '0').join(':'));58 });59 it('should not run into stack overflow while calling shrink on very large arrays', () => {60 // We expect the depth used by this test to be greater than61 // the maximal depth we computed before reaching a stack overflow62 expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);63 const mrng = new fc.Random(prand.xorshift128plus(seed));64 const arb = fc.array(fc.boolean(), { maxLength: maxDepthForArrays, size: 'max' });65 let value: fc.Value<boolean[]> | null = null;66 while (value === null) {67 const tempShrinkable = arb.generate(mrng, undefined);68 if (tempShrinkable.value.length >= callStackSize) {69 value = tempShrinkable;70 }71 }72 expect(() => iterateOverShrunkValues(arb, value!)).not.toThrow();73 });74 it('should not run into stack overflow while calling shrink on very large shuffled sub-arrays', () => {75 // We expect the depth used by this test to be greater than76 // the maximal depth we computed before reaching a stack overflow77 expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);78 const mrng = new fc.Random(prand.xorshift128plus(seed));79 const arb = fc.shuffledSubarray([...Array(maxDepthForArrays)].map((_, i) => i));80 let value: fc.Value<number[]> | null = null;81 while (value === null) {82 const tempShrinkable = arb.generate(mrng, undefined);83 if (tempShrinkable.value.length >= callStackSize) {84 value = tempShrinkable;85 }86 }87 expect(() => iterateOverShrunkValues(arb, value!)).not.toThrow();88 });89 it('should not run into stack overflow while calling shrink on very large arrays of commands', () => {90 // We expect the depth used by this test to be greater than91 // the maximal depth we computed before reaching a stack overflow92 expect(maxDepthForArrays).toBeGreaterThan(callStackSizeWithMargin);93 class AnyCommand implements fc.Command<Record<string, unknown>, unknown> {94 constructor(readonly b: boolean) {}95 check = () => true;96 run = () => {};97 }98 const mrng = new fc.Random(prand.xorshift128plus(seed));99 const arb = fc.commands([fc.boolean().map((b) => new AnyCommand(b))], {100 maxCommands: maxDepthForArrays,101 size: 'max',102 });103 let value: fc.Value<Iterable<fc.Command<Record<string, unknown>, unknown>>> | null = null;104 while (value === null) {105 const tempShrinkable = arb.generate(mrng, undefined);106 const cmds = [...tempShrinkable.value];107 if (cmds.length >= callStackSize) {108 fc.modelRun(() => ({ model: {}, real: {} }), cmds);109 value = tempShrinkable;110 }111 }112 expect(() => iterateOverShrunkValues(arb, value!)).not.toThrow();113 });...
Using AI Code Generation
1const {tempShrinkable} = require('fast-check');2const {shrinkString} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');3const {shrinkNumber} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');4const {shrinkObject} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');5const {shrinkArray} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');6const shrunkString = tempShrinkable(shrinkString, 'hello');7const shrunkNumber = tempShrinkable(shrinkNumber, 10);8const shrunkObject = tempShrinkable(shrinkObject, {a: 1, b: 2, c: 3});9const shrunkArray = tempShrinkable(shrinkArray, [1, 2, 3, 4, 5]);10console.log(shrunkString);11console.log(shrunkNumber);12console.log(shrunkObject);13console.log(shrunkArray);14const {tempShrinkable} = require('fast-check');15const {shrinkString} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');16const {shrinkNumber} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');17const {shrinkObject} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');18const {shrinkArray} = require('fast-check/lib/arbitrary/_internals/helpers/Shrinkable');19const shrunkString = tempShrinkable(shrinkString, 'hello');20const shrunkNumber = tempShrinkable(shrinkNumber, 10);21const shrunkObject = tempShrinkable(shrinkObject, {a: 1, b: 2, c: 3});22const shrunkArray = tempShrinkable(shrinkArray, [1, 2, 3, 4, 5]);23test('shrunkString should be a string', () => {24 expect(typeof shrunkString).toBe('string');25});26test('shrunkNumber should be a number', () => {27 expect(typeof shrunkNumber).toBe('number
Using AI Code Generation
1const { tempShrinkable } = require("fast-check");2console.log("tempShrinkable(1):", tempShrinkable(1));3console.log("tempShrinkable(3):", tempShrinkable(3));4console.log("tempShrinkable(5):", tempShrinkable(5));5console.log("tempShrinkable(7):", tempShrinkable(7));6const { tempShrinkable } = require("fast-check");7console.log("tempShrinkable(1):", tempShrinkable(1));8console.log("tempShrinkable(3):", tempShrinkable(3));9console.log("tempShrinkable(5):", tempShrinkable(5));10console.log("tempShrinkable(7):", tempShrinkable(7));11const { tempShrinkable } = require("fast-check");12console.log("tempShrinkable(1):", tempShrinkable(1));13console.log("tempShrinkable(3):", tempShrinkable(3));14console.log("tempShrinkable(5):", tempShrinkable(5));15console.log("tempShrinkable(7):", tempShrinkable(7));
Using AI Code Generation
1const fc = require("fast-check");2const { tempShrinkable } = require("fast-check/lib/check/arbitrary/definition/Shrinkable");3const arb = fc.constantFrom(1, 2, 3);4for (let i = 0; i < 5; i++) {5 const s = tempShrinkable(arb.generate(mrng()), false);6 console.log(s.value);7 console.log(s.shrink().map((s) => s.value));8}9const fc = require("fast-check");10const { tempShrinkable } = require("fast-check/lib/check/arbitrary/definition/Shrinkable");11const arb = fc.constantFrom(1, 2, 3);12for (let i = 0; i < 5; i++) {13 const s = tempShrinkable(arb.generate(mrng()), false);14 console.log(s.value);15 console.log(s.shrink().map((s) => s.value));16}17const fc = require("fast-check");18const { tempShrinkable } = require("fast-check/lib/check/arbitrary/definition/Shrinkable");19const arb = fc.constantFrom(1, 2, 3);20for (let i = 0; i < 5; i++) {21 const s = tempShrinkable(arb.generate(mrng()), false);22 console.log(s.value);23 console.log(s.shrink().map((s) => s.value));24}25const fc = require("fast-check");26const { tempShrinkable } = require("fast-check/lib/check/arbitrary/definition/Shrinkable");27const arb = fc.constantFrom(1, 2, 3);28for (let i = 0; i < 5; i++) {29 const s = tempShrinkable(arb.generate(mrng()), false);30 console.log(s.value);31 console.log(s.shrink().map((s) => s.value));32}33const fc = require("fast-check");34const {
Using AI Code Generation
1const fc = require("fast-check");2const { tempShrinkable } = require("fast-check/lib/check/arbitrary/definition/TempShrinkable.js");3test("tempShrinkable", () => {4 const arb = fc.nat();5 const shrinkable = arb.generate(mrng());6 const tempShrinkable1 = tempShrinkable(shrinkable);7 expect(tempShrinkable1.value).toEqual(shrinkable.value);8 expect(tempShrinkable1.context).toEqual(shrinkable.context);9 expect(tempShrinkable1.shrink).toEqual(shrinkable.shrink);10});11const fc = require("fast-check");12const { tempShrinkable } = require("fast-check/lib/check/arbitrary/definition/TempShrinkable.js");13test("tempShrinkable", () => {14 const arb = fc.nat();15 const shrinkable = arb.generate(mrng());16 const tempShrinkable1 = tempShrinkable(shrinkable);17 expect(tempShrinkable1.value).toEqual(shrinkable.value);18 expect(tempShrinkable1.context).toEqual(shrinkable.context);19 expect(tempShrinkable1.shrink).toEqual(shrinkable.shrink);20});21const fc = require("fast-check");22const { tempShrinkable } = require("fast-check/lib/check/arbitrary/definition/TempShrinkable.js");23test("tempShrinkable", () => {24 const arb = fc.nat();25 const shrinkable = arb.generate(mrng());26 const tempShrinkable1 = tempShrinkable(shrinkable);27 expect(tempShrinkable1.value).toEqual(shrinkable.value);28 expect(tempShrinkable1.context).toEqual(shrinkable.context);29 expect(tempShrinkable1.shrink).toEqual(shrinkable.shrink);30});
Using AI Code Generation
1const { tempShrinkable } = require('fast-check');2const shrinkable = tempShrinkable(42);3console.log(shrinkable.value);4const { tempShrinkable } = require('fast-check');5const shrinkable = tempShrinkable(42, () => tempShrinkable(24));6console.log(shrinkable.value);7const { tempShrinkable } = require('fast-check');8const shrinkable = tempShrinkable(42, () => tempShrinkable(24));9console.log(shrinkable.value);10console.log(shrinkable.shrink().value);11const { tempShrinkable } = require('fast-check');12const shrinkable = tempShrinkable(42, () => tempShrinkable(24));13console.log(shrinkable.value);14console.log(shrinkable.shrink().value);15console.log(shrinkable.shrink().shrink().value);16const { tempShrinkable } = require('fast-check');17const shrinkable = tempShrinkable(42, () => tempShrinkable(42));18console.log(shrinkable.value);
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!!