Best JavaScript code snippet using pact-foundation-pact
pact-web.spec.ts
Source:pact-web.spec.ts
...168 pact.mockService = {169 writePact: () => Promise.resolve("pact file written!"),170 removeInteractions: sinon.stub(),171 } as any172 const writePactPromise = pact.writePact()173 return Promise.all([174 expect(writePactPromise).to.eventually.eq("pact file written!"),175 expect(writePactPromise).to.eventually.be.fulfilled,176 ])177 })178 })179 })180 describe("#removeInteractions", () => {181 describe("when removing interactions is successful", () => {182 it("returns a successful promise", () => {183 pact.mockService = {184 removeInteractions: () => Promise.resolve("interactions removed!"),185 } as any186 const removeInteractionsPromise = pact.removeInteractions()...
index.ts
Source:index.ts
...37 //NOTE: spread only works for array containing more than one item38 if (formattedAlias.length > 1) {39 cy.wait([...formattedAlias]).spread((...intercepts) => {40 intercepts.forEach((intercept, index) => {41 writePact({42 intercept,43 testCaseTitle: `${testCaseTitle}-${formattedAlias[index]}`,44 pactConfig,45 blocklist: headersBlocklist46 })47 })48 })49 } else {50 cy.wait(formattedAlias).then((intercept) => {51 const flattenIntercept = Array.isArray(intercept) ? intercept[0] : intercept52 writePact({53 intercept: flattenIntercept,54 testCaseTitle: `${testCaseTitle}`,55 pactConfig,56 blocklist: headersBlocklist57 })58 })59 }60}61const requestDataMap: AnyObject = {}62const usePactGet = (alias: string) => {63 const formattedAlias = formatAlias(alias)64 // Cypress versions older than 8.2 do not have a currentTest objects65 const testCaseTitle = Cypress.currentTest ? Cypress.currentTest.title : ''66 formattedAlias.forEach((alias) => {67 cy.get(alias).then((response: any) => {68 const fullRequestAndResponse = {69 request: {70 method: requestDataMap[alias].method,71 url: requestDataMap[alias].url,72 headers: response.requestHeaders,73 body: response.requestBody74 },75 response: {76 body: response.body,77 statusCode: response.status,78 headers: response.headers,79 statusText: response.statusText80 }81 } as XHRRequestAndResponse82 writePact({83 intercept: fullRequestAndResponse,84 testCaseTitle: `${testCaseTitle}-${alias}`,85 pactConfig,86 blocklist: headersBlocklist87 })88 })89 })90}91const usePactRequest = (option: Partial<RequestOptionType>, alias: string) => {92 cy.request(option).as(alias)93 // Store request url and method to a global item as cy.request.get() doesn't94 // provide related information95 requestDataMap[`@${alias}`] = option96}...
mountebankSerialiser.js
Source:mountebankSerialiser.js
...40 timestamp: match.timestamp, // use this to detect duplicates41 }));42 });43 pact.interactions = [...pact.interactions, ...matches.flat()];44 writePact(pact);45};46const removeDuplicates = (pact) => {47 let timestamps = {};48 pact.interactions = pact.interactions.reduce((acc, interaction) => {49 if (!timestamps[interaction.timestamp]) acc.push(interaction)50 timestamps[interaction.timestamp] = true51 return acc52 }, []);53 return pact54};55const writePact = (pact) => {56 createPactDir();57 const cleanPact = removeDuplicates(pact);58 fs.writeFileSync(filePath, JSON.stringify(cleanPact));...
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!!