Best JavaScript code snippet using fast-check-monorepo
record.spec.ts
Source:record.spec.ts
1import fc from 'fast-check';2import { record, RecordConstraints } from '../../../src/arbitrary/record';3import { Arbitrary } from '../../../src/check/arbitrary/definition/Arbitrary';4import { FakeIntegerArbitrary, fakeArbitrary } from './__test-helpers__/ArbitraryHelpers';5import {6 assertProduceCorrectValues,7 assertProduceSameValueGivenSameSeed,8 assertProduceValuesShrinkableWithoutContext,9 assertShrinkProducesSameValueWithoutInitialContext,10} from './__test-helpers__/ArbitraryAssertions';11import * as PartialRecordArbitraryBuilderMock from '../../../src/arbitrary/_internals/builders/PartialRecordArbitraryBuilder';12function beforeEachHook() {13 jest.resetModules();14 jest.restoreAllMocks();15 fc.configureGlobal({ beforeEach: beforeEachHook });16}17beforeEach(beforeEachHook);18describe('record', () => {19 const keyArb: fc.Arbitrary<any> = fc20 .tuple(fc.string(), fc.boolean())21 .map(([name, symbol]) => (symbol ? Symbol.for(name) : name));22 it('should call buildPartialRecordArbitrary with keys=undefined when no constraints on keys', () =>23 fc.assert(24 fc.property(25 fc.uniqueArray(keyArb, { minLength: 1 }),26 fc.constantFrom(...([undefined, {}] as const)),27 (keys, constraints) => {28 // Arrange29 const recordModel: Record<string | symbol, Arbitrary<any>> = {};30 for (const k of keys) {31 const { instance } = fakeArbitrary();32 recordModel[k] = instance;33 }34 const { instance } = fakeArbitrary<any>();35 const buildPartialRecordArbitrary = jest.spyOn(36 PartialRecordArbitraryBuilderMock,37 'buildPartialRecordArbitrary'38 );39 buildPartialRecordArbitrary.mockReturnValue(instance);40 // Act41 const arb = constraints !== undefined ? record(recordModel, constraints) : record(recordModel);42 // Assert43 expect(arb).toBe(instance);44 expect(buildPartialRecordArbitrary).toHaveBeenCalledTimes(1);45 expect(buildPartialRecordArbitrary).toHaveBeenCalledWith(recordModel, undefined);46 }47 )48 ));49 it('should call buildPartialRecordArbitrary with keys=[] when constraints defines withDeletedKeys=true', () =>50 fc.assert(51 fc.property(fc.uniqueArray(keyArb, { minLength: 1 }), (keys) => {52 // Arrange53 const recordModel: Record<string | symbol, Arbitrary<any>> = {};54 for (const k of keys) {55 const { instance } = fakeArbitrary();56 recordModel[k] = instance;57 }58 const { instance } = fakeArbitrary<any>();59 const buildPartialRecordArbitrary = jest.spyOn(60 PartialRecordArbitraryBuilderMock,61 'buildPartialRecordArbitrary'62 );63 buildPartialRecordArbitrary.mockReturnValue(instance);64 // Act65 const arb = record(recordModel, { withDeletedKeys: true });66 // Assert67 expect(arb).toBe(instance);68 expect(buildPartialRecordArbitrary).toHaveBeenCalledTimes(1);69 expect(buildPartialRecordArbitrary).toHaveBeenCalledWith(recordModel, []);70 })71 ));72 it('should call buildPartialRecordArbitrary with keys=requiredKeys when constraints defines valid requiredKeys', () =>73 fc.assert(74 fc.property(fc.uniqueArray(keyArb, { minLength: 1 }), fc.func(fc.boolean()), (keys, isRequired) => {75 // Arrange76 const recordModel: Record<string | symbol, Arbitrary<any>> = {};77 const requiredKeys: any[] = [];78 for (const k of keys) {79 const { instance } = fakeArbitrary();80 Object.defineProperty(recordModel, k, {81 value: instance,82 configurable: true,83 enumerable: true,84 writable: true,85 });86 if (isRequired(k)) {87 requiredKeys.push(k);88 }89 }90 const { instance } = fakeArbitrary<any>();91 const buildPartialRecordArbitrary = jest.spyOn(92 PartialRecordArbitraryBuilderMock,93 'buildPartialRecordArbitrary'94 );95 buildPartialRecordArbitrary.mockReturnValue(instance);96 // Act97 const arb = record(recordModel, { requiredKeys });98 // Assert99 expect(arb).toBe(instance);100 expect(buildPartialRecordArbitrary).toHaveBeenCalledTimes(1);101 expect(buildPartialRecordArbitrary).toHaveBeenCalledWith(recordModel, requiredKeys);102 })103 ));104 it('should reject configurations specifying non existing keys as required', () =>105 fc.assert(106 fc.property(fc.uniqueArray(keyArb, { minLength: 1 }), keyArb, (keys, requiredKey) => {107 // Arrange108 fc.pre(!keys.includes(requiredKey));109 const recordModel: Record<string | symbol, Arbitrary<any>> = {};110 for (const k of keys) {111 const { instance } = fakeArbitrary();112 recordModel[k] = instance;113 }114 // Act / Assert115 expect(() =>116 record(recordModel, {117 requiredKeys: [requiredKey],118 })119 ).toThrowError();120 })121 ));122 it('should reject configurations specifying both requiredKeys and withDeletedKeys (even undefined)', () =>123 fc.assert(124 fc.property(125 fc.uniqueArray(fc.record({ name: keyArb, required: fc.boolean() }), {126 minLength: 1,127 selector: (entry) => entry.name,128 }),129 fc.option(fc.constant(true), { nil: undefined }),130 fc.option(fc.boolean(), { nil: undefined }),131 (keys, withRequiredKeys, withDeletedKeys) => {132 // Arrange133 const recordModel: Record<string | symbol, Arbitrary<any>> = {};134 for (const k of keys) {135 const { instance } = fakeArbitrary();136 recordModel[k.name] = instance;137 }138 // Act / Assert139 expect(() =>140 record(recordModel, {141 requiredKeys: withRequiredKeys ? keys.filter((k) => k.required).map((k) => k.name) : undefined,142 withDeletedKeys: withDeletedKeys,143 })144 ).toThrowError();145 }146 )147 ));148 it('should accept empty keys configurations with empty requiredKeys', () => {149 // Arrange150 const recordModel: Record<string | symbol, Arbitrary<any>> = {};151 // Act / Assert152 expect(() => record(recordModel, { requiredKeys: [] })).not.toThrowError();153 });154 it.each`155 keyName156 ${'constructor'}157 ${'toString'}158 ${'__proto__'}159 `('should reject configurations specifying non own properties ($keyName) as requiredKeys', ({ keyName }) => {160 // Arrange161 const recordModel: Record<string | symbol, Arbitrary<any>> = {};162 // Act / Assert163 expect(() => record(recordModel, { requiredKeys: [keyName] })).toThrowError();164 });165 it('should reject configurations specifying non enumerable properties as requiredKeys', () => {166 // Arrange167 const keyName = 'k';168 const recordModel: Record<string | symbol, Arbitrary<any>> = {};169 Object.defineProperty(recordModel, keyName, { value: fc.boolean(), enumerable: false });170 // Act / Assert171 expect(() => record(recordModel, { requiredKeys: [keyName] })).toThrowError();172 });173});174describe('record (integration)', () => {175 type Meta = { key: any; valueStart: number; kept: boolean };176 type Extra = [Meta[], RecordConstraints<any>];177 const keyArb: fc.Arbitrary<any> = fc178 .tuple(fc.string(), fc.boolean())179 .map(([name, symbol]) => (symbol ? Symbol.for(name) : name));180 const metaArbitrary = fc.uniqueArray(181 fc.record({182 key: keyArb,183 valueStart: fc.nat(1000),184 kept: fc.boolean(),185 }),186 { selector: (entry) => entry.key }187 );188 const constraintsArbitrary = fc.oneof(189 fc.record({ withDeletedKeys: fc.boolean() }, { requiredKeys: [] }),190 fc.record({ withRequiredKeys: fc.constant<true>(true) }, { requiredKeys: [] })191 );192 const extraParameters: fc.Arbitrary<Extra> = fc193 .tuple(metaArbitrary, constraintsArbitrary)194 .map(([metas, constraintsMeta]: [Meta[], { withDeletedKeys?: boolean; withRequiredKeys?: true }]) => {195 if ('withRequiredKeys' in constraintsMeta) {196 return [metas, { requiredKeys: metas.filter((m) => m.kept).map((m) => m.key) }] as [Meta[], RecordConstraints];197 }198 return [metas, constraintsMeta] as [Meta[], RecordConstraints];199 });200 const isCorrect = (value: any, extra: Extra) => {201 const [metas, constraints] = extra;202 // Rq: getOwnPropertyNames will also get non enumerable properties, but there are none in our case203 for (const k of [...Object.getOwnPropertyNames(value), ...Object.getOwnPropertySymbols(value)]) {204 // generated object should not have more keys205 if (metas.findIndex((m) => m.key === k) === -1) return false;206 }207 for (const m of metas) {208 // optional keys can be missing in the generated instance209 if (210 'withDeletedKeys' in constraints &&211 constraints.withDeletedKeys === true &&212 !Object.prototype.hasOwnProperty.call(value, m.key)213 ) {214 continue;215 }216 if (217 'requiredKeys' in constraints &&218 constraints.requiredKeys !== undefined &&219 !constraints.requiredKeys.includes(m.key) &&220 !Object.prototype.hasOwnProperty.call(value, m.key)221 ) {222 continue;223 }224 // values are associated to the right key (if key required)225 if (typeof value[m.key] !== 'number') return false;226 if (value[m.key] < m.valueStart) return false;227 if (value[m.key] > m.valueStart + 10) return false;228 }229 return true;230 };231 const recordBuilder = (extra: Extra) => {232 const [metas, constraints] = extra;233 const recordModel: Record<string | symbol, Arbitrary<number>> = {};234 for (const m of metas) {235 const instance = new FakeIntegerArbitrary(m.valueStart, 10);236 Object.defineProperty(recordModel, m.key, {237 value: instance,238 configurable: true,239 enumerable: true,240 writable: true,241 });242 }243 return record(recordModel, constraints);244 };245 it('should produce the same values given the same seed', () => {246 assertProduceSameValueGivenSameSeed(recordBuilder, { extraParameters });247 });248 it('should only produce correct values', () => {249 assertProduceCorrectValues(recordBuilder, isCorrect, { extraParameters });250 });251 it('should produce values seen as shrinkable without any context (if underlyings do)', () => {252 assertProduceValuesShrinkableWithoutContext(recordBuilder, { extraParameters });253 });254 it('should be able to shrink to the same values without initial context (if underlyings do)', () => {255 assertShrinkProducesSameValueWithoutInitialContext(recordBuilder, { extraParameters });256 });...
Using AI Code Generation
1const { metaArbitrary } = require("fast-check");2const { arbitrary: stringArbitrary } = require("fast-check/lib/types/arbitrary/StringArbitrary");3const { arbitrary: integerArbitrary } = require("fast-check/lib/types/arbitrary/IntegerArbitrary");4const { arbitrary: recordArbitrary } = require("fast-check/lib/types/arbitrary/RecordArbitrary");5const { arbitrary: arrayArbitrary } = require("fast-check/lib/types/arbitrary/ArrayArbitrary");6const { arbitrary: tupleArbitrary } = require("fast-check/lib/types/arbitrary/TupleArbitrary");7const { arbitrary: objectArbitrary } = require("fast-check/lib/types/arbitrary/ObjectArbitrary");8const { arbitrary: constantArbitrary } = require("fast-check/lib/types/arbitrary/ConstantArbitrary");9const { arbitrary: setArbitrary } = require("fast-check/lib/types/arbitrary/SetArbitrary");10const { arbitrary: mapArbitrary } = require("fast-check/lib/types/arbitrary/MapArbitrary");11const { arbitrary: dateArbitrary } = require("fast-check/lib/types/arbitrary/DateArbitrary");12const { arbitrary: bigIntArbitrary } = require("fast-check/lib/types/arbitrary/BigIntArbitrary");13const { arbitrary: booleanArbitrary } = require("fast-check/lib/types/arbitrary/BooleanArbitrary");14const { arbitrary: symbolArbitrary } = require("fast-check/lib/types/arbitrary/SymbolArbitrary");15const { arbitrary: floatArbitrary } = require("fast-check/lib/types/arbitrary/FloatArbitrary");16const { arbitrary: doubleArbitrary } = require("fast-check/lib/types/arbitrary/DoubleArbitrary");17const { arbitrary: oneofArbitrary } = require("fast-check/lib/types/arbitrary/OneOfArbitrary");18const { arbitrary: optionArbitrary } = require("fast-check/lib/types/arbitrary/OptionArbitrary");19const { arbitrary: nullArbitrary } = require("fast-check/lib/types/arbitrary/NullArbitrary");20const { arbitrary: undefinedArbitrary } = require("fast-check/lib/types/arbitrary/UndefinedArbitrary");21const { arbitrary: functionArbitrary } = require("fast-check/lib/types/arbitrary/FunctionArbitrary");22const { arbitrary: regexpArbitrary } = require("fast-check/lib/types/arbitrary/RegExpArbitrary");23const { arbitrary: unicodeStringArbitrary } = require("fast-check/lib/types/arbitrary/UnicodeStringArbitrary");
Using AI Code Generation
1const fc = require('fast-check');2const { metaArbitrary } = require('fast-check-monorepo');3const myArbitrary = fc.integer();4const myMetaArbitrary = metaArbitrary(myArbitrary);5const fc = require('fast-check');6const { metaArbitrary } = require('fast-check');7const myArbitrary = fc.integer();8const myMetaArbitrary = metaArbitrary(myArbitrary);9The error is thrown by the metaArbitrary function, which is being imported from fast-check. The code for this function is:10function metaArbitrary(arb) {11 return new fc.Arbitrary(function () {12 return arb.generate(mrng);13 }, arb.canShrinkWithoutContext ? undefined : function (v) {14 return arb.shrink(v, mrng);15 });16}17module.exports = {18}
Using AI Code Generation
1const { metaArbitrary } = require('fast-check');2const { generate } = require('astring');3const { parse } = require('acorn');4const { generate: generateEsprima } = require('escodegen');5const { generate: generateEscodegen } = require('escodegen');6const { generate: generateEscodegen2 } = require('escodegen');7const { generate: generateEscodegen3 } = require('escodegen');8const { generate: generateEscodegen4 } = require('escodegen');9const { generate: generateEscodegen5 } = require('escodegen');10const { generate: generateEscodegen6 } = require('escodegen');11const { generate: generateEscodegen7 } = require('escodegen');12const { generate: generateEscodegen8 } = require('escodegen');13const { generate: generateEscodegen9 } = require('escodegen');14const { generate: generateEscodegen10 } = require('escodegen');15const { generate: generateEscodegen11 } = require('escodegen');16const { generate: generateEscodegen12 } = require('escodegen');17const { generate: generateEscodegen13 } = require('escodegen');18const { generate: generateEscodegen14 } = require('escodegen');19const { generate: generateEscodegen15 } = require('escodegen');20const { generate: generateEscodegen16 } = require('escodegen');21const { generate: generateEscodegen17 } = require('escodegen');22const { generate: generateEscodegen18 } = require('escodegen');23const { generate: generateEscodegen19 } = require('escodegen');24const { generate: generateEscodegen20 } = require('escodegen');25const { generate: generateEscodegen21 } = require('escodegen');26const { generate: generateEscodegen22 } = require('escodegen');27const { generate: generateEscodegen23 } = require('escodegen');28const { generate: generateEscodegen24 } = require('escodegen');29const { generate: generateEscodegen25 } = require('escodegen');30const { generate: generateEscodegen26 } = require('escodegen');31const { generate: generateEscodegen27 } = require('escodegen');32const { generate: generateEscodegen28 } = require('escodegen');33const { generate: generateEscodegen29 } = require('escodegen');34const { generate: generateEscodegen30 } = require('escodegen');35const {
Using AI Code Generation
1const { metaArbitrary } = require('fast-check');2const { string } = require('fast-check');3const { metaObject } = require('fast-check');4const meta = metaObject({foo: string(), bar: string()});5const arbitrary = metaArbitrary(meta);6arbitrary.sample(10).forEach(console.log);7const { metaArbitrary } = require('fast-check');8const { string } = require('fast-check');9const { metaArray } = require('fast-check');10const { metaObject } = require('fast-check');11const meta = metaObject({12 foo: metaArray(string()),13 bar: metaObject({14 baz: string(),15 qux: string(),16 }),17});18const arbitrary = metaArbitrary(meta);19arbitrary.sample(10).forEach(console.log);
Using AI Code Generation
1const fc = require('fast-check');2const { metaArbitrary } = require('fast-check-monorepo');3fc.assert(4 fc.property(5 metaArbitrary({6 model: {7 a: fc.nat(),8 b: fc.nat(),9 },10 generate: ({ model }) => {11 return {12 };13 },14 shrink: ({ model }) => {15 return fc.shrinkObject({16 });17 },18 run: ({ model, data }) => {19 return data.a + data.b;20 },21 check: ({ model, output }) => {22 return model.a + model.b === output;23 },24 }),25 ({ model, data }) => {26 return data.a + data.b;27 },28 ({ model, output }) => {29 return model.a + model.b === output;30 }31);
Using AI Code Generation
1const {metaArbitrary} = require('fast-check-monorepo');2const myArbitrary = metaArbitrary({3});4describe('myArbitrary', () => {5});6const {metaArbitrary} = require('fast-check-monorepo');7const myArbitrary = metaArbitrary({8});9describe('myArbitrary', () => {10});11const {metaArbitrary} = require('fast-check-monorepo');12const myArbitrary = metaArbitrary({13});14describe('myArbitrary', () => {15});16const {metaArbitrary} = require('fast-check-monorepo');17const myArbitrary = metaArbitrary({18});19describe('myArbitrary', () => {20});21import {metaArbitrary} from 'fast-check-monorepo';22const myArbitrary = metaArbitrary({23});24describe('myArbitrary', () => {25});26function metaArbitrary<T>(27): Arbitrary<T>;
Using AI Code Generation
1const fc = require("fast-check");2const { metaArbitrary } = require("fast-check-monorepo");3const meta = fc.metaArbitrary();4console.log(meta);5{6 "scripts": {7 },8 "dependencies": {9 }10}
Using AI Code Generation
1import { metaArbitrary } from 'fast-check';2import { Arbitrary } from 'fast-check';3const arb = metaArbitrary(Arbitrary, 'number', 1, 10);4import { metaArbitrary } from 'fast-check';5import { Arbitrary } from 'fast-check';6const arb = metaArbitrary(Arbitrary, 'number', 1, 10);7import { metaArbitrary } from 'fast-check';8import { Arbitrary } from 'fast-check';9const arb = metaArbitrary(Arbitrary, 'number', 1, 10);10import { metaArbitrary } from 'fast-check';11import { Arbitrary } from 'fast-check';12const arb = metaArbitrary(Arbitrary, 'number', 1, 10);13import { metaArbitrary } from 'fast-check';14import { Arbitrary } from 'fast-check';15const arb = metaArbitrary(Arbitrary, 'number', 1, 10);16import { metaArbitrary } from 'fast-check';17import { Arbitrary } from 'fast-check';18const arb = metaArbitrary(Arbitrary, 'number', 1, 10);19import { metaArbitrary } from 'fast-check';20import { Arbitrary } from 'fast-check';21const arb = metaArbitrary(Arbitrary, 'number', 1, 10);22import
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!!