Best JavaScript code snippet using pact-foundation-pact
matchers.ts
Source:matchers.ts
1/** @module Matchers2 *3 * For specific matcher types (e.g. IpV6), the values generated are not random4 * but are fixed, to prevent contract invalidation after each run of the consumer test.5 */6import { isFunction, isNil, isEmpty, isUndefined } from "lodash"7import MatcherError from "../errors/matcherError"8// Note: The following regexes are Ruby formatted,9// so attempting to parse as JS without modification is probably not going to work as intended!10/* tslint:disable:max-line-length */11export const ISO8601_DATE_FORMAT =12 "^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))?)$"13export const ISO8601_DATETIME_FORMAT =14 "^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d([+-][0-2]\\d:[0-5]\\d|Z)$"15export const ISO8601_DATETIME_WITH_MILLIS_FORMAT =16 "^\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d\\.\\d{3,}([+-][0-2]\\d:[0-5]\\d|Z)$"17export const ISO8601_TIME_FORMAT =18 "^(T\\d\\d:\\d\\d(:\\d\\d)?(\\.\\d+)?(([+-]\\d\\d:\\d\\d)|Z)?)?$"19export const RFC3339_TIMESTAMP_FORMAT =20 "^(Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s\\d{2}\\s(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s\\d{4}\\s\\d{2}:\\d{2}:\\d{2}\\s(\\+|-)\\d{4}$"21export const UUID_V4_FORMAT = "^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$"22export const IPV4_FORMAT = "^(\\d{1,3}\\.)+\\d{1,3}$"23export const IPV6_FORMAT =24 "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"25export const HEX_FORMAT = "^[0-9a-fA-F]+$"26/* tslint:enable */27/**28 * Validates the given example against the regex.29 *30 * @param example Example to use in the matcher.31 * @param matcher Regular expression to check.32 */33export function validateExample(example: string, matcher: string): boolean {34 // Note we escape the double \\ as these get sent over the wire as JSON35 return new RegExp(matcher.replace("\\\\", "\\")).test(example)36}37/**38 * The eachLike matcher39 * @param {any} content40 * @param {Object} opts41 * @param {Number} opts.min42 */43export function eachLike<T>(content: T, opts?: { min: number }) {44 if (isUndefined(content)) {45 throw new MatcherError(46 "Error creating a Pact eachLike. Please provide a content argument"47 )48 }49 if (opts && (isNil(opts.min) || opts.min < 1)) {50 throw new MatcherError(51 "Error creating a Pact eachLike. Please provide opts.min that is > 0"52 )53 }54 const min = !isEmpty(opts) && opts ? opts.min : 155 return {56 contents: content,57 getValue: () => {58 const data = []59 for (let i = 0; i < min; i++) {60 data[i] = content61 }62 return data63 },64 json_class: "Pact::ArrayLike",65 min,66 }67}68/**69 * The somethingLike matcher70 * @param {any} value - the value to be somethingLike71 */72export function somethingLike<T>(value: T) {73 if (isNil(value) || isFunction(value)) {74 throw new MatcherError(75 "Error creating a Pact somethingLike Match. Value cannot be a function or undefined"76 )77 }78 return {79 contents: value,80 getValue: () => {81 return value82 },83 json_class: "Pact::SomethingLike",84 }85}86/**87 * The term matcher. Also aliased to 'regex' for discoverability.88 * @param {Object} opts89 * @param {string} opts.generate - a value to represent the matched String90 * @param {string} opts.matcher - a Regex representing the value91 */92export function term(opts: { generate: string; matcher: string }) {93 const generate = opts.generate94 const matcher = opts.matcher95 if (isNil(generate) || isNil(matcher)) {96 throw new MatcherError(`Error creating a Pact Term.97 Please provide an object containing "generate" and "matcher" properties`)98 }99 if (!validateExample(generate, matcher)) {100 throw new MatcherError(101 `Example '${generate}' does not match provided regular expression '${matcher}'`102 )103 }104 return {105 data: {106 generate,107 matcher: {108 json_class: "Regexp",109 o: 0,110 s: matcher,111 },112 },113 getValue: () => {114 return generate115 },116 json_class: "Pact::Term",117 }118}119/**120 * UUID v4 matcher.121 * @param {string} uuuid - a UUID to use as an example.122 */123export function uuid(id?: string) {124 return term({125 generate: id || "ce118b6e-d8e1-11e7-9296-cec278b6b50a",126 matcher: UUID_V4_FORMAT,127 })128}129/**130 * IPv4 matcher.131 * @param {string} ip - an IPv4 address to use as an example. Defaults to `127.0.0.13`132 */133export function ipv4Address(ip?: string) {134 return term({135 generate: ip || "127.0.0.13",136 matcher: IPV4_FORMAT,137 })138}139/**140 * IPv6 matcher.141 * @param {string} ip - an IPv6 address to use as an example. Defaults to '::ffff:192.0.2.128'142 */143export function ipv6Address(ip?: string) {144 return term({145 generate: ip || "::ffff:192.0.2.128",146 matcher: IPV6_FORMAT,147 })148}149/**150 * ISO8601 DateTime matcher.151 * @param {string} date - an ISO8601 Date and Time string152 * e.g. 2015-08-06T16:53:10+01:00 are valid153 */154export function iso8601DateTime(date?: string) {155 return term({156 generate: date || "2015-08-06T16:53:10+01:00",157 matcher: ISO8601_DATETIME_FORMAT,158 })159}160/**161 * ISO8601 DateTime matcher with required millisecond precision162 * @param {string} date - an ISO8601 Date and Time string, e.g. 2015-08-06T16:53:10.123+01:00163 */164export function iso8601DateTimeWithMillis(date?: string) {165 return term({166 generate: date || "2015-08-06T16:53:10.123+01:00",167 matcher: ISO8601_DATETIME_WITH_MILLIS_FORMAT,168 })169}170/**171 * ISO8601 Date matcher.172 * @param {string} date - a basic yyyy-MM-dd date string e.g. 2000-09-31173 */174export function iso8601Date(date?: string) {175 return term({176 generate: date || "2013-02-01",177 matcher: ISO8601_DATE_FORMAT,178 })179}180/**181 * ISO8601 Time Matcher, matches a pattern of the format "'T'HH:mm:ss".182 * @param {string} date - a ISO8601 formatted time string e.g. T22:44:30.652Z183 */184export function iso8601Time(time?: string) {185 return term({186 generate: time || "T22:44:30.652Z",187 matcher: ISO8601_TIME_FORMAT,188 })189}190/**191 * RFC3339 Timestamp matcher, a subset of ISO8609192 * @param {string} date - an RFC3339 Date and Time string, e.g. Mon, 31 Oct 2016 15:21:41 -0400193 */194export function rfc3339Timestamp(timestamp?: string) {195 return term({196 generate: timestamp || "Mon, 31 Oct 2016 15:21:41 -0400",197 matcher: RFC3339_TIMESTAMP_FORMAT,198 })199}200/**201 * Hexadecimal Matcher.202 * @param {string} hex - a hex value.203 */204export function hexadecimal(hex?: string) {205 return term({206 generate: hex || "3F",207 matcher: HEX_FORMAT,208 })209}210/**211 * Decimal Matcher.212 * @param {float} float - a decimal value.213 */214export function decimal(float?: number) {215 return somethingLike<number>(isNil(float) ? 13.01 : float)216}217/**218 * Integer Matcher.219 * @param {integer} int - an int value.220 */221export function integer(int?: number) {222 return somethingLike<number>(isNil(int) ? 13 : int)223}224/**225 * Boolean Matcher.226 */227export function boolean(value: boolean = true) {228 return somethingLike<boolean>(value)229}230// Convenience alias'231export { somethingLike as like }232export { term as regex }233export interface MatcherResult {234 json_class: string235 getValue(): any236}237export function isMatcher(x: MatcherResult | any): x is MatcherResult {238 return (x as MatcherResult).getValue !== undefined239}240// Recurse the object removing any underlying matching guff, returning241// the raw example content242export function extractPayload(obj: any, stack: any = {}): any {243 // special case: top level matching object244 // we need to strip the properties245 if (isMatcher(obj) && isEmpty(stack)) {246 return extractPayload(obj.getValue(), obj.getValue())247 }248 // recurse the (remaining) object, replacing Matchers with their249 // actual contents250 for (const property in obj) {251 if (obj.hasOwnProperty(property)) {252 const value = obj[property]253 if (isMatcher(value)) {254 extractPayload(value.getValue(), (stack[property] = value.getValue()))255 } else if (typeof value === "object") {256 extractPayload(value, (stack[property] = value))257 }258 }259 }260 return stack...
pact.utils.ts
Source:pact.utils.ts
1import { MessageConsumerPact, Pact } from '@pact-foundation/pact';2import { term, UUID_V4_FORMAT } from '@pact-foundation/pact/src/dsl/matchers';3import { resolve } from 'path';4export function pactForResource(resource: string, suffix = 'Controller'): Pact {5 return new Pact({6 consumer: `app-${resource}`,7 provider: 'api',8 log: resolve(process.cwd(), 'coverage', 'pact', 'logs', 'api.log'),9 logLevel: 'warn',10 dir: resolve(process.cwd(), 'apps', 'api', 'target', 'test-classes', 'pact', resource + suffix),11 cors: true,12 timeout: 10000,13 spec: 2,14 pactfileWriteMode: 'overwrite',15 });16}17export function pactForMessages(resource: string, suffix = 'Messages'): MessageConsumerPact {18 return new MessageConsumerPact({19 consumer: `app-${resource}`,20 provider: 'ami',21 log: resolve(process.cwd(), 'coverage', 'pact', 'logs', 'ami.log'),22 logLevel: 'warn',23 dir: resolve(process.cwd(), 'apps', 'api', 'target', 'test-classes', 'pact-messages', resource + suffix),24 spec: 2,25 pactfileWriteMode: 'update',26 });27}28export const BEARER_TOKEN_REGEX = /^Bearer\s([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-+/=]*)$/;29export function bearer(token?: string) {30 const defaultToken =31 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';32 return term({33 generate: `Bearer ${token || defaultToken}`,34 matcher: BEARER_TOKEN_REGEX.source,35 });36}37export const JWT_TOKEN_REGEX = /^([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-+/=]*)$/;38export function jwt(token?: string) {39 const defaultToken =40 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';41 return term({42 generate: token || defaultToken,43 matcher: JWT_TOKEN_REGEX.source,44 });45}46export function withUuid(string?: string) {47 let generate = string || '/{uuid}';48 generate = generate.replace(/{uuid}/, 'ce118b6e-d8e1-11e7-9296-cec278b6b50a');49 let matcher = string || '/{uuid}';50 matcher = matcher.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');51 matcher = matcher.replace(/\\{uuid\\}/, UUID_V4_FORMAT.slice(1, -1));52 matcher = `^${matcher}$`;53 return term({54 generate,55 matcher,56 });...
uuid.ts
Source:uuid.ts
1export function uuid(): string {2 const numbers = new Uint8Array(36);3 crypto.getRandomValues(numbers);4 return uuidFromNumbers(numbers);5}6const enum UuidElements {7 Random09AF,8 Random89AB,9 Hyphen,10 Version,11}12const UUID_V4_FORMAT = [13 UuidElements.Random09AF,14 UuidElements.Random09AF,15 UuidElements.Random09AF,16 UuidElements.Random09AF,17 UuidElements.Random09AF,18 UuidElements.Random09AF,19 UuidElements.Random09AF,20 UuidElements.Random09AF,21 UuidElements.Hyphen,22 UuidElements.Random09AF,23 UuidElements.Random09AF,24 UuidElements.Random09AF,25 UuidElements.Random09AF,26 UuidElements.Hyphen,27 UuidElements.Version,28 UuidElements.Random09AF,29 UuidElements.Random09AF,30 UuidElements.Random09AF,31 UuidElements.Hyphen,32 UuidElements.Random89AB,33 UuidElements.Random09AF,34 UuidElements.Random09AF,35 UuidElements.Random09AF,36 UuidElements.Hyphen,37 UuidElements.Random09AF,38 UuidElements.Random09AF,39 UuidElements.Random09AF,40 UuidElements.Random09AF,41 UuidElements.Random09AF,42 UuidElements.Random09AF,43 UuidElements.Random09AF,44 UuidElements.Random09AF,45 UuidElements.Random09AF,46 UuidElements.Random09AF,47 UuidElements.Random09AF,48 UuidElements.Random09AF,49] as const;50export function uuidFromNumbers(random_numbers: Uint8Array): string {51 return UUID_V4_FORMAT.map((kind, i) => {52 switch (kind) {53 case UuidElements.Random09AF:54 return (random_numbers[i] & 0b1111).toString(16);55 case UuidElements.Random89AB:56 return ((random_numbers[i] & 0b11) + 8).toString(16);57 case UuidElements.Version:58 return '4';59 case UuidElements.Hyphen:60 return '-';61 }62 }).join('');...
Using AI Code Generation
1var pact = require('pact-foundation/pact');2console.log(pact.UUID_V4_FORMAT);3var pact = require('pact');4console.log(pact.UUID_V4_FORMAT);5var pact = require('pact-foundation/pact');6var uuid = pact.UUID_V4_FORMAT;7console.log(uuid);8var pact = require('pact-foundation/pact');9var uuid = pact.UUID_V4_FORMAT;10console.log(uuid);11at Object. (/Users/saurabhsingh/Documents/Work/Personal/NodeJS/pact-test/test.js:3:15)12at Module._compile (module.js:624:30)13at Object.Module._extensions..js (module.js:635:10)14at Module.load (module.js:545:32)15at tryModuleLoad (module.js:508:12)16at Function.Module._load (module.js:500:3)17at Function.Module.runMain (module.js:665:10)18at startup (node.js:201:19)
Using AI Code Generation
1const { UUID_V4_FORMAT } = require("pact-foundation/pact-node")2console.log(UUID_V4_FORMAT)3const { UUID_V4_FORMAT } = require("pact-foundation/pact-node")4console.log(UUID_V4_FORMAT)5const { UUID_V4_FORMAT } = require("pact-foundation/pact-node")6console.log(UUID_V4_FORMAT)7const { UUID_V4_FORMAT } = require("pact-foundation/pact-node")8console.log(UUID_V4_FORMAT)9Your name to display (optional):10Your name to display (optional):11Your name to display (optional):
Using AI Code Generation
1var uuid = require('uuid');2var uuidv4 = require('uuid/v4');3var uuidv1 = require('uuid/v1');4var uuidv5 = require('uuid/v5');5var uuidv3 = require('uuid/v3');6var uuidv6 = require('uuid/v6');7var uuidv7 = require('uuid/v7');8var uuidv8 = require('uuid/v8');9var uuidv9 = require('uuid/v9');10var uuidv10 = require('uuid/v10');11var uuidv11 = require('uuid/v11');12var uuidv12 = require('uuid/v12');13var uuidv13 = require('uuid/v13');14var uuidv14 = require('uuid/v14');15var uuidv15 = require('uuid/v15');16var uuidv16 = require('uuid/v16');17var uuidv17 = require('uuid/v17');18var uuidv18 = require('uuid/v18');19var uuidv19 = require('uuid/v19');20var uuidv20 = require('uuid/v20');21var uuidv21 = require('uuid/v21');22var uuidv22 = require('uuid/v22');23var uuidv23 = require('uuid/v23');24var uuidv24 = require('uuid/v24');25var uuidv25 = require('uuid/v25');26var uuidv26 = require('uuid/v26');27var uuidv27 = require('uuid/v27');28var uuidv28 = require('uuid/v28');29var uuidv29 = require('uuid/v29');30var uuidv30 = require('uuid/v30');31var uuidv31 = require('uuid/v31');32var uuidv32 = require('uuid/v32');33var uuidv33 = require('uuid/v33');34var uuidv34 = require('uuid/v34');35var uuidv35 = require('uuid/v35');36var uuidv36 = require('uuid/v36');37var uuidv37 = require('uuid/v37');38var uuidv38 = require('uuid/v38');39var uuidv39 = require('uuid/v39');40var uuidv40 = require('uuid/v40');41var uuidv41 = require('uuid/v41');42var uuidv42 = require('uuid/v42');43var uuidv43 = require('uuid/v43');44var uuidv44 = require('uuid/v44');45var uuidv45 = require('uuid/v45');
Using AI Code Generation
1var pact = require('pact-foundation/pact-node');2var uuid = pact.uuidV4Format();3console.log(uuid);4var pact = require('pact');5var uuid = pact.uuidV4Format();6console.log(uuid);7uuidV4Format: function() {8 return uuid.v4();9},10I am still seeing this issue with the latest version of pact-node (6.0.1). I get the same error as @jacobdawson11var pact = require('pact-node');12var uuid = pact.uuidV4Format();13console.log(uuid);14I'm also seeing this issue with the latest version of pact-node (6.0.1). I get the same error as @jacobdawson15var pact = require('pact-node');16var uuid = pact.uuidV4Format();17console.log(uuid);18var pact = require('pact-node');19var uuid = pact.uuidV4Format();20console.log(uuid);21var pact = require('pact-node');22var uuid = pact.uuidV4Format();23console.log(uuid);24var pact = require('pact-node');25var uuid = pact.uuidV4Format();26console.log(uuid);
Using AI Code Generation
1var uuid = require('uuid');2var uuidV4Format = uuid.v4();3console.log(uuidV4Format);4var uuid = require('uuid');5var uuidV4Format = uuid.v4();6console.log(uuidV4Format);7var uuid = require('uuid');8var uuidV4Format = uuid.v4();9console.log(uuidV4Format);10var uuid = require('uuid');11var uuidV4Format = uuid.v4();12console.log(uuidV4Format);13var uuid = require('uuid');14var uuidV4Format = uuid.v4();15console.log(uuidV4Format);16var uuid = require('uuid');17var uuidV4Format = uuid.v4();18console.log(uuidV4Format);19var uuid = require('uuid');20var uuidV4Format = uuid.v4();21console.log(uuidV4Format);22var uuid = require('uuid');23var uuidV4Format = uuid.v4();24console.log(uuidV4Format);25var uuid = require('uuid');26var uuidV4Format = uuid.v4();27console.log(uuidV4Format);28var uuid = require('uuid');29var uuidV4Format = uuid.v4();30console.log(uuidV4Format);31var uuid = require('uuid');32var uuidV4Format = uuid.v4();33console.log(uuidV4Format);34var uuid = require('uuid');35var uuidV4Format = uuid.v4();36console.log(uuidV4Format);37var uuid = require('uuid');38var uuidV4Format = uuid.v4();39console.log(uuidV
Using AI Code Generation
1const {uuidV4Format} = require('pact-foundation/pact');2const uuid = require('uuid/v4');3const uuidV4 = uuid();4const uuidV4Format = uuidV4Format();5console.log(uuidV4Format);6console.log(uuidV4);7const {uuidV4Format} = require('pact-foundation/pact');8const uuid = require('uuid/v4');9const uuidV4 = uuid();10const uuidV4Format = uuidV4Format();11console.log(uuidV4Format);12console.log(uuidV4);13const {uuidV4Format} = require('pact-foundation/pact');14const uuid = require('uuid/v4');15const uuidV4 = uuid();16const uuidV4Format = uuidV4Format();17console.log(uuidV4Format);18console.log(uuidV4);19const {uuidV4Format} = require('pact-foundation/p
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!!