Best JavaScript code snippet using fast-check-monorepo
CloneArbitrary.spec.ts
Source:CloneArbitrary.spec.ts
...28 clone.mockReturnValueOnce(mrngClone1).mockReturnValueOnce(mrngClone2);29 const { instance: sourceArb, generate } = fakeArbitrary<symbol>();30 generate.mockReturnValue(new Value(producedValue, undefined));31 // Act32 const arb = new CloneArbitrary(sourceArb, numValues);33 const out = arb.generate(mrng, biasFactor);34 // Assert35 expect(out.value).toEqual([producedValue, producedValue, producedValue]);36 expect(generate).toHaveBeenCalledTimes(3);37 expect(generate).toHaveBeenCalledWith(mrngClone1, biasFactor);38 expect(generate).toHaveBeenCalledWith(mrngClone2, biasFactor);39 expect(generate).toHaveBeenLastCalledWith(mrng, biasFactor);40 });41 it.each`42 type | cloneable43 ${'non-cloneable'} | ${false}44 ${'cloneable'} | ${true}45 `('should produce a $type tuple when sub-value is $type', ({ cloneable }) => {46 // Arrange47 const numValues = 1;48 const { instance: mrng } = fakeRandom();49 const { instance: sourceArb, generate } = fakeArbitrary<unknown>();50 if (cloneable) generate.mockReturnValue(new Value({ [cloneMethod]: jest.fn() }, undefined));51 else generate.mockReturnValue(new Value({ m: jest.fn() }, undefined));52 // Act53 const arb = new CloneArbitrary(sourceArb, numValues);54 const out = arb.generate(mrng, numValues);55 // Assert56 expect(out.hasToBeCloned).toBe(cloneable);57 expect(hasCloneMethod(out.value)).toBe(cloneable);58 });59 });60 describe('canShrinkWithoutContext', () => {61 it('should return false if passed value does not have the right length', () =>62 fc.assert(63 fc.property(fc.nat({ max: 1000 }), fc.nat({ max: 1000 }), (numValues, numRequestedValues) => {64 // Arrange65 fc.pre(numValues !== numRequestedValues);66 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();67 // Act68 const arb = new CloneArbitrary(sourceArb, numValues);69 const out = arb.canShrinkWithoutContext([...Array(numRequestedValues)]);70 // Assert71 expect(out).toBe(false);72 expect(canShrinkWithoutContext).not.toHaveBeenCalled();73 })74 ));75 it('should return false if values are not equal regarding Object.is', () => {76 // Arrange77 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();78 // Act79 const arb = new CloneArbitrary(sourceArb, 2);80 const out = arb.canShrinkWithoutContext([{}, {}]);81 // Assert82 expect(out).toBe(false);83 expect(canShrinkWithoutContext).not.toHaveBeenCalled();84 });85 it.each`86 canShrinkWithoutContextValue87 ${true}88 ${false}89 `(90 'should ask sub-arbitrary whenever length is correct and children are equal regarding Object.is',91 ({ canShrinkWithoutContextValue }) => {92 // Arrange93 const value = {};94 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();95 canShrinkWithoutContext.mockReturnValue(canShrinkWithoutContextValue);96 // Act97 const arb = new CloneArbitrary(sourceArb, 2);98 const out = arb.canShrinkWithoutContext([value, value]);99 // Assert100 expect(out).toBe(canShrinkWithoutContextValue);101 expect(canShrinkWithoutContext).toHaveBeenCalledTimes(1);102 expect(canShrinkWithoutContext).toHaveBeenCalledWith(value);103 }104 );105 });106 describe('shrink', () => {107 it('should shrink numValues times the value and zip the outputs together', () => {108 // Arrange109 const value = Symbol();110 const s1 = Symbol();111 const s2 = Symbol();112 const numValues = 3;113 const { instance: sourceArb, shrink } = fakeArbitrary<symbol>();114 shrink115 .mockReturnValueOnce(Stream.of<Value<symbol>>(new Value(s1, undefined), new Value(s2, undefined)))116 .mockReturnValueOnce(Stream.of<Value<symbol>>(new Value(s1, undefined), new Value(s2, undefined)))117 .mockReturnValueOnce(Stream.of<Value<symbol>>(new Value(s1, undefined), new Value(s2, undefined)));118 // Act119 const arb = new CloneArbitrary(sourceArb, numValues);120 const shrinks = [...arb.shrink([value, value, value])];121 // Assert122 expect(shrinks.map((v) => v.value)).toEqual([123 [s1, s1, s1],124 [s2, s2, s2],125 ]);126 expect(shrink).toHaveBeenCalledTimes(3);127 expect(shrink).toHaveBeenCalledWith(value, undefined);128 });129 });130});131describe('CloneArbitrary (integration)', () => {132 type Extra = number;133 const extraParameters: fc.Arbitrary<Extra> = fc.nat({ max: 100 });134 const isCorrect = (value: number[], extra: Extra) =>135 Array.isArray(value) && value.length === extra && new Set(value).size <= 1;136 // Should never be called for extra = 0137 const isStrictlySmaller = (t1: number[], t2: number[]) => t1[0] < t2[0];138 const cloneBuilder = (extra: Extra) => new CloneArbitrary(new FakeIntegerArbitrary(), extra);139 it('should produce the same values given the same seed', () => {140 assertProduceSameValueGivenSameSeed(cloneBuilder, { extraParameters });141 });142 it('should only produce correct values', () => {143 assertProduceCorrectValues(cloneBuilder, isCorrect, { extraParameters });144 });145 it('should produce values seen as shrinkable without any context', () => {146 // Only when equal regarding Object.is147 assertProduceValuesShrinkableWithoutContext(cloneBuilder, { extraParameters });148 });149 it('should be able to shrink to the same values without initial context (if underlyings do)', () => {150 assertShrinkProducesSameValueWithoutInitialContext(cloneBuilder, { extraParameters });151 });152 it('should preserve strictly smaller ordering in shrink (if underlyings do)', () => {153 assertShrinkProducesStrictlySmallerValue(cloneBuilder, isStrictlySmaller, { extraParameters });154 });155 it('should produce the right shrinking tree', () => {156 // Arrange157 const arb = new CloneArbitrary(new FirstArbitrary(), 5);158 const { instance: mrng } = fakeRandom();159 // Act160 const g = arb.generate(mrng, undefined);161 const renderedTree = renderTree(buildShrinkTree(arb, g)).join('\n');162 // Assert163 expect(renderedTree).toMatchInlineSnapshot(`164 "[4,4,4,4,4]165 â> [2,2,2,2,2]166 | â> [0,0,0,0,0]167 â> [3,3,3,3,3]168 â> [0,0,0,0,0]169 â> [1,1,1,1,1]"170 `);171 });172 it('should not re-use twice the same instance of cloneable', () => {173 // Arrange174 const alreadySeenCloneable = new Set<unknown>();175 const arb = new CloneArbitrary(new CloneableArbitrary(), 5);176 const { instance: mrng } = fakeRandom();177 // Act178 const g = arb.generate(mrng, undefined);179 const treeA = buildShrinkTree(arb, g);180 const treeB = buildShrinkTree(arb, g);181 // Assert182 walkTree(treeA, ([_first, cloneable, _second]) => {183 expect(alreadySeenCloneable.has(cloneable)).toBe(false);184 alreadySeenCloneable.add(cloneable);185 });186 walkTree(treeB, ([_first, cloneable, _second]) => {187 expect(alreadySeenCloneable.has(cloneable)).toBe(false);188 alreadySeenCloneable.add(cloneable);189 });...
clone.spec.ts
Source:clone.spec.ts
...6 jest.restoreAllMocks();7}8beforeEach(beforeEachHook);9describe('clone', () => {10 it('should instantiate CloneArbitrary(arb, numValues) for clone(arb, numValues)', () => {11 // Arrange12 const numValues = 10;13 const { instance: sourceArbitrary } = fakeArbitrary();14 const { instance } = fakeArbitrary();15 const CloneArbitrary = jest.spyOn(CloneArbitraryMock, 'CloneArbitrary');16 CloneArbitrary.mockImplementation(() => instance as CloneArbitraryMock.CloneArbitrary<unknown>);17 // Act18 const arb = clone(sourceArbitrary, numValues);19 // Assert20 expect(CloneArbitrary).toHaveBeenCalledWith(sourceArbitrary, numValues);21 expect(arb).toBe(instance);22 });...
clone.ts
Source:clone.ts
...20 * @public21 */22function clone<T, N extends number>(arb: Arbitrary<T>, numValues: N): Arbitrary<CloneValue<T, N>>;23function clone<T>(arb: Arbitrary<T>, numValues: number): Arbitrary<T[]> {24 return new CloneArbitrary(arb, numValues);25}...
Using AI Code Generation
1const { cloneArbitrary } = require('@dubzzz/fast-check');2const arb = cloneArbitrary([1, 2, 3]);3console.log(arb.generate());4const { cloneArbitrary } = require('@dubzzz/fast-check');5const arb = cloneArbitrary(new Date());6console.log(arb.generate());7array (arrayArbitrary: Arbitrary<T[]>) => Arbitrary<T[]>8const { array } = require('@dubzzz/fast-check');9const arb = array(arbInt());10console.log(arb.generate());11arraySet (arrayArbitrary: Arbitrary<T[]>) => Arbitrary<T[]>12const { arraySet } = require('@dubzzz/fast-check');13const arb = arraySet(arbInt());14console.log(arb.generate());15tuple (arbitraries: Arbitrary<T>[]) => Arbitrary<T[]>
Using AI Code Generation
1import { cloneArbitrary } from 'fast-check';2const arb = cloneArbitrary(42);3import { cloneArbitrary } from 'fast-check';4const arb = cloneArbitrary(42);5 at Object.generate (node_modules/fast-check/lib/check/arbitrary/CloneArbitrary.js:22:32)6 at Object.<anonymous> (test.js:6:10)7 at Module._compile (internal/modules/cjs/loader.js:1158:30)8 at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)9 at Module.load (internal/modules/cjs/loader.js:1002:32)10 at Function.Module._load (internal/modules/cjs/loader.js:901:14)11 at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)12@rajsolanki I don't see any issue with your code. Could you please provide a minimal reproducible example (with a package.json) so that I can have a look at it?
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!!