Best JavaScript code snippet using pact-foundation-pact
matchers.spec.ts
Source:matchers.spec.ts
...47 });48 describe('#atLeastOneLike', () => {49 describe('with no examples', () => {50 it('returns a JSON representation of an atLeastOneLike matcher', () => {51 const result = MatchersV3.atLeastOneLike({52 a: 'b',53 });54 expect(result).to.deep.equal({55 'pact:matcher:type': 'type',56 min: 1,57 value: [58 {59 a: 'b',60 },61 ],62 });63 });64 });65 describe('when provided examples', () => {66 it('returns a JSON representation of an atLeastOneLike matcher with the correct number of examples', () => {67 const result = MatchersV3.atLeastOneLike(68 {69 a: 'b',70 },71 472 );73 expect(result).to.deep.equal({74 'pact:matcher:type': 'type',75 min: 1,76 value: [{ a: 'b' }, { a: 'b' }, { a: 'b' }, { a: 'b' }],77 });78 });79 });80 });81 describe('#atLeastLike', () => {82 describe('with no examples', () => {83 it('returns a JSON representation of an atLeastLike matcher', () => {84 const result = MatchersV3.atLeastLike(85 {86 a: 'b',87 },88 289 );90 expect(result).to.deep.equal({91 'pact:matcher:type': 'type',92 min: 2,93 value: [{ a: 'b' }, { a: 'b' }],94 });95 });96 });97 describe('when provided examples', () => {98 it('returns a JSON representation of an atLeastLike matcher with the correct number of examples', () => {99 const result = MatchersV3.atLeastLike(100 {101 a: 'b',102 },103 2,104 4105 );106 expect(result).to.deep.equal({107 'pact:matcher:type': 'type',108 min: 2,109 value: [{ a: 'b' }, { a: 'b' }, { a: 'b' }, { a: 'b' }],110 });111 });112 });113 it('throws an error if the number of examples is less than the minimum', () => {114 expect(() => MatchersV3.atLeastLike({ a: 'b' }, 4, 2)).to.throw(115 'atLeastLike has a minimum of 4 but 2 elements were requested. Make sure the count is greater than or equal to the min.'116 );117 });118 });119 describe('#atMostLike', () => {120 describe('with no examples', () => {121 it('returns a JSON representation of an atMostLike matcher', () => {122 const result = MatchersV3.atMostLike(123 {124 a: 'b',125 },126 2127 );128 expect(result).to.deep.equal({129 'pact:matcher:type': 'type',130 max: 2,131 value: [{ a: 'b' }],132 });133 });134 });135 describe('when provided examples', () => {136 it('returns a JSON representation of an atMostLike matcher with the correct number of examples', () => {137 const result = MatchersV3.atMostLike(138 {139 a: 'b',140 },141 4,142 4143 );144 expect(result).to.deep.equal({145 'pact:matcher:type': 'type',146 max: 4,147 value: [{ a: 'b' }, { a: 'b' }, { a: 'b' }, { a: 'b' }],148 });149 });150 });151 it('throws an error if the number of examples is more than the maximum', () => {152 expect(() => MatchersV3.atMostLike({ a: 'b' }, 2, 4)).to.throw(153 'atMostLike has a maximum of 2 but 4 elements where requested. Make sure the count is less than or equal to the max.'154 );155 });156 });157 describe('#constrainedArrayLike', () => {158 describe('with no examples', () => {159 it('returns a JSON representation of an constrainedArrayLike matcher', () => {160 const result = MatchersV3.constrainedArrayLike(161 {162 a: 'b',163 },164 2,165 4166 );167 expect(result).to.deep.equal({168 'pact:matcher:type': 'type',169 min: 2,170 max: 4,171 value: [{ a: 'b' }, { a: 'b' }],172 });173 });174 });175 describe('when provided examples', () => {176 it('returns a JSON representation of an constrainedArrayLike matcher with the correct number of examples', () => {177 const result = MatchersV3.constrainedArrayLike(178 {179 a: 'b',180 },181 2,182 4,183 3184 );185 expect(result).to.deep.equal({186 'pact:matcher:type': 'type',187 min: 2,188 max: 4,189 value: [{ a: 'b' }, { a: 'b' }, { a: 'b' }],190 });191 });192 });193 it('throws an error if the number of examples is less than the minimum', () => {194 expect(() =>195 MatchersV3.constrainedArrayLike({ a: 'b' }, 4, 6, 2)196 ).to.throw(197 'constrainedArrayLike has a minimum of 4 but 2 elements where requested. Make sure the count is greater than or equal to the min.'198 );199 });200 it('throws an error if the number of examples is more than the maximum', () => {201 expect(() =>202 MatchersV3.constrainedArrayLike({ a: 'b' }, 4, 6, 8)203 ).to.throw(204 'constrainedArrayLike has a maximum of 6 but 8 elements where requested. Make sure the count is less than or equal to the max.'205 );206 });207 });208 describe('#integer', () => {209 it('returns a JSON representation of an integer matcher', () => {210 const result = MatchersV3.integer(100);211 expect(result).to.deep.equal({212 'pact:matcher:type': 'integer',213 value: 100,214 });215 });216 describe('when the example is zero', () => {217 it('returns a JSON representation of an integer matcher', () => {218 const result = MatchersV3.integer(0);219 expect(result).to.deep.equal({220 'pact:matcher:type': 'integer',221 value: 0,222 });223 });224 });225 describe('when no example is given', () => {226 it('also includes a random integer generator', () => {227 const result = MatchersV3.integer();228 expect(result).to.deep.equal({229 'pact:matcher:type': 'integer',230 'pact:generator:type': 'RandomInt',231 value: 101,232 });233 });234 });235 });236 describe('#decimal', () => {237 it('returns a JSON representation of an decimal matcher', () => {238 const result = MatchersV3.decimal(100.3);239 expect(result).to.deep.equal({240 'pact:matcher:type': 'decimal',241 value: 100.3,242 });243 });244 describe('when the example is zero', () => {245 it('returns a JSON representation of an integer matcher', () => {246 const result = MatchersV3.decimal(0.0);247 expect(result).to.deep.equal({248 'pact:matcher:type': 'decimal',249 value: 0.0,250 });251 });252 });253 describe('when no example is given', () => {254 it('also includes a random decimal generator', () => {255 const result = MatchersV3.decimal();256 expect(result).to.deep.equal({257 'pact:matcher:type': 'decimal',258 'pact:generator:type': 'RandomDecimal',259 value: 12.34,260 });261 });262 });263 });264 describe('#number', () => {265 it('returns a JSON representation of an number matcher', () => {266 const result = MatchersV3.number(100.3);267 expect(result).to.deep.equal({268 'pact:matcher:type': 'number',269 value: 100.3,270 });271 });272 describe('when no example is given', () => {273 it('also includes a random integer generator', () => {274 const result = MatchersV3.number();275 expect(result).to.deep.equal({276 'pact:matcher:type': 'number',277 'pact:generator:type': 'RandomInt',278 value: 1234,279 });280 });281 });282 });283 describe('#boolean', () => {284 it('returns a JSON representation of a like matcher', () => {285 const result = MatchersV3.boolean(true);286 expect(result).to.deep.equal({287 'pact:matcher:type': 'type',288 value: true,289 });290 });291 });292 describe('#string', () => {293 it('returns a JSON representation of a like matcher', () => {294 const result = MatchersV3.string('true');295 expect(result).to.deep.equal({296 'pact:matcher:type': 'type',297 value: 'true',298 });299 });300 });301 describe('#regex', () => {302 it('returns a JSON representation of a regex matcher', () => {303 const result = MatchersV3.regex('\\d+', '1234');304 expect(result).to.deep.equal({305 'pact:matcher:type': 'regex',306 regex: '\\d+',307 value: '1234',308 });309 });310 describe('when given a regular expression', () => {311 it('returns a JSON representation of a regex matcher', () => {312 const result = MatchersV3.regex(/\d+/, '1234');313 expect(result).to.deep.equal({314 'pact:matcher:type': 'regex',315 regex: '\\d+',316 value: '1234',317 });318 });319 });320 });321 describe('#equal', () => {322 it('returns a JSON representation of an equality matcher', () => {323 const result = MatchersV3.equal('true');324 expect(result).to.deep.equal({325 'pact:matcher:type': 'equality',326 value: 'true',327 });328 });329 });330 describe('#datetime', () => {331 describe('when an example is given', () => {332 it('returns a JSON representation of a datetime matcher', () => {333 const result = MatchersV3.datetime(334 "yyyy-MM-dd'T'HH:mm:ss.SSSX",335 '2016-02-11T09:46:56.023Z'336 );337 expect(result).to.deep.equal({338 'pact:matcher:type': 'timestamp',339 format: "yyyy-MM-dd'T'HH:mm:ss.SSSX",340 value: '2016-02-11T09:46:56.023Z',341 });342 });343 });344 });345 describe('#time', () => {346 it('returns a JSON representation of a time matcher', () => {347 const result = MatchersV3.time('HH:mm:ss', '09:46:56');348 expect(result).to.deep.equal({349 'pact:generator:type': 'Time',350 'pact:matcher:type': 'time',351 format: 'HH:mm:ss',352 value: '09:46:56',353 });354 });355 });356 describe('#date', () => {357 it('returns a JSON representation of a date matcher', () => {358 const result = MatchersV3.date('yyyy-MM-dd', '2016-02-11');359 expect(result).to.deep.equal({360 'pact:generator:type': 'Date',361 'pact:matcher:type': 'date',362 format: 'yyyy-MM-dd',363 value: '2016-02-11',364 });365 });366 });367 describe('#includes', () => {368 it('returns a JSON representation of an include matcher', () => {369 const result = MatchersV3.includes('true');370 expect(result).to.deep.equal({371 'pact:matcher:type': 'include',372 value: 'true',373 });374 });375 });376 describe('#nullValue', () => {377 it('returns a JSON representation of an null matcher', () => {378 const result = MatchersV3.nullValue();379 expect(result).to.deep.equal({380 'pact:matcher:type': 'null',381 });382 });383 });384 describe('#url', () => {385 it('returns a JSON representation of a regex matcher for the URL', () => {386 const result = MatchersV3.url2('http://localhost:8080', [387 'users',388 '1234',389 'posts',390 'latest',391 ]);392 expect(result).to.deep.equal({393 'pact:matcher:type': 'regex',394 regex: '.*(\\/users\\/1234\\/posts\\/latest)$',395 value: 'http://localhost:8080/users/1234/posts/latest',396 });397 });398 describe('when provided with a regex matcher', () => {399 it('returns a JSON representation of a regex matcher for the URL', () => {400 const result = MatchersV3.url2('http://localhost:8080', [401 'users',402 MatchersV3.regex('\\d+', '1234'),403 'posts',404 'latest',405 ]);406 expect(result).to.deep.equal({407 'pact:matcher:type': 'regex',408 regex: '.*(\\/users\\/\\d+\\/posts\\/latest)$',409 value: 'http://localhost:8080/users/1234/posts/latest',410 });411 });412 });413 describe('when provided with a regular expression', () => {414 it('returns a JSON representation of a regex matcher for the URL', () => {415 const result = MatchersV3.url2('http://localhost:8080', [416 'users',417 /\d+/,418 'posts',419 'latest',420 ]);421 expect(result).to.deep.contain({422 'pact:matcher:type': 'regex',423 regex: '.*(\\/users\\/\\d+\\/posts\\/latest)$',424 });425 expect(result.value).to.match(/\/users\/\d+\/posts\/latest$/);426 });427 });428 describe('when no base URL is provided', () => {429 it('returns regex matcher and a MockServerURL generator', () => {430 const result = MatchersV3.url([431 'users',432 MatchersV3.regex('\\d+', '1234'),433 'posts',434 'latest',435 ]);436 expect(result).to.deep.equal({437 'pact:matcher:type': 'regex',438 'pact:generator:type': 'MockServerURL',439 regex: '.*(\\/users\\/\\d+\\/posts\\/latest)$',440 value: 'http://localhost:8080/users/1234/posts/latest',441 example: 'http://localhost:8080/users/1234/posts/latest',442 });443 });444 });445 });446 describe('#uuid', () => {447 it('returns a JSON representation of an regex matcher for UUIDs', () => {448 const result = MatchersV3.uuid('ba4bd1bc-5556-11eb-9286-d71bc5b507be');449 expect(result).to.deep.equal({450 'pact:matcher:type': 'regex',451 regex: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',452 value: 'ba4bd1bc-5556-11eb-9286-d71bc5b507be',453 });454 });455 it('throws an exception if the example value does not match the UUID regex', () => {456 expect(() => MatchersV3.uuid('not a uuid')).to.throw();457 expect(() => MatchersV3.uuid('ba4bd1bc-5556-11eb-9286')).to.throw();458 expect(() =>459 MatchersV3.uuid('ba4bd1bc-5556-11eb-9286-d71bc5b507be-1234')460 ).to.throw();461 });462 it('if no example is provided, it sets up a generator', () => {463 const result = MatchersV3.uuid();464 expect(result).to.deep.equal({465 'pact:matcher:type': 'regex',466 'pact:generator:type': 'Uuid',467 regex: '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}',468 value: 'e2490de5-5bd3-43d5-b7c4-526e33f71304',469 });470 });471 });472 describe('#reify', () => {473 describe('when given an object with no matchers', () => {474 const object = {475 some: 'data',476 more: 'strings',477 an: ['array'],478 someObject: {479 withData: true,480 withNumber: 1,481 },482 };483 it('returns just that object', () => {484 expect(MatchersV3.reify(object)).to.deep.equal(object);485 });486 });487 describe('when given an object with null values', () => {488 const object = {489 some: 'data',490 more: null,491 an: [null],492 someObject: {493 withData: true,494 withNumber: 1,495 andNull: null,496 },497 };498 it('returns just that object', () => {499 expect(MatchersV3.reify(object)).to.deep.equal(object);500 });501 });502 describe('when given an object with some matchers', () => {503 const someMatchers = {504 some: MatchersV3.like('data'),505 more: 'strings',506 an: ['array'],507 another: MatchersV3.eachLike('this'),508 someObject: {509 withData: MatchersV3.like(true),510 withTerm: MatchersV3.regex('this|that', 'this'),511 withNumber: 1,512 withAnotherNumber: MatchersV3.like(2),513 },514 };515 const expected = {516 some: 'data',517 more: 'strings',518 an: ['array'],519 another: ['this'],520 someObject: {521 withData: true,522 withTerm: 'this',523 withNumber: 1,524 withAnotherNumber: 2,525 },526 };527 it('returns without matching guff', () => {528 expect(MatchersV3.reify(someMatchers)).to.deep.equal(expected);529 });530 });531 describe('when given a simple matcher', () => {532 it('removes all matching guff', () => {533 const expected = 'myawesomeword';534 const matcher = MatchersV3.regex('\\w+', 'myawesomeword');535 expect(MatchersV3.isMatcher(matcher)).to.eq(true);536 expect(MatchersV3.reify(matcher)).to.eql(expected);537 });538 });539 describe('when given a complex nested object with matchers', () => {540 it('removes all matching guff', () => {541 const o = MatchersV3.like({542 stringMatcher: {543 awesomeSetting: MatchersV3.like('a string'),544 },545 anotherStringMatcher: {546 nestedSetting: {547 anotherStringMatcherSubSetting: MatchersV3.like(true),548 },549 anotherSetting: MatchersV3.regex('this|that', 'this'),550 },551 arrayMatcher: {552 lotsOfValueregex: MatchersV3.atLeastOneLike('useful', 3),553 },554 arrayOfMatcherregex: {555 lotsOfValueregex: MatchersV3.atLeastOneLike(556 {557 foo: 'bar',558 baz: MatchersV3.like('bat'),559 },560 3561 ),562 },563 });564 const expected = {565 stringMatcher: {566 awesomeSetting: 'a string',567 },568 anotherStringMatcher: {569 nestedSetting: {...
consumer.spec.js
Source:consumer.spec.js
...38 due: timestamp(39 "yyyy-MM-dd'T'HH:mm:ss.SSSX",40 '2016-02-11T09:46:56.023Z'41 ),42 tasks: atLeastOneLike(43 {44 id: integer(),45 name: string('Do the laundry'),46 done: boolean(true),47 },48 449 ),50 }),51 });52 });53 it('generates a list of TODOs for the main screen', () => {54 let result = provider.executeTest((mockserver) => {55 console.log('In Test Function', mockserver);56 return TodoApp.setUrl(mockserver.url)...
index.js
Source:index.js
1import React, { useContext, useEffect, useState } from 'react';2import { withRouter } from 'react-router-dom';3import ThumbUpAltIcon from '@material-ui/icons/ThumbUpAlt';4import ThumbUpOutlinedIcon from '@material-ui/icons/ThumbUpOutlined';5import { gql, useMutation } from '@apollo/client';6import { useTranslation } from 'react-i18next';7import { AuthContext } from '../../App';8const graphql = gql`9 mutation likeIt($idDoc: String) {10 likeIt(idDoc: $idDoc) {11 idDoc12 likes {13 uid14 name15 }16 }17 }18`;19const Like = ({20 idDoc,21 likes22}) => {23 const { t } = useTranslation();24 const { userInfo } = useContext(AuthContext);25 const [atLeastOneLike, setAtLeastOneLike] = useState(false);26 const [likeIt] = useMutation(graphql);27 const likeAction = async () => {28 const result = await likeIt({29 variables: {30 'idDoc': idDoc,31 }32 });33 if (result.data?.likeIt?.likes?.find((like) => like.uid === userInfo.uid)) {34 setAtLeastOneLike(true);35 } else {36 setAtLeastOneLike(false);37 }38 };39 useEffect(() => {40 if (likes?.find((like) => like.uid === userInfo.uid)) {41 setAtLeastOneLike(true);42 } else {43 setAtLeastOneLike(false);44 }45 }, [userInfo, likes]);46 return <div onClick={() => likeAction()}>47 {atLeastOneLike ?48 <span><ThumbUpAltIcon/> {t('Like')}</span> :49 <span><ThumbUpOutlinedIcon/> {t('Like')}</span>50 }51 </div>;52};...
Using AI Code Generation
1const { Matchers } = require('@pact-foundation/pact');2const like = Matchers.like;3const eachLike = Matchers.eachLike;4const term = Matchers.term;5const somethingLike = Matchers.somethingLike;6const like = Matchers.like;7const eachLike = Matchers.eachLike;8const term = Matchers.term;9const somethingLike = Matchers.somethingLike;10const atLeastOneLike = Matchers.atLeastOneLike;11const getResponse = {12 "data": {13 "attributes": {
Using AI Code Generation
1const { Matchers } = require('@pact-foundation/pact');2const { somethingLike } = Matchers;3const atLeastOneLike = (expected) => {4 return somethingLike(expected, { min: 1 });5};6module.exports = {7};8const { Matchers } = require('@pact-foundation/pact');9const { somethingLike } = Matchers;10const { atLeastOneLike } = require('./test2');11const expected = {12 'id': somethingLike('1'),13 'name': somethingLike('Test'),14 'email': somethingLike('
Using AI Code Generation
1const { Matchers } = require('@pact-foundation/pact');2const { somethingLike, eachLike, like } = Matchers;3const atLeastOneLike = (value) => {4 return expect.arrayContaining([value])5}6module.exports = {7}8const { Matchers } = require('@pact-foundation/pact');9const { somethingLike, eachLike, like } = Matchers;10const { atLeastOneLike } = require('./test2');11describe('Pact with Jest', () => {12 const { Matchers } = require('@pact-foundation/pact');13 const { somethingLike, eachLike, like } = Matchers;14 it('should validate the expectations of matching requests and responses', async () => {15 const interaction = {16 withRequest: {17 headers: {18 },19 },20 willRespondWith: {21 headers: {22 },23 body: like({24 data: atLeastOneLike(like({25 id: somethingLike(1),26 name: somethingLike('John'),27 email: somethingLike('
Using AI Code Generation
1describe('Pact', () => {2 it('should validate the expectations of Consumer and Provider', () => {3 return new Verifier({4 pactUrls: [path.resolve(process.cwd(), 'pacts/provider-consumer.json')],5 }).verifyProvider().then(output => {6 console.log('Pact Verification Complete!')7 console.log(output)8 }, err => {9 console.log(err)10 })11 })12})13{ providerApplicationVersion: '1.0.0',14 [ { providerState: 'There is a list of products',15 [ { description: 'A request for products',16 interactionId: 'd5e6e7f6-4a4e-4f1e-9d7a-4a4b4d4b4d4e' } ],17 verificationProperties: { providerVersion: '1.0.0' } } ] }
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!!