Best JavaScript code snippet using fast-check-monorepo
StreamArbitrary.spec.ts
Source:StreamArbitrary.spec.ts
1import * as fc from 'fast-check';2import { StreamArbitrary } from '../../../../src/arbitrary/_internals/StreamArbitrary';3import { Value } from '../../../../src/check/arbitrary/definition/Value';4import { cloneIfNeeded, hasCloneMethod } from '../../../../src/check/symbols';5import { Stream } from '../../../../src/stream/Stream';6import {7 assertProduceCorrectValues,8 assertProduceSameValueGivenSameSeed,9} from '../__test-helpers__/ArbitraryAssertions';10import { FakeIntegerArbitrary, fakeArbitrary } from '../__test-helpers__/ArbitraryHelpers';11import { fakeRandom } from '../__test-helpers__/RandomHelpers';12import * as StringifyMock from '../../../../src/utils/stringify';13function beforeEachHook() {14 jest.resetModules();15 jest.restoreAllMocks();16 fc.configureGlobal({ beforeEach: beforeEachHook });17}18beforeEach(beforeEachHook);19describe('StreamArbitrary', () => {20 describe('generate', () => {21 it('should produce a cloneable instance of Stream', () => {22 // Arrange23 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 });...
Using AI Code Generation
1const { values1Bis } = require('fast-check-monorepo');2console.log(values1Bis());3{4 "scripts": {5 },6 "dependencies": {7 }8}9To ensure that the library works with different versions of the JavaScript engine (V8)10To ensure that the library works with different versions of the JavaScript engine of the browser (V8)11To ensure that the library works with different versions of the JavaScript engine of the browser extensions (V8)
Using AI Code Generation
1const values1Bis = require('./index.js').values1Bis2const values = values1Bis(10)3console.log(values)4const values1Bis = function (n) {5}6module.exports = { values1Bis }7I have tried to use the require method and I have tried to use the import method but I always get the same error:8I have tried to use module.exports = values1Bis instead of module.exports = { values1Bis } but I still get the same error:9I have also tried to use module.exports = { values1Bis: values1Bis } instead of module.exports = { values1Bis } but I still get the same error:10I have also tried to use export default values1Bis instead of module.exports = { values1Bis } but I still get the same error:11I have also tried to use export default { values1Bis } instead of module.exports = { values1Bis } but I still get the same error:12I have also tried to use export default { values1Bis: values1Bis } instead of module.exports = { values1Bis } but I still get the same error:
Using AI Code Generation
1const { values1Bis } = require("fast-check-monorepo");2console.log(values1Bis());3const { values2Bis } = require("fast-check-monorepo");4console.log(values2Bis());5const { values1Bis } = require("fast-check-monorepo");6console.log(values1Bis());
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!!