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 );...
webUrl.ts
Source:webUrl.ts
...66 });67 const validSchemes = c.validSchemes || ['http', 'https'];68 const schemeArb = constantFrom(...validSchemes);69 const authorityArb = webAuthority(resolvedAuthoritySettings);70 const pathArb = buildUriPathArbitrary(resolvedSize);71 return tuple(72 schemeArb,73 authorityArb,74 pathArb,75 c.withQueryParameters === true ? option(webQueryParameters({ size: resolvedSize })) : constant(null),76 c.withFragments === true ? option(webFragments({ size: resolvedSize })) : constant(null)77 ).map(partsToUrlMapper, partsToUrlUnmapper);...
UriPathArbitraryBuilder.ts
Source:UriPathArbitraryBuilder.ts
...18 return ['medium', 'medium']; // 1000 = 100 x 1019 }20}21/** @internal */22export function buildUriPathArbitrary(resolvedSize: Size): Arbitrary<string> {23 const [segmentSize, numSegmentSize] = sqrtSize(resolvedSize);24 return array(webSegment({ size: segmentSize }), { size: numSegmentSize }).map(25 segmentsToPathMapper,26 segmentsToPathUnmapper27 );...
Using AI Code Generation
1const fc = require('fast-check');2const buildUriPathArbitrary = require('fast-check-monorepo/packages/arbitrary/_internals/builders/PathArbitraryBuilder').buildUriPathArbitrary;3const arb = buildUriPathArbitrary();4fc.assert(5 fc.property(arb, (s) => {6 console.log(s);7 return true;8 })9);10const fc = require('fast-check');11const buildUriPathArbitrary = require('fast-check-monorepo/packages/arbitrary/_internals/builders/PathArbitraryBuilder').buildUriPathArbitrary;12const arb = buildUriPathArbitrary();13fc.assert(14 fc.property(arb, (s) => {15 console.log(s);16 return true;17 })18);
Using AI Code Generation
1const assert = require('assert');2const { buildUriPathArbitrary } = require('fast-check-monorepo');3describe('buildUriPathArbitrary', function() {4 it('should build a valid URI path', function() {5 const arb = buildUriPathArbitrary();6 const value = arb.generate(mrng());7 assert.equal(value, '/foo/bar/baz');8 });9});10"devDependencies": {11 }
Using AI Code Generation
1const fc = require('fast-check');2const { buildUriPathArbitrary } = require('fast-check-monorepo');3fc.assert(4 fc.property(buildUriPathArbitrary(), (path) => {5 console.log(path);6 return true;7 })8);
Using AI Code Generation
1import { buildUriPathArbitrary } from 'fast-check-monorepo';2const fc = require('fast-check');3const pathArb = buildUriPathArbitrary();4const pathArbWithParams = buildUriPathArbitrary({5});6fc.assert(fc.property(pathArb, path => {7 return path.startsWith('/');8}));9fc.assert(fc.property(pathArbWithParams, path => {10 return path.startsWith('/');11}));12You can also use the library directly from the browser. To do so, you need to include the minified version of the library (generated with rollup) in your HTML file. For example:
Using AI Code Generation
1const { buildUriPathArbitrary } = require('fast-check');2const fc = require('fast-check');3const { buildPath } = require('./test1.js');4const pathArbitrary = buildUriPathArbitrary();5const pathArbitrary1 = pathArbitrary.filter(path => path.length > 0);6const pathArbitrary2 = buildPath();7fc.assert(8 fc.property(pathArbitrary1, path => {9 return path.length > 0;10 })11);12fc.assert(13 fc.property(pathArbitrary2, path => {14 return path.length > 0;15 })16);17const pathArbitrary3 = buildPath();18fc.assert(19 fc.property(pathArbitrary3, path => {20 return path.length > 0;21 })22);23const pathArbitrary4 = buildPath();24fc.assert(25 fc.property(pathArbitrary4, path => {26 return path.length > 0;27 })28);29const pathArbitrary5 = buildPath();30fc.assert(31 fc.property(pathArbitrary5, path => {32 return path.length > 0;33 })34);35const pathArbitrary6 = buildPath();36fc.assert(37 fc.property(pathArbitrary6, path => {38 return path.length > 0;39 })40);41const pathArbitrary7 = buildPath();42fc.assert(43 fc.property(pathArbitrary7, path => {44 return path.length > 0;45 })46);
Using AI Code Generation
1const { buildUriPathArbitrary } = require('fast-check-monorepo');2const fc = require('fast-check');3const pathArb = buildUriPathArbitrary();4fc.assert(5 fc.property(pathArb, (path) => {6 return true;7 })8);9const { buildUriPathArbitrary } = require('fast-check-monorepo');10const fc = require('fast-check');11const express = require('express');12const app = express();13const port = 3000;14const pathArb = buildUriPathArbitrary();15fc.assert(16 fc.property(pathArb, (path) => {17 return true;18 })19);20app.get(pathArb, (req, res) => {21 res.send('Hello World!');22});23app.listen(port, () => {24});25const { buildUriPathArbitrary } = require('fast-check-monorepo');26const fc = require('fast-check');27const express = require('express');28const request = require('supertest');29const app = express();30const port = 3000;31const pathArb = buildUriPathArbitrary();32fc.assert(33 fc.property(pathArb, (path) => {34 return true;35 })36);37app.get(pathArb, (req, res) => {38 res.send('Hello World!');39});40app.listen(port, () => {41});42request(app)43 .get(pathArb)44 .expect(200)45 .end((err, res
Using AI Code Generation
1const fc = require("fast-check");2const { buildUriPathArbitrary } = require("fast-check-monorepo");3const { buildUri } = require("fast-check-monorepo");4const { buildUriArbitrary } = require("fast-check-monorepo");5const path = buildUriPathArbitrary().generate(fc.random(42)).value;6console.log(path);7const uri = buildUri(path);8console.log(uri);9const uri = buildUriArbitrary().generate(fc.random(42)).value;10console.log(uri);11const fc = require("fast-check");12const { buildUriPathArbitrary } = require("fast-check-monorepo");13const { buildUri } = require("fast-check-monorepo");14const { buildUriArbitrary } = require("fast-check-monorepo");15const path = buildUriPathArbitrary().generate(fc.random(42)).value;16console.log(path);17const uri = buildUri(path);18console.log(uri);19const uri = buildUriArbitrary().generate(fc.random(42)).value;20console.log(uri);21const fc = require("fast-check");22const { buildUriPathArbitrary } = require("fast-check-monorepo");23const { buildUri } = require("fast-check-monorepo");24const { buildUriArbitrary } = require("fast-check-monorepo");25const path = buildUriPathArbitrary().generate(fc.random(42)).value;26console.log(path);27const uri = buildUri(path);28console.log(uri);29const uri = buildUriArbitrary().generate(fc.random(42)).value;30console.log(uri);
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!!