Best JavaScript code snippet using fast-check-monorepo
StreamArbitrary.spec.ts
Source:StreamArbitrary.spec.ts
...23 const biasFactor = 48;24 const { instance: sourceArb } = fakeArbitrary();25 const { instance: mrng } = fakeRandom();26 // Act27 const arb = new StreamArbitrary(sourceArb);28 const out = arb.generate(mrng, biasFactor);29 // Assert30 expect(out.value).toBeInstanceOf(Stream);31 expect(out.hasToBeCloned).toBe(true);32 expect(hasCloneMethod(out.value)).toBe(true);33 });34 it('should not call generate before we pull from the Stream but decide bias', () => {35 // Arrange36 const biasFactor = 48;37 const { instance: sourceArb, generate } = fakeArbitrary();38 const { instance: mrng, nextInt } = fakeRandom();39 // Act40 const arb = new StreamArbitrary(sourceArb);41 arb.generate(mrng, biasFactor).value;42 // Assert43 expect(nextInt).toHaveBeenCalledTimes(1);44 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);45 expect(generate).not.toHaveBeenCalled();46 });47 it('should not check bias again for cloned instances', () => {48 // Arrange49 const biasFactor = 48;50 const { instance: sourceArb } = fakeArbitrary();51 const { instance: mrng, nextInt } = fakeRandom();52 // Act53 const arb = new StreamArbitrary(sourceArb);54 const out = arb.generate(mrng, biasFactor);55 const s1 = out.value;56 const s2 = out.value;57 // Assert58 expect(nextInt).toHaveBeenCalledTimes(1);59 expect(nextInt).toHaveBeenCalledWith(1, biasFactor);60 expect(s2).not.toBe(s1);61 });62 it('should call generate with cloned instance of Random as we pull from the Stream', () => {63 // Arrange64 const numValuesToPull = 5;65 const biasFactor = 48;66 let index = 0;67 const expectedValues = [...Array(numValuesToPull)].map(() => Symbol());68 const { instance: sourceArb, generate } = fakeArbitrary();69 generate.mockImplementation(() => new Value(expectedValues[index++], undefined));70 const { instance: mrng, clone, nextInt } = fakeRandom();71 nextInt.mockReturnValueOnce(1); // for bias72 const { instance: mrngCloned } = fakeRandom();73 clone.mockReturnValueOnce(mrngCloned);74 // Act75 const arb = new StreamArbitrary(sourceArb);76 const stream = arb.generate(mrng, biasFactor).value;77 const values = [...stream.take(numValuesToPull)];78 // Assert79 expect(generate).toHaveBeenCalledTimes(numValuesToPull);80 for (const call of generate.mock.calls) {81 expect(call).toEqual([mrngCloned, biasFactor]);82 }83 expect(values).toEqual(expectedValues);84 });85 it('should call generate with cloned instance of Random specific for each Stream', () => {86 // Arrange87 const numValuesToPullS1 = 5;88 const numValuesToPullS2 = 3;89 const biasFactor = 48;90 const { instance: sourceArb, generate } = fakeArbitrary();91 generate.mockImplementation(() => new Value(Symbol(), undefined));92 const { instance: mrng, clone, nextInt } = fakeRandom();93 nextInt.mockReturnValueOnce(1); // for bias94 const { instance: mrngClonedA } = fakeRandom();95 const { instance: mrngClonedB } = fakeRandom();96 clone.mockReturnValueOnce(mrngClonedA).mockReturnValueOnce(mrngClonedB);97 // Act98 const arb = new StreamArbitrary(sourceArb);99 const out = arb.generate(mrng, biasFactor);100 const s1 = out.value;101 const c1 = s1[Symbol.iterator]();102 for (let i = 0; i !== numValuesToPullS1; ++i) {103 const next = c1.next();104 expect(next.done).toBe(false);105 }106 const s2 = out.value;107 const c2 = s2[Symbol.iterator]();108 for (let i = 0; i !== numValuesToPullS2; ++i) {109 const next = c2.next();110 expect(next.done).toBe(false);111 }112 c1.next();113 // Assert114 expect(generate).toHaveBeenCalledTimes(numValuesToPullS1 + numValuesToPullS2 + 1);115 const calls = generate.mock.calls;116 for (let i = 0; i !== numValuesToPullS1; ++i) {117 const call = calls[i];118 expect(call).toEqual([mrngClonedA, biasFactor]);119 }120 for (let i = 0; i !== numValuesToPullS2; ++i) {121 const call = calls[numValuesToPullS1 + i];122 expect(call).toEqual([mrngClonedB, biasFactor]);123 }124 expect(calls[numValuesToPullS1 + numValuesToPullS2]).toEqual([mrngClonedA, biasFactor]);125 });126 it('should only print pulled values on print', () =>127 fc.assert(128 fc.property(fc.array(fc.integer()), (expectedValues) => {129 // Arrange130 const biasFactor = 48;131 let index = 0;132 const { instance: sourceArb, generate } = fakeArbitrary<number>();133 generate.mockImplementation(() => new Value(expectedValues[index++], undefined));134 const { instance: mrng, clone, nextInt } = fakeRandom();135 nextInt.mockReturnValueOnce(2); // for no bias136 const { instance: mrngCloned } = fakeRandom();137 clone.mockReturnValueOnce(mrngCloned);138 const fakeStringify = (v: unknown) => '<' + String(v) + '>';139 const stringify = jest.spyOn(StringifyMock, 'stringify');140 stringify.mockImplementation(fakeStringify);141 // Act142 const arb = new StreamArbitrary(sourceArb);143 const stream = arb.generate(mrng, biasFactor).value;144 const values = [...stream.take(expectedValues.length)];145 // Assert146 expect(values).toEqual(expectedValues);147 expect(String(stream)).toEqual(`Stream(${expectedValues.map(fakeStringify).join(',')}â¦)`);148 expect(stringify).toHaveBeenCalledTimes(expectedValues.length);149 expect(generate).toHaveBeenCalledTimes(expectedValues.length);150 if (expectedValues.length > 0) {151 expect(generate).toHaveBeenCalledWith(mrngCloned, undefined);152 }153 })154 ));155 it('should create independant Stream even in terms of toString', () => {156 // Arrange157 const biasFactor = 48;158 let index = 0;159 const { instance: sourceArb, generate } = fakeArbitrary<number>();160 generate.mockImplementation(() => new Value(index++, undefined));161 const { instance: mrng, clone, nextInt } = fakeRandom();162 nextInt.mockReturnValueOnce(2); // for no bias163 const { instance: mrngCloned } = fakeRandom();164 clone.mockReturnValueOnce(mrngCloned);165 const stringify = jest.spyOn(StringifyMock, 'stringify');166 stringify.mockImplementation((v) => '<' + String(v) + '>');167 // Act168 const arb = new StreamArbitrary(sourceArb);169 const out = arb.generate(mrng, biasFactor);170 const stream1 = out.value;171 const stream2 = out.value;172 const values1 = [...stream1.take(2)];173 const values2 = [...stream2.take(3)];174 const values1Bis = [...stream1.take(2)];175 // Assert176 expect(values1).toEqual([0, 1]);177 expect(values2).toEqual([2, 3, 4]);178 expect(values1Bis).toEqual([5, 6]);179 expect(String(stream1)).toEqual(`Stream(<0>,<1>,<5>,<6>â¦)`);180 expect(String(stream2)).toEqual(`Stream(<2>,<3>,<4>â¦)`);181 expect(stringify).toHaveBeenCalledTimes(7);182 expect(generate).toHaveBeenCalledTimes(7);183 });184 });185 describe('canShrinkWithoutContext', () => {186 function* infiniteG() {187 yield 1;188 }189 it.each`190 data | description191 ${Stream.nil()} | ${'empty stream'}192 ${Stream.of(1, 5, 6, 74, 4)} | ${'finite stream'}193 ${new Stream(infiniteG())} | ${'infinite stream'}194 `('should return false for any Stream whatever the size ($description)', ({ data }) => {195 // Arrange196 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();197 // Act198 const arb = new StreamArbitrary(sourceArb);199 const out = arb.canShrinkWithoutContext(data);200 // Assert201 expect(out).toBe(false);202 expect(canShrinkWithoutContext).not.toHaveBeenCalled();203 });204 it('should return false even for its own values', () => {205 // Arrange206 const { instance: sourceArb, canShrinkWithoutContext } = fakeArbitrary();207 const { instance: mrng } = fakeRandom();208 // Act209 const arb = new StreamArbitrary(sourceArb);210 const g = arb.generate(mrng, undefined);211 const out = arb.canShrinkWithoutContext(g.value);212 // Assert213 expect(out).toBe(false);214 expect(canShrinkWithoutContext).not.toHaveBeenCalled();215 });216 });217 describe('shrink', () => {218 it('should always shrink to nil', () => {219 // Arrange220 const { instance: sourceArb, generate, shrink } = fakeArbitrary<number>();221 generate.mockReturnValue(new Value(0, undefined));222 const { instance: mrng } = fakeRandom();223 // Act224 const arb = new StreamArbitrary(sourceArb);225 const { value, context } = arb.generate(mrng, undefined);226 const pullValues = [...value.take(50)];227 const shrinks = [...arb.shrink(value, context)];228 // Assert229 expect(pullValues).toBeDefined();230 expect(shrinks).toHaveLength(0);231 expect(shrink).not.toHaveBeenCalled();232 });233 });234});235describe('StreamArbitrary (integration)', () => {236 const sourceArb = new FakeIntegerArbitrary();237 const isEqual = (s1: Stream<number>, s2: Stream<number>) => {238 expect([...cloneIfNeeded(s1).take(10)]).toEqual([...cloneIfNeeded(s2).take(10)]);239 };240 const isCorrect = (value: Stream<number>) =>241 value instanceof Stream && [...value.take(10)].every((v) => sourceArb.canShrinkWithoutContext(v));242 const streamBuilder = () => new StreamArbitrary(sourceArb);243 it('should produce the same values given the same seed', () => {244 assertProduceSameValueGivenSameSeed(streamBuilder, { isEqual });245 });246 it('should only produce correct values', () => {247 assertProduceCorrectValues(streamBuilder, isCorrect);248 });...
infiniteStream.spec.ts
Source:infiniteStream.spec.ts
...6 jest.restoreAllMocks();7}8beforeEach(beforeEachHook);9describe('infiniteStream', () => {10 it('should instantiate StreamArbitrary(arb, numValues) for infiniteStream(arb)', () => {11 // Arrange12 const { instance: sourceArbitrary } = fakeArbitrary();13 const { instance } = fakeArbitrary();14 const StreamArbitrary = jest.spyOn(StreamArbitraryMock, 'StreamArbitrary');15 StreamArbitrary.mockImplementation(() => instance as StreamArbitraryMock.StreamArbitrary<unknown>);16 // Act17 const arb = infiniteStream(sourceArbitrary);18 // Assert19 expect(StreamArbitrary).toHaveBeenCalledWith(sourceArbitrary);20 expect(arb).toBe(instance);21 });...
infiniteStream.ts
Source:infiniteStream.ts
...11 * @remarks Since 1.8.012 * @public13 */14function infiniteStream<T>(arb: Arbitrary<T>): Arbitrary<Stream<T>> {15 return new StreamArbitrary(arb);16}...
Using AI Code Generation
1const { StreamArbitrary } = require('fast-check-monorepo');2const { stream } = StreamArbitrary;3const { take } = require('rxjs/operators');4const { of } = require('rxjs');5const { toArray } = require('rxjs/operators');6const { map } = require('rxjs/operators');7const { concat } = require('rxjs/operators');8const { from } = require('rxjs');9const { mergeMap } = require('rxjs/operators');10const { merge } = require('rxjs');11const { generate } = require('rxjs');12const { zip } = require('rxjs');13const { delay } = require('rxjs/operators');14const { interval } = require('rxjs');15const { filter } = require('rxjs/operators');16const { takeWhile } = require('rxjs/operators');17const { takeUntil } = require('rxjs/operators');18const { skip } = require('rxjs/operators');19const { skipWhile } = require('rxjs/operators');20const { skipUntil } = require('rxjs/operators');21const { distinct } = require('rxjs/operators');22const { distinctUntilChanged } = require('rxjs/operators');23const { scan } = require('rxjs/operators');24const { reduce } = require('rxjs/operators');25const { repeat } = require('rxjs/operators');26const { repeatWhen } = require('rxjs/operators');27const { repeatUntil } = require('rxjs/operators');28const { repeatWhile } = require('rxjs/operators');29const { startWith } = require('rxjs/operators');30const { endWith } = require('rxjs/operators');31const { count } = require('rxjs/operators');32const { min } = require('rxjs/operators');33const { max } = require('rxjs/operators');34const { toArray: toArray$1 } = require('rxjs/operators');35const { toPromise } = require('rxjs/operators');36const { subscribeOn } = require('rxjs/operators');37const { observeOn } = require('rxjs/operators');38const { mapTo } = require('rxjs/operators');39const { pluck } = require('rxjs/operators');40const { tap } = require('rxjs/operators');41const { catchError } = require('rxjs/operators');42const { mergeMapTo } = require('rxjs/operators');43const { concatMap } = require('rxjs/operators');
Using AI Code Generation
1const { StreamArbitrary } = require('fast-check');2const { stream } = require('fast-check/lib/types/stream/Stream.js');3const { Random } = require('fast-check/lib/random/generator/Random.js');4const random = new Random();5const streamArbitrary = StreamArbitrary.arbitraryFor(6 StreamArbitrary.nat(),7);8const s = streamArbitrary.generate();9console.log(s);10'use strict';11const { Arbitrary } = require('../check/arbitrary/definition/Arbitrary.js');12const { Stream } = require('./Stream.js');13class StreamArbitrary extends Arbitrary {14 static arbitraryFor(arb, stream, random) {15 return new StreamArbitrary(arb, stream, random);16 }17 constructor(arb, stream, random) {18 super();19 this.arb = arb;20 this.stream = stream;21 this.random = random;22 }23 generate(mrng) {24 const self = this;25 const generator = function* () {26 let mrng = self.random;27 while (true) {28 const g = self.arb.generate(mrng);29 mrng = g[1];30 yield g[0];31 }32 };33 return this.stream(generator());34 }35}36module.exports = { StreamArbitrary };37'use strict';38class Stream {39 constructor(generator) {40 this._generator = generator;41 this._current = null;42 this._cache = undefined;43 }44 get head() {45 if (this._current === null) {46 this._current = this._generator.next();47 }48 return this._current.value;49 }50 get tail() {51 if (this._current === null) {52 this._current = this._generator.next();53 }54 if (this._current.done) {55 throw new Error('Cannot access tail of an empty stream');56 }57 this._current = null;58 return this;59 }60 get isEmpty() {61 if (this._current === null) {
Using AI Code Generation
1const { StreamArbitrary } = require('fast-check');2const { Stream } = require('stream');3const streamArbitrary = StreamArbitrary.string();4const stream = streamArbitrary.generate(mrng);5stream.pipe(process.stdout);6{7 "scripts": {8 },9 "dependencies": {10 }11}12| Packages / Softwares | Version(s) |
Using AI Code Generation
1const stream = require('stream');2const { StreamArbitrary } = require('fast-check');3const streamArbitrary = new StreamArbitrary();4const { string: stringArbitrary } = require('fast-check');5const stringStream = streamArbitrary.stream(stringArbitrary);6stringStream.on('data', (data) => {7 console.log(data);8});9stringStream.on('end', () => {10 console.log('end');11});12stringStream.on('error', (err) => {13 console.log('error', err);14});15{16 "scripts": {17 },18 "dependencies": {19 }20}
Using AI Code Generation
1const { StreamArbitrary } = require('fast-check');2const { createReadStream } = require('fs');3const { createGzip } = require('zlib');4const stream = createReadStream('test.js').pipe(createGzip());5const arb = StreamArbitrary.fromStream(stream);6const fc = require('fast-check');7fc.assert(8 fc.property(arb, (s) => {9 return true;10 })11);
Using AI Code Generation
1const { StreamArbitrary } = require('fast-check');2const { stream } = require('fast-check');3const stream1 = stream(() => 1);4const stream2 = stream(StreamArbitrary);5const stream3 = stream(new StreamArbitrary());6const stream4 = stream(() => new StreamArbitrary());7const stream5 = stream(() => 1);8const stream6 = stream(StreamArbitrary);9const stream7 = stream(new StreamArbitrary());10const stream8 = stream(() => new StreamArbitrary());11const stream9 = stream(() => 1);12const stream10 = stream(StreamArbitrary);13const stream11 = stream(new StreamArbitrary());14const stream12 = stream(() => new StreamArbitrary());15const stream13 = stream(() => 1);16const stream14 = stream(StreamArbitrary);17const stream15 = stream(new StreamArbitrary());
Using AI Code Generation
1const { StreamArbitrary, stream } = require('fast-check')2const Stream = require('stream')3const streamArbitrary = StreamArbitrary.integer()4const streamOfRandomNumbers = stream(streamArbitrary)5const summingStream = new Stream.Transform({6 transform(chunk, encoding, callback) {7 callback(null, this.sum)8 }9})10streamOfRandomNumbers.pipe(summingStream)11summingStream.on('data', data => {12 console.log('summingStream: ', data)13})14streamOfRandomNumbers.on('data', data => {
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!!