Best JavaScript code snippet using fast-check-monorepo
prop.spec.ts
Source:prop.spec.ts
...5import type { PropType, ExtractPropTypes } from 'vue'6describe('buildProp', () => {7 it('Only type', () => {8 expectTypeOf(9 buildProp({10 type: definePropType<'a' | 'b'>(String),11 })12 ).toEqualTypeOf<{13 readonly type: PropType<'a' | 'b'>14 readonly required: false15 readonly default: undefined16 readonly validator: ((val: unknown) => boolean) | undefined17 [propKey]: true18 }>()19 })20 it('Only values', () => {21 expectTypeOf(22 buildProp({23 values: [1, 2, 3, 4],24 } as const)25 ).toEqualTypeOf<{26 readonly type: PropType<1 | 2 | 3 | 4>27 readonly required: false28 readonly default: undefined29 readonly validator: ((val: unknown) => boolean) | undefined30 [propKey]: true31 }>()32 })33 it('Type and values', () => {34 expectTypeOf(35 buildProp({36 type: definePropType<number[]>(Array),37 values: [1, 2, 3, 4],38 } as const)39 ).toEqualTypeOf<{40 readonly type: PropType<1 | 2 | 3 | 4 | number[]>41 readonly required: false42 readonly default: undefined43 readonly validator: ((val: unknown) => boolean) | undefined44 [propKey]: true45 }>()46 })47 it('Values and validator', () => {48 expectTypeOf(49 buildProp({50 values: ['a', 'b', 'c'],51 validator: (val: unknown): val is number => typeof val === 'number',52 } as const)53 ).toEqualTypeOf<{54 readonly type: PropType<number | 'a' | 'b' | 'c'>55 readonly required: false56 readonly default: undefined57 readonly validator: ((val: unknown) => boolean) | undefined58 [propKey]: true59 }>()60 })61 it('Values and required', () => {62 expectTypeOf(63 buildProp({64 values: ['a', 'b', 'c'],65 required: true,66 } as const)67 ).toEqualTypeOf<{68 readonly type: PropType<'a' | 'b' | 'c'>69 readonly required: true70 readonly default?: undefined71 readonly validator: ((val: unknown) => boolean) | undefined72 [propKey]: true73 }>()74 })75 it('Value and default', () => {76 expectTypeOf(77 buildProp({78 values: ['a', 'b', 'c'],79 required: false,80 default: 'b',81 } as const)82 ).toEqualTypeOf<{83 readonly type: PropType<'a' | 'b' | 'c'>84 readonly required: false85 readonly default: 'b'86 readonly validator: ((val: unknown) => boolean) | undefined87 [propKey]: true88 }>()89 })90 it('Type and Array default value', () => {91 expectTypeOf(92 buildProp({93 type: definePropType<string[]>(Array),94 default: () => mutable(['a', 'b'] as const),95 } as const)96 ).toEqualTypeOf<{97 readonly type: PropType<string[]>98 readonly required: false99 readonly default: ['a', 'b']100 readonly validator: ((val: unknown) => boolean) | undefined101 [propKey]: true102 }>()103 })104 it('Type and Object default value', () => {105 interface Options {106 key: string107 }108 expectTypeOf(109 buildProp({110 type: definePropType<Options>(Object),111 default: () => mutable({ key: 'value' } as const),112 } as const)113 ).toEqualTypeOf<{114 readonly type: PropType<Options>115 readonly required: false116 readonly default: { key: 'value' }117 readonly validator: ((val: unknown) => boolean) | undefined118 [propKey]: true119 }>()120 })121 it('Type, validator and Object default value', () => {122 interface Options {123 key: string124 }125 expectTypeOf(126 buildProp({127 type: definePropType<Options>(Object),128 default: () => ({ key: 'value' }),129 validator: (val: unknown): val is string => true,130 } as const)131 ).toEqualTypeOf<{132 readonly type: PropType<string | Options>133 readonly required: false134 readonly default: { key: string }135 readonly validator: ((val: unknown) => boolean) | undefined136 [propKey]: true137 }>()138 })139 it('Type, validator, required', () => {140 expectTypeOf(141 buildProp({142 type: definePropType<'a' | 'b' | 'c'>(String),143 required: true,144 validator: (val: unknown): val is number => true,145 } as const)146 ).toEqualTypeOf<{147 readonly type: PropType<number | 'a' | 'b' | 'c'>148 readonly required: true149 readonly default?: undefined150 readonly validator: ((val: unknown) => boolean) | undefined151 [propKey]: true152 }>()153 })154 it('Normal type', () => {155 expectTypeOf(156 buildProp({157 type: String,158 })159 ).toEqualTypeOf<{160 readonly type: PropType<string>161 readonly required: false162 readonly default: undefined163 readonly validator: ((val: unknown) => boolean) | undefined164 [propKey]: true165 }>()166 })167 it('Normal types', () => {168 expectTypeOf(buildProp({ type: [String, Number, Boolean] })).toEqualTypeOf<{169 readonly type: PropType<string | number | boolean>170 readonly required: false171 readonly default: undefined172 readonly validator: ((val: unknown) => boolean) | undefined173 [propKey]: true174 }>()175 })176 it('Normal type and values', () => {177 expectTypeOf(178 buildProp({179 type: String,180 values: ['1', '2', '3'],181 } as const)182 ).toEqualTypeOf<{183 readonly type: PropType<'1' | '2' | '3'>184 readonly required: false185 readonly default: undefined186 readonly validator: ((val: unknown) => boolean) | undefined187 [propKey]: true188 }>()189 })190 it('Required and validator', () => {191 expectTypeOf(192 buildProp({193 required: true,194 validator: (val: unknown): val is string => true,195 } as const)196 ).toEqualTypeOf<{197 readonly type: PropType<string>198 readonly required: true199 readonly default?: undefined200 readonly validator: ((val: unknown) => boolean) | undefined201 [propKey]: true202 }>()203 })204 it('Required and validator', () => {205 expectTypeOf(206 buildProp({207 values: keyOf({ a: 'a', b: 'b' }),208 default: 'a',209 } as const)210 ).toEqualTypeOf<{211 readonly type: PropType<'a' | 'b'>212 readonly required: false213 readonly default: 'a'214 readonly validator: ((val: unknown) => boolean) | undefined215 [propKey]: true216 }>()217 })218 it('Type and default value', () => {219 expectTypeOf(220 buildProp({221 type: definePropType<{ key: 'a' | 'b' | 'c' } | undefined>(Object),222 default: () => mutable({ key: 'a' } as const),223 } as const)224 ).toEqualTypeOf<{225 readonly type: PropType<{ key: 'a' | 'b' | 'c' } | undefined>226 readonly required: false227 readonly default: { key: 'a' }228 readonly validator: ((val: unknown) => boolean) | undefined229 [propKey]: true230 }>()231 })232 it('Type and default value', () => {233 expectTypeOf(234 buildProp({235 type: [String, Number],236 default: '',237 } as const)238 ).toEqualTypeOf<{239 readonly type: PropType<string | number>240 readonly required: false241 readonly default: ''242 readonly validator: ((val: unknown) => boolean) | undefined243 [propKey]: true244 }>()245 })246 it('default value is empty object', () => {247 expectTypeOf(248 buildProp({249 type: Object,250 default: () => mutable({} as const),251 } as const)252 ).toEqualTypeOf<{253 readonly type: PropType<Record<string, any>>254 readonly required: false255 readonly default: {}256 readonly validator: ((val: unknown) => boolean) | undefined257 [propKey]: true258 }>()259 })260 it('extract', () => {261 const props = {262 key1: buildProp({263 type: String,264 required: true,265 }),266 key2: buildProp({267 type: [String, Number],268 required: true,269 }),270 } as const271 type Extracted = ExtractPropTypes<typeof props>272 expectTypeOf<Extracted>().toEqualTypeOf<{273 readonly key1: string274 readonly key2: string | number275 }>()276 })277})278describe('buildProps', () => {279 it('test buildProps', () => {280 const propsCommon = buildProps({281 type: {282 type: String,283 default: 'hello',284 },285 } as const)286 const props = buildProps({287 ...propsCommon,288 key1: {289 type: definePropType<'a' | 'b'>(String),290 },291 key2: {292 values: [1, 2, 3, 4],293 },294 key3: {295 values: [1, 2, 3, 4],296 default: 2,297 },298 key4: {299 values: keyOf({ a: 'a', b: 'b' }),300 default: 'a',301 },302 key5: Boolean,303 key6: String,304 key7: null,305 key8: Object,306 key9: Date,307 key10: Set,308 key11: undefined,309 // nested310 key12: buildProp({311 type: String,312 } as const),313 // default generator314 key13: {315 type: [String, Number, Function],316 default: () => '123' as const,317 } as const,318 key14: {319 type: Function,320 default: () => '123' as const,321 } as const,322 key15: {323 type: Function,324 default: () => () => '123' as const,...
props.ts
Source:props.ts
...65 * @description çæ propï¼è½æ´å¥½å°ä¼åç±»å66 * @example67 // limited options68 // the type will be PropType<'light' | 'dark'>69 buildProp({70 type: String,71 values: ['light', 'dark'],72 } as const)73 * @example74 // limited options and other types75 // the type will be PropType<'small' | 'medium' | number>76 buildProp({77 type: [String, Number],78 values: ['small', 'medium'],79 validator: (val: unknown): val is number => typeof val === 'number',80 } as const)81 @link see more: https://github.com/element-plus/element-plus/pull/334182 */83export function buildProp<84 T = never,85 D extends BuildPropType<T, V, C> = never,86 R extends boolean = false,87 V = never,88 C = never,89>(option: BuildPropOption<T, D, R, V, C>, key?: string): BuildPropReturn<T, D, R, V, C> {90 // filter native prop type and nested prop, e.g `null`, `undefined` (from `buildProps`)91 if (!isObject(option) || !!option[propKey]) return option as any;92 const { values, required, default: defaultValue, type, validator } = option;93 const _validator =94 values || validator95 ? (val: unknown) => {96 let valid = false;97 let allowedValues: unknown[] = [];98 if (values) {99 allowedValues = [...values, defaultValue];100 valid ||= allowedValues.includes(val);101 }102 if (validator) valid ||= validator(val);103 if (!valid && allowedValues.length > 0) {104 const allowValuesText = [...new Set(allowedValues)]105 .map((value) => JSON.stringify(value))106 .join(', ');107 warn(108 `Invalid prop: validation failed${109 key ? ` for prop "${key}"` : ''110 }. Expected one of [${allowValuesText}], got value ${JSON.stringify(val)}.`,111 );112 }113 return valid;114 }115 : undefined;116 return {117 type:118 typeof type === 'object' && Object.getOwnPropertySymbols(type).includes(wrapperKey)119 ? type[wrapperKey]120 : type,121 required: !!required,122 default: defaultValue,123 validator: _validator,124 [propKey]: true,125 } as unknown as BuildPropReturn<T, D, R, V, C>;126}127type NativePropType = [((...args: any) => any) | { new (...args: any): any } | undefined | null];128export const buildProps = <129 O extends {130 [K in keyof O]: O[K] extends BuildPropReturn<any, any, any, any, any>131 ? O[K]132 : [O[K]] extends NativePropType133 ? O[K]134 : O[K] extends BuildPropOption<infer T, infer D, infer R, infer V, infer C>135 ? D extends BuildPropType<T, V, C>136 ? BuildPropOption<T, D, R, V, C>137 : never138 : never;139 },140>(141 props: O,142) =>143 fromPairs(144 Object.entries(props).map(([key, option]) => [key, buildProp(option as any, key)]),145 ) as unknown as {146 [K in keyof O]: O[K] extends { [propKey]: boolean }147 ? O[K]148 : [O[K]] extends NativePropType149 ? O[K]150 : O[K] extends BuildPropOption<151 infer T,152 // eslint-disable-next-line @typescript-eslint/no-unused-vars153 infer _D,154 infer R,155 infer V,156 infer C157 >158 ? BuildPropReturn<T, O[K]['default'], R, V, C>...
Using AI Code Generation
1const fc = require("fast-check");2const { buildProp } = require("fast-check-monorepo");3const isEven = (n) => n % 2 === 0;4const isOdd = (n) => !isEven(n);5const isPositive = (n) => n > 0;6const isNegative = (n) => !isPositive(n);7const isZero = (n) => n === 0;8const isNonZero = (n) => !isZero(n);9const isInteger = (n) => Number.isInteger(n);10const isNotInteger = (n) => !isInteger(n);11const prop = buildProp()12 .integer()13 .filter(isOdd)14 .filter(isNegative)15 .filter(isNotInteger);16fc.assert(prop);17const fc = require("fast-check");18const { buildProp } = require("fast-check-monorepo");19const isEven = (n) => n % 2 === 0;20const isOdd = (n) => !isEven(n);21const isPositive = (n) => n > 0;22const isNegative = (n) => !isPositive(n);23const isZero = (n) => n === 0;24const isNonZero = (n) => !isZero(n);25const isInteger = (n) => Number.isInteger(n);26const isNotInteger = (n) => !isInteger(n);27const prop = buildProp()28 .integer()29 .filter(isOdd)30 .filter(isNegative)31 .filter(isNotInteger);32fc.assert(prop);33const fc = require("fast-check");34const { buildProp } = require("fast-check-monorepo");35const isEven = (n) => n % 2 === 0;36const isOdd = (n) => !isEven(n);37const isPositive = (n) => n > 0;38const isNegative = (n) => !isPositive(n);
Using AI Code Generation
1const { buildProp } = require('fast-check-monorepo');2const prop = buildProp((a, b) => a + b === b + a, {3 a: fc.integer(),4 b: fc.integer(),5});6fc.assert(prop);
Using AI Code Generation
1const fc = require('fast-check');2const { buildProp } = require('fast-check-monorepo');3const prop = (a, b) => {4 return a + b === b + a;5};6const params = {7 a: fc.integer(),8 b: fc.integer(),9};10const arb = buildProp(prop, params);11fc.assert(arb, { numRuns: 100 });12const fc = require('fast-check');13const { buildProp } = require('fast-check-monorepo');14const prop = (a, b) => {15 return a + b === b + a;16};17const params = {18 a: fc.integer(),19 b: fc.integer(),20};21 .tuple(fc.integer(), fc.integer())22 .map(([a, b]) => ({ a, b }));23const arb = buildProp(prop, params, customGenerator);24fc.assert(arb, { numRuns: 100 });25- generator: a custom generator (optional)
Using AI Code Generation
1const { buildProp } = require('fast-check-monorepo');2const { string } = require('fast-check');3const isUpperCase = (str) => str === str.toUpperCase();4test('isUpperCase should return true only for upper case strings', () => {5 const prop = buildProp(string(), (s) => isUpperCase(s) === (s === s.toUpperCase()));6 fc.assert(prop);7});8Please read [CONTRIBUTING.md](
Using AI Code Generation
1const fc = require("fast-check");2const { buildProp } = require("fast-check-monorepo");3const { add, sub, mul, div } = require("./test2");4const addProp = buildProp(5 (a, b) => add(a, b),6 (a, b) => a + b,7 [fc.integer(), fc.integer()],8);9const subProp = buildProp(10 (a, b) => sub(a, b),11 (a, b) => a - b,12 [fc.integer(), fc.integer()],13);14const mulProp = buildProp(15 (a, b) => mul(a, b),16 (a, b) => a * b,17 [fc.integer(), fc.integer()],18);19const divProp = buildProp(20 (a, b) => div(a, b),21 (a, b) => a / b,22 [fc.integer(), fc.integer()],23);24describe("Maths", () => {25 it("should add", () => {26 fc.assert(addProp);27 });28 it("should sub", () => {29 fc.assert(subProp);30 });31 it("should mul", () => {32 fc.assert(mulProp);33 });34 it("should div", () => {35 fc.assert(divProp);36 });37});
Using AI Code Generation
1const { buildProp } = require('fast-check-monorepo');2const { property } = require('fast-check');3const { isPositive } = require('is-positive');4const isPositiveProp = buildProp(property, isPositive);5describe('isPositive', () => {6 it('should hold', () => {7 fc.assert(isPositiveProp(fc.integer()));8 });9});10const { buildProp } = require('fast-check-monorepo');11const { property } = require('fast-check');12const { isPositive } = require('is-positive');13const isPositiveProp = buildProp(property, isPositive);14describe('isPositive', () => {15 it('should hold', () => {16 expect(isPositiveProp(fc.integer())).toBeTruthy();17 });18});19The documentation is available [here](
Using AI Code Generation
1const fc = require('fast-check');2const buildProp = require('../src/buildProp');3const test3 = () => {4 const prop = buildProp({5 arb: fc.integer(),6 fn: (x) => {7 return x === 0;8 },9 });10 fc.assert(prop, { numRuns: 1000 });11};12module.exports = test3;
Using AI Code Generation
1const { buildProp } = require("fast-check-monorepo");2const fc = require("fast-check");3const numberFunction = (x) => x;4const numberFunctionArb = fc.nat();5const numberFunctionProp = buildProp(numberFunction, numberFunctionArb);6const stringFunction = (x) => x;7const stringFunctionArb = fc.string();8const stringFunctionProp = buildProp(stringFunction, stringFunctionArb);9const numberToStringFunction = (x) => x.toString();10const numberToStringFunctionArb = fc.nat();11const numberToStringFunctionProp = buildProp(12);13const stringToNumberFunction = (x) => parseInt(x);14const stringToNumberFunctionArb = fc.string();15const stringToNumberFunctionProp = buildProp(16);17const stringToStringFunction = (x) => x;18const stringToStringFunctionArb = fc.string();19const stringToStringFunctionProp = buildProp(20);21const numberToNumberFunction = (x) => x;22const numberToNumberFunctionArb = fc.nat();23const numberToNumberFunctionProp = buildProp(24);25const stringToStringFunction = (x) => x;26const stringToStringFunctionArb = fc.string();27const stringToStringFunctionProp = buildProp(28);29const numberToNumberFunction = (x) => x;30const numberToNumberFunctionArb = fc.nat();31const numberToNumberFunctionProp = buildProp(32);
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!!