How to use ConstantArbitrary method in fast-check-monorepo

Best JavaScript code snippet using fast-check-monorepo

ConstantArbitrary.spec.ts

Source:ConstantArbitrary.spec.ts Github

copy

Full Screen

...16 const expectedBiasFactor = 48;17 const value = Symbol();18 const { instance: mrng } = fakeRandom();19 // Act20 const arb = new ConstantArbitrary([value]);21 const g = arb.generate(mrng, expectedBiasFactor);22 // Assert23 expect(g.value).toEqual(value);24 expect(g.hasToBeCloned).toBe(false);25 });26 it('should call Random to generate any integer in [0, length-1] when provided multiple values', () =>27 fc.assert(28 fc.property(29 fc.array(fc.anything(), { minLength: 2 }),30 fc.option(fc.integer({ min: 2 }), { nil: undefined }),31 fc.nat(),32 (values, biasFactor, mod) => {33 // Arrange34 const { instance: mrng, nextInt } = fakeRandom();35 nextInt.mockImplementation((a, b) => a + (mod % (b - a + 1)));36 // Act37 const arb = new ConstantArbitrary(values);38 const g = arb.generate(mrng, biasFactor);39 // Assert40 expect(nextInt).toHaveBeenCalledTimes(1);41 expect(nextInt).toHaveBeenCalledWith(0, values.length - 1);42 expect(values).toContainEqual(g.value);43 }44 )45 ));46 it('should be able to generate any of the requested values', () =>47 fc.assert(48 fc.property(49 fc.array(fc.anything(), { minLength: 2 }),50 fc.option(fc.integer({ min: 2 }), { nil: undefined }),51 (values, biasFactor) => {52 // Arrange53 const { instance: mrng, nextInt } = fakeRandom();54 // Act / Assert55 const arb = new ConstantArbitrary(values);56 const notSeenValues = [...values];57 for (let idx = 0; idx !== values.length; ++idx) {58 nextInt.mockImplementationOnce((a, _b) => a + idx);59 const g = arb.generate(mrng, biasFactor);60 const index = notSeenValues.findIndex((v) => Object.is(g.value, v));61 expect(index).not.toBe(-1);62 notSeenValues.splice(index, 1);63 }64 expect(notSeenValues).toEqual([]);65 }66 )67 ));68 it('should produce a cloneable instance if provided value is cloneable', () => {69 // Arrange70 const expectedBiasFactor = 48;71 const cloneable = Object.defineProperty([], cloneMethod, { value: jest.fn() });72 const { instance: mrng } = fakeRandom();73 // Act74 const arb = new ConstantArbitrary([cloneable]);75 const g = arb.generate(mrng, expectedBiasFactor);76 // Assert77 expect(g.hasToBeCloned).toBe(true);78 });79 it('should clone cloneable instances for each access', () => {80 // Arrange81 const expectedBiasFactor = 48;82 const cloneMethodImpl = jest.fn();83 const cloneable = Object.defineProperty([], cloneMethod, { value: cloneMethodImpl });84 cloneMethodImpl.mockReturnValue(cloneable); // in reality it should be a clone of it, not itself85 const { instance: mrng } = fakeRandom();86 // Act87 const arb = new ConstantArbitrary([cloneable]);88 const g = arb.generate(mrng, expectedBiasFactor);89 expect(cloneMethodImpl).not.toHaveBeenCalled();90 // Assert91 g.value;92 expect(cloneMethodImpl).toHaveBeenCalledTimes(1);93 g.value;94 expect(cloneMethodImpl).toHaveBeenCalledTimes(2);95 });96 });97 describe('canShrinkWithoutContext', () => {98 it("should mark value as 'canShrinkWithoutContext' whenever one of the original values is equal regarding Object.is", () =>99 fc.assert(100 fc.property(fc.array(fc.anything(), { minLength: 1 }), fc.nat(), (values, mod) => {101 // Arrange102 const selectedValue = values[mod % values.length];103 // Act104 const arb = new ConstantArbitrary(values);105 const out = arb.canShrinkWithoutContext(selectedValue);106 // Assert107 expect(out).toBe(true);108 })109 ));110 it('should not detect values not equal regarding to Object.is', () => {111 // Arrange112 const values: unknown[] = [0, [], {}, ''];113 const selectedValue: unknown = [];114 // Act115 const arb = new ConstantArbitrary<unknown>(values);116 const out = arb.canShrinkWithoutContext(selectedValue);117 // Assert118 expect(out).toBe(false); // Object.is([], []) is falsy119 });120 });121 describe('shrink', () => {122 it('should shrink towards the first value if it was not already this one and to nil otherwise', () =>123 fc.assert(124 fc.property(fc.array(fc.anything(), { minLength: 1 }), fc.nat(), (values, mod) => {125 // Arrange126 const { instance: mrng, nextInt } = fakeRandom();127 nextInt.mockImplementation((a, b) => a + (mod % (b - a + 1)));128 // Act129 const arb = new ConstantArbitrary(values);130 const value = arb.generate(mrng, undefined);131 const shrinks = [...arb.shrink(value.value, value.context)];132 // Assert133 if (Object.is(value.value, values[0])) {134 expect(shrinks.map((v) => v.value)).toEqual([]);135 } else {136 expect(shrinks.map((v) => v.value)).toEqual([values[0]]);137 }138 })139 ));140 it('should shrink towards the first value if it was not already this one and to nil otherwise even without any context', () =>141 fc.assert(142 fc.property(fc.array(fc.anything(), { minLength: 1 }), fc.nat(), (values, mod) => {143 // Arrange144 const { instance: mrng, nextInt } = fakeRandom();145 nextInt.mockImplementation((a, b) => a + (mod % (b - a + 1)));146 // Act147 const arb = new ConstantArbitrary(values);148 const value = arb.generate(mrng, undefined);149 const shrinks = [...arb.shrink(value.value, undefined)];150 // Assert151 if (Object.is(value.value, values[0])) {152 expect(shrinks.map((v) => v.value)).toEqual([]);153 } else {154 expect(shrinks.map((v) => v.value)).toEqual([values[0]]);155 }156 })157 ));158 it('should not shrink towards the first value if generated value is equal to the first one regarding `Object.is`', () => {159 // Arrange160 const { instance: mrng, nextInt } = fakeRandom();161 // Act / Assert162 const arb = new ConstantArbitrary([Number.NaN, Number.NaN, Number.NaN]);163 for (let idx = 0; idx !== 3; ++idx) {164 nextInt.mockReturnValue(idx);165 const value = arb.generate(mrng, undefined);166 expect(value.value).toBe(Number.NaN);167 const shrinks = [...arb.shrink(value.value, value.context)];168 expect(shrinks).toHaveLength(0);169 }170 });171 });172});173describe('ConstantArbitrary (integration)', () => {174 type Extra = unknown[];175 const extraParameters: fc.Arbitrary<Extra> = fc.array(fc.anything(), { minLength: 1 });176 // In other words: extra.includes(value) --but with Object.is177 const isCorrect = (value: unknown, extra: Extra) => extra.findIndex((v) => Object.is(value, v)) !== -1;178 // In other words: extra.indexOf(v1) < extra.indexOf(v2) --but with Object.is179 // If the same value has been declared twice in the `extra` once for `extra[0]` and the other for `extra[n]` (with n > 0)180 // Shrinker should never shrink `extra[n]` into `extra[0]` if they are equal regarding `Object.is`181 const isStrictlySmaller = (v1: unknown, v2: unknown, extra: Extra) =>182 extra.findIndex((v) => Object.is(v1, v)) < extra.findIndex((v) => Object.is(v2, v));183 const constantBuilder = (extra: Extra) => new ConstantArbitrary(extra);184 it('should produce the same values given the same seed', () => {185 assertProduceSameValueGivenSameSeed(constantBuilder, { extraParameters });186 });187 it('should only produce correct values', () => {188 assertProduceCorrectValues(constantBuilder, isCorrect, { extraParameters });189 });190 it('should produce values seen as shrinkable without any context', () => {191 assertProduceValuesShrinkableWithoutContext(constantBuilder, { extraParameters });192 });193 it('should preserve strictly smaller ordering in shrink', () => {194 assertShrinkProducesStrictlySmallerValue(constantBuilder, isStrictlySmaller, { extraParameters });195 });196 it('should not re-use twice the same instance of cloneable', () => {197 // Arrange198 const alreadySeenCloneable = new Set<unknown>();199 const buildCloneable = (): unknown => {200 return Object.defineProperty([], cloneMethod, { value: buildCloneable });201 };202 const arb = new ConstantArbitrary([buildCloneable()]);203 const { instance: mrng } = fakeRandom();204 // Act205 const g = arb.generate(mrng, undefined);206 const treeA = buildShrinkTree(arb, g);207 const treeB = buildShrinkTree(arb, g);208 // Assert209 walkTree(treeA, (cloneable) => {210 expect(alreadySeenCloneable.has(cloneable)).toBe(false);211 alreadySeenCloneable.add(cloneable);212 });213 walkTree(treeB, (cloneable) => {214 expect(alreadySeenCloneable.has(cloneable)).toBe(false);215 alreadySeenCloneable.add(cloneable);216 });...

Full Screen

Full Screen

constantFrom.spec.ts

Source:constantFrom.spec.ts Github

copy

Full Screen

...9 fc.configureGlobal({ beforeEach: beforeEachHook });10}11beforeEach(beforeEachHook);12describe('constantFrom', () => {13 it('should instantiate ConstantArbitrary(csts) for constantFrom(...csts)', () =>14 fc.assert(15 fc.property(fc.array(fc.anything(), { minLength: 1 }), (csts) => {16 // Arrange17 const { instance } = fakeArbitrary();18 const ConstantArbitrary = jest.spyOn(ConstantArbitraryMock, 'ConstantArbitrary');19 ConstantArbitrary.mockImplementation(() => instance as ConstantArbitraryMock.ConstantArbitrary<unknown>);20 // Act21 const arb = constantFrom(...csts);22 // Assert23 expect(ConstantArbitrary).toHaveBeenCalledWith(csts);24 expect(arb).toBe(instance);25 })26 ));27 it('should throw when receiving no parameters', () => {...

Full Screen

Full Screen

constant.spec.ts

Source:constant.spec.ts Github

copy

Full Screen

...9 fc.configureGlobal({ beforeEach: beforeEachHook });10}11beforeEach(beforeEachHook);12describe('constant', () => {13 it('should instantiate ConstantArbitrary([c]) for constant(c)', () =>14 fc.assert(15 fc.property(fc.anything(), (c) => {16 // Arrange17 const { instance } = fakeArbitrary();18 const ConstantArbitrary = jest.spyOn(ConstantArbitraryMock, 'ConstantArbitrary');19 ConstantArbitrary.mockImplementation(() => instance as ConstantArbitraryMock.ConstantArbitrary<unknown>);20 // Act21 const arb = constant(c);22 // Assert23 expect(ConstantArbitrary).toHaveBeenCalledWith([c]);24 expect(arb).toBe(instance);25 })26 ));27 it('should not throw on cloneable instance', () => {...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { ConstantArbitrary } = require("fast-check/lib/arbitrary/constantArbitrary");3const { array } = require("fast-check/lib/arbitrary/array");4const { tuple } = require("fast-check/lib/arbitrary/tuple");5const { integer } = require("fast-check/lib/arbitrary/integer");6const { string } = require("fast-check/lib/arbitrary/string");7const { oneof } = require("fast-check/lib/arbitrary/oneof");8const { record } = require("fast-check/lib/arbitrary/record");9const { object } = require("fast-check/lib/arbitrary/object");10const { set } = require("fast-check/lib/arbitrary/set");11const { map } = require("fast-check/lib/arbitrary/map");12const { bigInt } = require("fast-check/lib/arbitrary/bigInt");13const { date } = require("fast-check/lib/arbitrary/date");14const { double } = require("fast-check/lib/arbitrary/double");15const { float } = require("fast-check/lib/arbitrary/float");16const { boolean } = require("fast-check/lib/arbitrary/boolean");17const { char } = require("fast-check/lib/arbitrary/char");18const { unicode } = require("fast-check/lib/arbitrary/unicode");19const { unicodeString } = require("fast-check/lib/arbitrary/unicodeString");20const { fullUnicode } = require("fast-check/lib/arbitrary/fullUnicode");21const { fullUnicodeString } = require("fast-check/lib/arbitrary/fullUnicodeString");22const { hexa } = require("fast-check/lib/arbitrary/hexa");23const { hexaString } = require("fast-check/lib/arbitrary/hexaString");24const { base64 } = require("fast-check/lib/arbitrary/base64");25const { base64String } = require("fast-check/lib/arbitrary/base64String");26const { ascii } = require("fast-check/lib/arbitrary/ascii");27const { asciiString } = require("fast-check/lib/arbitrary/asciiString");28const { option } = require("fast-check/lib/arbitrary/option");29const { frequency } = require("fast-check/lib/arbitrary/frequency");30const { subarray } = require("fast-check/lib/arbitrary/subarray");31const { subarrayOf } = require("fast-check/lib/arbitrary/subarrayOf");32const { shuffle } = require("fast-check/lib/arbitrary/shuffle");33const {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ConstantArbitrary } = require('fast-check/lib/check/arbitrary/ConstantArbitrary.js');2const { integer } = require('fast-check/lib/arbitrary/IntegerArbitrary.js');3const { property } = require('fast-check/lib/check/property/Property.js');4const { string } = require('fast-check/lib/arbitrary/StringArbitrary.js');5const arb = ConstantArbitrary.from(integer(1, 100));6const arb2 = ConstantArbitrary.from(string());7property(arb, arb2, (a, b) => {8 return a === a && b === b;9}).check();10const { ConstantArbitrary } = require('fast-check');11const { integer } = require('fast-check');12const { property } = require('fast-check');13const { string } = require('fast-check');14const arb = ConstantArbitrary.from(integer(1, 100));15const arb2 = ConstantArbitrary.from(string());16property(arb, arb2, (a, b) => {17 return a === a && b === b;18}).check();

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require('fast-check');2const { ConstantArbitrary } = require('fast-check/lib/arbitrary/ConstantArbitrary.js');3const constantArbitrary = new ConstantArbitrary(2);4const constantArbitrary2 = new ConstantArbitrary(3);5fc.assert(fc.property(fc.integer(), (x) => {6 return constantArbitrary.generate(1)[0] === 2 && constantArbitrary2.generate(1)[0] === 3;7}));8const constantArbitrary = new ConstantArbitrary(2); const constantArbitrary2 = new ConstantArbitrary(3); fc.assert(fc.property(fc.integer(), (x) => { return constantArbitrary.generate(1)[0] === 2 && constantArbitrary2.generate(1)[0] === 3; }));9const fc = require ( 'fast-check' ) ; const { ConstantArbitrary } = require ( 'fast-check/lib/arbitrary/ConstantArbitrary.js' ) ; const constantArbitrary = new ConstantArbitrary ( 2 ) ; const constantArbitrary2 = new ConstantArbitrary ( 3 ) ; fc . assert ( fc

Full Screen

Using AI Code Generation

copy

Full Screen

1const { constant } = require('fast-check');2const { constantArbitrary } = constant;3const arb = constantArbitrary(1);4"dependencies": {5 },6 "devDependencies": {7 }8const { constant } = require('fast-check');9const { constantArbitrary } = constant;10const arb = constantArbitrary(1);11"scripts": {12 }13"dependencies": {14 },15 "devDependencies": {16 }17const { constant } = require('fast-check');18const { constantArbitrary } = constant;19const arb = constantArbitrary(1);20"scripts": {21 }

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ConstantArbitrary } = require('fast-check');2const arb = ConstantArbitrary('foo');3const { ConstantArbitrary } = require('fast-check/lib/check/arbitrary/ConstantArbitrary');4const arb = ConstantArbitrary('foo');5const { ConstantArbitrary } = require('fast-check');6const arb = ConstantArbitrary('foo');7It is not possible to use the latest version of fast-check with a require('fast-check') . You have to use require('fast-check/lib/check

Full Screen

Using AI Code Generation

copy

Full Screen

1const { ConstantArbitrary } = require("fast-check");2const { constant } = require("fast-check");3const { assert } = require("chai");4const { expect } = require("chai");5describe("ConstantArbitrary", () => {6 it("should generate the same value", () => {7 const arb = new ConstantArbitrary(42);8 const out = arb.generate(mrng());9 assert.equal(out, 42);10 });11});12describe("ConstantArbitrary", () => {13 it("should generate the same value", () => {14 const arb = constant(42);15 const out = arb.generate(mrng());16 assert.equal(out, 42);17 });18});19describe("ConstantArbitrary", () => {20 it("should generate the same value", () => {21 const arb = constant(42);22 const out = arb.generate(mrng());23 expect(out).to.equal(42);24 });25});26describe("ConstantArbitrary", () => {27 it("should generate the same value", () => {28 const arb = new ConstantArbitrary(42);29 const out = arb.generate(mrng());30 expect(out).to.equal(42);31 });32});33describe("ConstantArbitrary", () => {34 it("should generate the same value", () => {35 const arb = constant(42);36 const out = arb.generate(mrng());37 assert.equal(out, 42);38 });39});40describe("ConstantArbitrary", () => {41 it("should generate the same value", () => {42 const arb = constant(42);43 const out = arb.generate(mrng());44 expect(out).to.equal(42);45 });46});47describe("ConstantArbitrary", () => {48 it("should generate the same value", () => {49 const arb = new ConstantArbitrary(42);50 const out = arb.generate(mrng());51 assert.equal(out, 42);52 });53});54describe("ConstantArbitrary", () => {55 it("should generate the same value", () => {56 const arb = constant(42);57 const out = arb.generate(mrng());58 expect(out).to.equal(42);59 });60});

Full Screen

Using AI Code Generation

copy

Full Screen

1const fc = require("fast-check");2const { ConstantArbitrary } = require("fast-check/lib/types/arbitrary/ConstantArbitrary");3const { array } = require("fast-check/lib/types/arbitrary/ArrayArbitrary");4const { integer } = require("fast-check/lib/types/arbitrary/IntegerArbitrary");5const { Random } = require("fast-check/lib/random/generator/Random");6const { Stream } = require("fast-check/lib/stream/Stream");7const { RandomArray } = require("fast-check/lib/random/RandomArray");8const { RandomInteger } = require("fast-check/lib/random/RandomInteger");9const random = new Random(RandomArray);10const intArray = array(new ConstantArbitrary(new RandomInteger(0, 100), random), 10);11intArray.generate(random).value;12const fc = require("fast-check");13const { array } = require("fast-check/lib/types/arbitrary/ArrayArbitrary");14const { integer } = require("fast-check/lib/types/arbitrary/IntegerArbitrary");15const intArray = array(integer(0, 100), 10);16intArray.generate().value;

Full Screen

Using AI Code Generation

copy

Full Screen

1const { constant } = require('fast-check');2const constantArbitrary = constant('hello');3const result = constantArbitrary.generate();4console.log(result);5const { constant } = require('fast-check');6const constantArbitrary = constant('hello');7const result = constantArbitrary.generate();8console.log(result);9const { constant } = require('fast-check');10const constantArbitrary = constant('hello');11const result = constantArbitrary.generate();12console.log(result);13const { constant } = require('fast-check');14const constantArbitrary = constant('hello');15const result = constantArbitrary.generate();16console.log(result);17const { constant } = require('fast-check');18const constantArbitrary = constant('hello');19const result = constantArbitrary.generate();20console.log(result);21const { constant } = require('fast-check');22const constantArbitrary = constant('hello');23const result = constantArbitrary.generate();24console.log(result);25const { constant } = require('fast-check');26const constantArbitrary = constant('hello');27const result = constantArbitrary.generate();28console.log(result);29const { constant } = require('fast-check');30const constantArbitrary = constant('hello');31const result = constantArbitrary.generate();32console.log(result);33const { constant } = require('fast-check');34const constantArbitrary = constant('hello');35const result = constantArbitrary.generate();36console.log(result);37const { constant } = require('fast-check');

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