Best JavaScript code snippet using fast-check-monorepo
webUrl.spec.ts
Source:webUrl.spec.ts
1import fc from 'fast-check';2import { webUrl, WebUrlConstraints } from '../../../src/arbitrary/webUrl';3import { URL } from 'url';4import {5 assertProduceCorrectValues,6 assertProduceSameValueGivenSameSeed,7 assertProduceValuesShrinkableWithoutContext,8 assertShrinkProducesSameValueWithoutInitialContext,9} from './__test-helpers__/ArbitraryAssertions';10import { Value } from '../../../src/check/arbitrary/definition/Value';11import { buildShrinkTree, renderTree } from './__test-helpers__/ShrinkTree';12import { relativeSizeArb, sizeArb, sizeRelatedGlobalConfigArb } from './__test-helpers__/SizeHelpers';13import * as UriPathArbitraryBuilderMock from '../../../src/arbitrary/_internals/builders/UriPathArbitraryBuilder';14import * as WebAuthorityMock from '../../../src/arbitrary/webAuthority';15import * as WebFragmentsMock from '../../../src/arbitrary/webFragments';16import * as WebQueryParametersMock from '../../../src/arbitrary/webQueryParameters';17import { withConfiguredGlobal } from './__test-helpers__/GlobalSettingsHelpers';18import { fakeArbitrary } from './__test-helpers__/ArbitraryHelpers';19function beforeEachHook() {20 jest.resetModules();21 jest.restoreAllMocks();22 fc.configureGlobal({ beforeEach: beforeEachHook });23}24beforeEach(beforeEachHook);25describe('webUrl', () => {26 it('should always use the same size value for all its sub-arbitraries (except webAuthority when using its own)', () => {27 fc.assert(28 fc.property(sizeRelatedGlobalConfigArb, webUrlConstraintsBuilder(), (config, constraints) => {29 // Arrange30 const { instance } = fakeArbitrary();31 const buildUriPathArbitrary = jest.spyOn(UriPathArbitraryBuilderMock, 'buildUriPathArbitrary');32 buildUriPathArbitrary.mockReturnValue(instance);33 const webAuthority = jest.spyOn(WebAuthorityMock, 'webAuthority');34 webAuthority.mockReturnValue(instance);35 const webFragments = jest.spyOn(WebFragmentsMock, 'webFragments');36 webFragments.mockReturnValue(instance);37 const webQueryParameters = jest.spyOn(WebQueryParametersMock, 'webQueryParameters');38 webQueryParameters.mockReturnValue(instance);39 // Act40 withConfiguredGlobal(config, () => webUrl(constraints));41 // Assert42 expect(buildUriPathArbitrary).toHaveBeenCalledTimes(1); // always used43 expect(webAuthority).toHaveBeenCalledTimes(1); // always used44 const resolvedSizeForPath = buildUriPathArbitrary.mock.calls[0][0];45 if (constraints.authoritySettings === undefined || constraints.authoritySettings === undefined) {46 expect(webAuthority.mock.calls[0][0]!.size).toBe(resolvedSizeForPath);47 }48 if (constraints.withFragments) {49 expect(webFragments.mock.calls[0][0]!.size).toBe(resolvedSizeForPath);50 }51 if (constraints.withQueryParameters) {52 expect(webQueryParameters.mock.calls[0][0]!.size).toBe(resolvedSizeForPath);53 }54 })55 );56 });57});58describe('webUrl (integration)', () => {59 type Extra = WebUrlConstraints;60 const extraParametersBuilder = webUrlConstraintsBuilder;61 const isCorrect = (t: string) => {62 // Valid url given the specs defined by WHATWG URL Standard: https://url.spec.whatwg.org/63 // A TypeError will be thrown if the input is not a valid URL: https://nodejs.org/api/url.html#url_constructor_new_url_input_base64 expect(() => new URL(t)).not.toThrow();65 };66 const webUrlBuilder = (extra: Extra) => webUrl(extra);67 it('should produce the same values given the same seed', () => {68 assertProduceSameValueGivenSameSeed(webUrlBuilder, { extraParameters: extraParametersBuilder() });69 });70 it('should only produce correct values', () => {71 assertProduceCorrectValues(webUrlBuilder, isCorrect, { extraParameters: extraParametersBuilder() });72 });73 it('should produce values seen as shrinkable without any context', () => {74 assertProduceValuesShrinkableWithoutContext(webUrlBuilder, { extraParameters: extraParametersBuilder(true) });75 });76 it('should be able to shrink to the same values without initial context', () => {77 assertShrinkProducesSameValueWithoutInitialContext(webUrlBuilder, {78 extraParameters: extraParametersBuilder(true),79 });80 });81 it.each`82 rawValue83 ${'http://my.domain.org/a/z'}84 ${'http://user:pass@my.domain.org/a/z'}85 ${'http://my.domain.org/a/z?query#fragments'}86 `('should be able to shrink $rawValue', ({ rawValue }) => {87 // Arrange88 const arb = webUrl({89 authoritySettings: { withUserInfo: true },90 withQueryParameters: true,91 withFragments: true,92 });93 const value = new Value(rawValue, undefined);94 // Act95 const renderedTree = renderTree(buildShrinkTree(arb, value, { numItems: 100 })).join('\n');96 // Assert97 expect(arb.canShrinkWithoutContext(rawValue)).toBe(true);98 expect(renderedTree).toMatchSnapshot();99 });100});101// Helpers102function webUrlConstraintsBuilder(onlySmall?: boolean): fc.Arbitrary<WebUrlConstraints> {103 return fc.record(104 {105 validSchemes: fc.constant(['ftp']),106 authoritySettings: fc.record(107 {108 withIPv4: fc.boolean(),109 withIPv6: fc.boolean(),110 withIPv4Extended: fc.boolean(),111 withUserInfo: fc.boolean(),112 withPort: fc.boolean(),113 size: onlySmall ? fc.constantFrom('-1', '=', 'xsmall', 'small') : fc.oneof(sizeArb, relativeSizeArb),114 },115 { requiredKeys: [] }116 ),117 withQueryParameters: fc.boolean(),118 withFragments: fc.boolean(),119 size: onlySmall ? fc.constantFrom('-1', '=', 'xsmall', 'small') : fc.oneof(sizeArb, relativeSizeArb),120 },121 { requiredKeys: [] }122 );...
emailAddress.spec.ts
Source:emailAddress.spec.ts
1import fc from 'fast-check';2import { emailAddress, EmailAddressConstraints } from '../../../src/arbitrary/emailAddress';3import { Value } from '../../../src/check/arbitrary/definition/Value';4import {5 assertProduceSameValueGivenSameSeed,6 assertProduceCorrectValues,7 assertProduceValuesShrinkableWithoutContext,8 assertShrinkProducesSameValueWithoutInitialContext,9} from './__test-helpers__/ArbitraryAssertions';10import { buildShrinkTree, renderTree } from './__test-helpers__/ShrinkTree';11import { relativeSizeArb, sizeArb } from './__test-helpers__/SizeHelpers';12function beforeEachHook() {13 jest.resetModules();14 jest.restoreAllMocks();15 fc.configureGlobal({ beforeEach: beforeEachHook });16}17beforeEach(beforeEachHook);18describe('emailAddress (integration)', () => {19 const expectValidEmailRfc1123 = (t: string) => {20 // Taken from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address21 const rfc1123 =22 /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;23 expect(t).toMatch(rfc1123);24 };25 const expectValidEmailRfc2821 = (t: string) => {26 const [localPart, domain] = t.split('@');27 // The maximum total length of a user name or other local-part is 64 characters.28 expect(localPart.length).toBeLessThanOrEqual(64);29 // The maximum total length of a domain name or number is 255 characters.30 expect(domain.length).toBeLessThanOrEqual(255);31 };32 const expectValidEmailRfc5322 = (t: string) => {33 // Taken from https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression34 const rfc5322 =35 /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;36 expect(t).toMatch(rfc5322);37 };38 type Extra = EmailAddressConstraints;39 const extraParameters: fc.Arbitrary<Extra> = fc.record(40 { size: fc.oneof(sizeArb, relativeSizeArb) },41 { requiredKeys: [] }42 );43 const isCorrect = (t: string) => {44 expectValidEmailRfc1123(t);45 expectValidEmailRfc2821(t);46 expectValidEmailRfc5322(t);47 };48 const emailAddressBuilder = () => emailAddress();49 it('should produce the same values given the same seed', () => {50 assertProduceSameValueGivenSameSeed(emailAddressBuilder, { extraParameters });51 });52 it('should only produce correct values', () => {53 assertProduceCorrectValues(emailAddressBuilder, isCorrect, { extraParameters });54 });55 it('should produce values seen as shrinkable without any context', () => {56 assertProduceValuesShrinkableWithoutContext(emailAddressBuilder, { extraParameters });57 });58 it('should be able to shrink to the same values without initial context', () => {59 assertShrinkProducesSameValueWithoutInitialContext(emailAddressBuilder, { extraParameters });60 });61 it.each`62 rawValue63 ${'me@domain.com'}64 `('should be able to shrink $rawValue', ({ rawValue }) => {65 // Arrange66 const arb = emailAddress();67 const value = new Value(rawValue, undefined);68 // Act69 const renderedTree = renderTree(buildShrinkTree(arb, value, { numItems: 100 })).join('\n');70 // Assert71 expect(arb.canShrinkWithoutContext(rawValue)).toBe(true);72 expect(renderedTree).toMatchSnapshot();73 });...
webAuthority.spec.ts
Source:webAuthority.spec.ts
1import fc from 'fast-check';2import { webAuthority, WebAuthorityConstraints } from '../../../src/arbitrary/webAuthority';3import { URL } from 'url';4import {5 assertProduceCorrectValues,6 assertProduceSameValueGivenSameSeed,7 assertProduceValuesShrinkableWithoutContext,8 assertShrinkProducesSameValueWithoutInitialContext,9} from './__test-helpers__/ArbitraryAssertions';10import { relativeSizeArb, sizeArb } from './__test-helpers__/SizeHelpers';11function beforeEachHook() {12 jest.resetModules();13 jest.restoreAllMocks();14 fc.configureGlobal({ beforeEach: beforeEachHook });15}16beforeEach(beforeEachHook);17describe('webAuthority (integration)', () => {18 type Extra = WebAuthorityConstraints;19 const extraParametersBuilder: (onlySmall?: boolean) => fc.Arbitrary<Extra> = (onlySmall?: boolean) =>20 fc.record(21 {22 withIPv4: fc.boolean(),23 withIPv4Extended: fc.boolean(),24 withIPv6: fc.boolean(),25 withPort: fc.boolean(),26 withUserInfo: fc.boolean(),27 size: onlySmall ? fc.constantFrom('-1', '=', 'xsmall', 'small') : fc.oneof(sizeArb, relativeSizeArb),28 },29 { requiredKeys: [] }30 );31 const isCorrectForURL = (webAuthority: string) => {32 expect(() => new URL(`http://${webAuthority}`)).not.toThrow();33 };34 const webAuthorityBuilder = (extra: Extra) => webAuthority(extra);35 it('should produce the same values given the same seed', () => {36 assertProduceSameValueGivenSameSeed(webAuthorityBuilder, { extraParameters: extraParametersBuilder() });37 });38 it('should only produce correct values regarding `new URL`', () => {39 assertProduceCorrectValues(webAuthorityBuilder, isCorrectForURL, { extraParameters: extraParametersBuilder() });40 });41 it('should produce values seen as shrinkable without any context', () => {42 assertProduceValuesShrinkableWithoutContext(webAuthorityBuilder, { extraParameters: extraParametersBuilder(true) });43 });44 it('should be able to shrink to the same values without initial context', () => {45 assertShrinkProducesSameValueWithoutInitialContext(webAuthorityBuilder, {46 extraParameters: extraParametersBuilder(true),47 });48 });...
Using AI Code Generation
1const { relativeSizeArb } = require('fast-check-monorepo');2const fc = require('fast-check');3const { relativeSize } = require('fast-check');4const { relativeSizeArb } = require('fast-check-monorepo');5const fc = require('fast-check');6const { relativeSize } = require('fast-check');7const { relativeSizeArb } = require('fast-check-monorepo');8const fc = require('fast-check');9const { relativeSize } = require('fast-check');10const { relativeSizeArb } = require('fast-check-monorepo');11const fc = require('fast-check');12const { relativeSize } = require('fast-check');13const { relativeSizeArb } = require('fast-check-monorepo');14const fc = require('fast-check');15const { relativeSize } = require('fast-check');16const { relativeSizeArb } = require('fast-check-monorepo');17const fc = require('fast-check');18const { relativeSize } = require('fast-check');19const { relativeSizeArb } = require('fast-check-monorepo');20const fc = require('fast-check');21const { relativeSize } = require('fast-check');22const { relativeSizeArb } = require('fast-check-monorepo');23const fc = require('fast-check');24const { relativeSize } = require('fast-check');25const { relativeSizeArb } = require('fast-check-monorepo');26const fc = require('fast-check');27const { relativeSize } = require('fast-check');28const { relativeSizeArb } = require('fast-check-monorepo');29const fc = require('fast-check');30const { relativeSize } = require('fast-check');
Using AI Code Generation
1var fc = require('fast-check');2var arb = fc.integer(-100, 100);3var arb2 = fc.relativeSizeArb(arb);4var arb3 = fc.relativeSizeArb(arb2);5var arb4 = fc.relativeSizeArb(arb3);6var arb5 = fc.relativeSizeArb(arb4);7var arb6 = fc.relativeSizeArb(arb5);8var arb7 = fc.relativeSizeArb(arb6);9var arb8 = fc.relativeSizeArb(arb7);10var arb9 = fc.relativeSizeArb(arb8);11var arb10 = fc.relativeSizeArb(arb9);12var arb11 = fc.relativeSizeArb(arb10);13var arb12 = fc.relativeSizeArb(arb11);14var arb13 = fc.relativeSizeArb(arb12);15var arb14 = fc.relativeSizeArb(arb13);16var arb15 = fc.relativeSizeArb(arb14);17var arb16 = fc.relativeSizeArb(arb15);18var arb17 = fc.relativeSizeArb(arb16);19var arb18 = fc.relativeSizeArb(arb17);20var arb19 = fc.relativeSizeArb(arb18);21var arb20 = fc.relativeSizeArb(arb19);22var arb21 = fc.relativeSizeArb(arb20);23var arb22 = fc.relativeSizeArb(arb21);24var arb23 = fc.relativeSizeArb(arb22);25var arb24 = fc.relativeSizeArb(arb23);26var arb25 = fc.relativeSizeArb(arb24);27var arb26 = fc.relativeSizeArb(arb25);28var arb27 = fc.relativeSizeArb(arb26);29var arb28 = fc.relativeSizeArb(arb27);30var arb29 = fc.relativeSizeArb(arb28);31var arb30 = fc.relativeSizeArb(arb29);32var arb31 = fc.relativeSizeArb(arb30);33var arb32 = fc.relativeSizeArb(arb31);34var arb33 = fc.relativeSizeArb(arb32);35var arb34 = fc.relativeSizeArb(arb33);36var arb35 = fc.relativeSizeArb(arb34);37var arb36 = fc.relativeSizeArb(arb35);38var arb37 = fc.relativeSizeArb(arb36);39var arb38 = fc.relativeSizeArb(arb37
Using AI Code Generation
1const fc = require('fast-check');2const {relativeSizeArb} = require('fast-check-monorepo');3const arb = relativeSizeArb(fc.integer(), fc.integer());4fc.assert(fc.property(arb, (a, b) => {5 return a < b;6}));7const fc = require('fast-check');8const {relativeSizeArb} = require('fast-check-monorepo');9const arb = relativeSizeArb(fc.integer(), fc.integer());10fc.assert(fc.property(arb, (a, b) => {11 return a < b;12}));13const fc = require('fast-check');14const {relativeSizeArb} = require('fast-check-monorepo');15const arb = relativeSizeArb(fc.integer(), fc.integer());16fc.assert(fc.property(arb, (a, b) => {17 return a < b;18}));19const fc = require('fast-check');20const {relativeSizeArb} = require('fast-check-monorepo');21const arb = relativeSizeArb(fc.integer(), fc.integer());22fc.assert(fc.property(arb, (a, b) => {23 return a < b;24}));25const fc = require('fast-check');26const {relativeSizeArb} = require('fast-check-monorepo');27const arb = relativeSizeArb(fc.integer(), fc.integer());28fc.assert(fc.property(arb, (a, b) => {29 return a < b;30}));31const fc = require('fast-check');32const {relativeSizeArb} = require('fast-check-monorepo');33const arb = relativeSizeArb(fc.integer(), fc.integer());34fc.assert(fc.property(arb, (a, b) => {35 return a < b;36}));37const fc = require('fast-check');38const {relativeSize
Using AI Code Generation
1const { relativeSizeArb } = require('fast-check');2const arb = relativeSizeArb(0, 1);3arb.generate(mrng).value;4arb.generate(mrng).value;5arb.generate(mrng).value;6arb.generate(mrng).value;7arb.generate(mrng).value;8arb.generate(mrng).value;9arb.generate(mrng).value;10arb.generate(mrng).value;11arb.generate(mrng).value;12arb.generate(mrng).value;13arb.generate(mrng).value;14arb.generate(mrng).value;15arb.generate(mrng).value;16arb.generate(mrng).value;17arb.generate(mrng).value;18arb.generate(mrng).value;19arb.generate(mrng).value;20arb.generate(mrng).value;21arb.generate(mrng).value;22arb.generate(mrng).value;23arb.generate(mrng).value;24arb.generate(mrng).value;25arb.generate(mrng).value;26arb.generate(mrng).value;27arb.generate(mrng).value;28arb.generate(mrng).value;29arb.generate(mrng).value;30arb.generate(mrng).value;31arb.generate(mrng).value;32arb.generate(mrng).value;33arb.generate(mrng).value;34arb.generate(mrng).value;
Using AI Code Generation
1const { relativeSizeArb } = require('fast-check-monorepo');2const fc = require('fast-check');3const fs = require('fs');4const array = fc.sample(5 fc.integer(0, 1000),6 relativeSizeArb(1000),7)[0];8fs.writeFileSync('test3.txt', array);9const { relativeSizeArb } = require('fast-check-monorepo');10const fc = require('fast-check');11const fs = require('fs');12const array = fc.sample(13 fc.integer(0, 10000),14 relativeSizeArb(10000),15)[0];16fs.writeFileSync('test4.txt', array);17const { relativeSizeArb } = require('fast-check-monorepo');18const fc = require('fast-check');19const fs = require('fs');20const array = fc.sample(21 fc.integer(0, 100000),22 relativeSizeArb(100000),23)[0];24fs.writeFileSync('test5.txt', array);25const { relativeSizeArb } = require('fast-check-monorepo');26const fc = require('fast-check');27const fs = require('fs');28const array = fc.sample(29 fc.integer(0, 1000000),30 relativeSizeArb(
Using AI Code Generation
1const fc = require('fast-check');2const base = 100;3const percent = 10;4const max = 1000;5const relativeSizeArb = require('./relativeSizeArb.js');6const sizeArb = relativeSizeArb(base, percent, max);7const numArb = fc.integer(1, 1000);8const uniqueNumArb = fc.array(numArb, { minLength: 1, maxLength: 1000 }).map(x => [...new Set(x)]);9const uniqueSizeArb = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => [...new Set(x)]);10const uniqueSizeArb2 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 });11const uniqueSizeArb3 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => Array.from(new Set(x)));12const uniqueSizeArb4 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => [...new Set(x)]);13const uniqueSizeArb5 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => x.filter((v, i, a) => a.indexOf(v) === i));14const uniqueSizeArb6 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => x.filter((v, i, a) => a.indexOf(v) === i));15const uniqueSizeArb7 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => x.filter((v, i, a) => a.indexOf(v) === i));16const uniqueSizeArb8 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => x.filter((v, i, a) => a.indexOf(v) === i));17const uniqueSizeArb9 = fc.array(sizeArb, { minLength: 1, maxLength: 1000 }).map(x => x
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!!