Best JavaScript code snippet using pact-foundation-pact
index.ts
Source:index.ts
...142 if (interactionObj instanceof Interaction) {143 // This will throw if not valid144 const interactionState = interactionObj.json();145 // Convert into the same type146 interaction = interactionToInteractionObject(interactionState);147 } else {148 interaction = interactionObj;149 }150 this.interaction.uponReceiving(interaction.uponReceiving);151 if (interaction.state) {152 this.interaction.given(interaction.state);153 }154 setRequestDetails(this.interaction, interaction.withRequest);155 setResponseDetails(this.interaction, interaction.willRespondWith);156 this.startMockServer();157 return Promise.resolve('');158 }159 /**160 * Checks with the Mock Service if the expected interactions have been exercised....
interaction.ts
Source:interaction.ts
1/**2 * An Interaction is where you define the state of your interaction with a Provider.3 * @module Interaction4 */5import { isNil, keys, omitBy } from 'lodash';6import { HTTPMethods, HTTPMethod } from '../common/request';7import { Matcher, isMatcher, AnyTemplate } from './matchers';8import ConfigurationError from '../errors/configurationError';9interface QueryObject {10 [name: string]: string | Matcher<string> | string[];11}12export type Query = string | QueryObject;13export type Headers = {14 [header: string]: string | Matcher<string>;15};16export interface RequestOptions {17 method: HTTPMethods | HTTPMethod;18 path: string | Matcher<string>;19 query?: Query;20 headers?: Headers;21 body?: AnyTemplate;22}23export interface ResponseOptions {24 status: number;25 headers?: Headers;26 body?: AnyTemplate;27}28export interface InteractionObject {29 state: string | undefined;30 uponReceiving: string;31 withRequest: RequestOptions;32 willRespondWith: ResponseOptions;33}34export interface InteractionState {35 providerState?: string;36 description?: string;37 request?: RequestOptions;38 response?: ResponseOptions;39}40export interface InteractionStateComplete {41 providerState?: string;42 description: string;43 request: RequestOptions;44 response: ResponseOptions;45}46/**47 * Returns valid if object or matcher only contains string values48 * @param query49 */50const throwIfQueryObjectInvalid = (query: QueryObject) => {51 if (isMatcher(query)) {52 return;53 }54 Object.values(query).forEach((value) => {55 if (56 !(isMatcher(value) || Array.isArray(value) || typeof value === 'string')57 ) {58 throw new ConfigurationError(`Query must only contain strings.`);59 }60 });61};62export class Interaction {63 protected state: InteractionState = {};64 /**65 * Gives a state the provider should be in for this interaction.66 * @param {string} providerState - The state of the provider.67 * @returns {Interaction} interaction68 */69 public given(providerState: string): this {70 if (providerState) {71 this.state.providerState = providerState;72 }73 return this;74 }75 /**76 * A free style description of the interaction.77 * @param {string} description - A description of the interaction.78 * @returns {Interaction} interaction79 */80 public uponReceiving(description: string): this {81 if (isNil(description)) {82 throw new ConfigurationError(83 'You must provide a description for the interaction.'84 );85 }86 this.state.description = description;87 return this;88 }89 /**90 * The request that represents this interaction triggered by the consumer.91 * @param {Object} requestOpts92 * @param {string} requestOpts.method - The HTTP method93 * @param {string} requestOpts.path - The path of the URL94 * @param {string} requestOpts.query - Any query string in the interaction95 * @param {Object} requestOpts.headers - A key-value pair oject of headers96 * @param {Object} requestOpts.body - The body, in {@link String} format or {@link Object} format97 * @returns {Interaction} interaction98 */99 public withRequest(requestOpts: RequestOptions): this {100 if (isNil(requestOpts.method)) {101 throw new ConfigurationError('You must provide an HTTP method.');102 }103 if (keys(HTTPMethods).indexOf(requestOpts.method.toString()) < 0) {104 throw new ConfigurationError(105 `You must provide a valid HTTP method: ${keys(HTTPMethods).join(', ')}.`106 );107 }108 if (isNil(requestOpts.path)) {109 throw new ConfigurationError('You must provide a path.');110 }111 if (typeof requestOpts.query === 'object') {112 throwIfQueryObjectInvalid(requestOpts.query);113 }114 this.state.request = omitBy(requestOpts, isNil) as RequestOptions;115 return this;116 }117 /**118 * The response expected by the consumer.119 * @param {Object} responseOpts120 * @param {string} responseOpts.status - The HTTP status121 * @param {string} responseOpts.headers122 * @param {Object} responseOpts.body123 * @returns {Interaction} interaction124 */125 public willRespondWith(responseOpts: ResponseOptions): this {126 if (127 isNil(responseOpts.status) ||128 responseOpts.status.toString().trim().length === 0129 ) {130 throw new ConfigurationError('You must provide a status code.');131 }132 this.state.response = omitBy(133 {134 body: responseOpts.body,135 headers: responseOpts.headers || undefined,136 status: responseOpts.status,137 },138 isNil139 ) as ResponseOptions;140 return this;141 }142 /**143 * Returns the interaction object created.144 * @returns {Object}145 */146 public json(): InteractionStateComplete {147 if (isNil(this.state.description)) {148 throw new ConfigurationError(149 'You must provide a description for the Interaction'150 );151 }152 if (153 isNil(this.state.request) ||154 isNil(this.state?.request?.method) ||155 isNil(this.state?.request?.path)156 ) {157 throw new ConfigurationError(158 'You must provide a request with at least a method and path for the Interaction'159 );160 }161 if (isNil(this.state.response) || isNil(this.state?.response?.status)) {162 throw new ConfigurationError(163 'You must provide a response with a status for the Interaction'164 );165 }166 return this.state as InteractionStateComplete;167 }168}169export const interactionToInteractionObject = (170 interaction: InteractionStateComplete171): InteractionObject => {172 return {173 state: interaction.providerState,174 uponReceiving: interaction.description,175 withRequest: {176 method: interaction.request?.method,177 path: interaction.request?.path,178 query: interaction.request?.query,179 body: interaction.request?.body,180 headers: interaction.request?.headers,181 },182 willRespondWith: {183 status: interaction.response?.status,184 body: interaction.response?.body,185 headers: interaction.response?.headers,186 },187 };...
Using AI Code Generation
1const { interactionToInteractionObject } = require('pact-foundation-pact');2const interaction = {3};4const interactionObject = interactionToInteractionObject(interaction);5console.log(interactionObject);6const { interactionToInteractionObject } = require('pact-foundation-pact');7const interaction = {8};9const interactionObject = interactionToInteractionObject(interaction);10console.log(interactionObject);11const { interactionToInteractionObject } = require('pact-foundation-pact');12const interaction = {13};14const interactionObject = interactionToInteractionObject(interaction);15console.log(interactionObject);16const { interactionToInteractionObject } = require('pact-foundation-pact');17const interaction = {18};19const interactionObject = interactionToInteractionObject(interaction);20console.log(interactionObject);21const { interactionToInteractionObject } = require('pact-foundation-pact');22const interaction = {23};24const interactionObject = interactionToInteractionObject(interaction);25console.log(interactionObject);26const { interactionToInteractionObject } = require('pact
Using AI Code Generation
1const { interactionToInteractionObject } = require('@pact-foundation/pact-node');2const { Interaction } = require('@pact-foundation/pact');3const path = require('path');4const interaction = new Interaction()5 .uponReceiving('a request for a list of users')6 .withRequest({7 headers: {8 }9 })10 .willRespondWith({11 headers: {12 },13 body: [{ id: 1, name: 'Bob' }]14 });15const interactionObject = interactionToInteractionObject(interaction);16console.log(JSON.stringify(interactionObject));17const { Matchers } = require('@pact-foundation/pact');18const { InteractionObject } = require('@pact-foundation/pact-node');19const path = require('path');20const interaction = new InteractionObject({21 withRequest: {22 headers: {23 }24 },25 willRespondWith: {26 headers: {27 },28 body: Matchers.eachLike({ id: 1, name: 'Bob' })29 }30});31console.log(JSON.stringify(interaction));
Using AI Code Generation
1const {interactionToInteractionObject} = require('@pact-foundation/pact-node');2const interaction = {3 withRequest: {4 headers: {5 }6 },7 willRespondWith: {8 headers: {9 },10 body: {11 }12 }13}14const interactionObject = interactionToInteractionObject(interaction);15console.log('interactionObject', interactionObject);16module.exports = interactionObject;17const {interactionObjectToInteraction} = require('@pact-foundation/pact-node');18const interactionObject = {19 withRequest: {20 headers: {21 }22 },23 willRespondWith: {24 headers: {25 },26 body: {27 }28 }29}30const interaction = interactionObjectToInteraction(interactionObject);31console.log('interaction', interaction);32module.exports = interaction;33const {interactionToInteractionObject, interactionObjectToInteraction} = require('@pact-foundation/pact-node');34const interaction = {35 withRequest: {36 headers: {37 }38 },39 willRespondWith: {40 headers: {41 },42 body: {43 }44 }45}46const interactionObject = interactionToInteractionObject(interaction);47console.log('interactionObject', interactionObject);
Using AI Code Generation
1var pactNode = require('pact-node');2var pactFoundation = require('pact-foundation');3var interaction = {4 "request": {5 },6 "response": {7 "headers": {8 },9 {10 }11 }12};13var pactNodeInteraction = pactFoundation.interactionToInteractionObject(interaction);14console.log(pactNodeInteraction);15var pactNode = require('pact-node');16var pactFoundation = require('pact-foundation');17var interaction = {18 "request": {19 },20 "response": {21 "headers": {22 },23 {24 }25 }26};27var pactNodeInteraction = pactFoundation.interactionToInteractionObject(interaction);28console.log(pactNodeInteraction);29var pactNode = require('pact-node');30var pactFoundation = require('pact-foundation');31var interaction = {32 "request": {33 },34 "response": {35 "headers": {36 },37 {
Using AI Code Generation
1const Pact = require('@pact-foundation/pact-node');2const interaction = require('./test1.json');3const interactionObject = Pact.interactionToInteractionObject(interaction);4console.log(interactionObject);5const fs = require('fs');6fs.writeFileSync('test2.json', JSON.stringify(interactionObject, null, 2));7{8 "request": {9 },10 "response": {11 "headers": {12 },13 {14 },15 {16 }17 }18}
Using AI Code Generation
1const pact = require('@pact-foundation/pact-node');2const interactionToInteractionObject = require('./pact-node-converter');3const interaction = {4 withRequest: {5 headers: {6 },7 body: {8 }9 },10 willRespondWith: {11 headers: {12 },13 body: {14 }15 }16};17const interactionObject = interactionToInteractionObject(interaction);18console.log(interactionObject);19const interactionToInteractionObject = (interaction) => {20 const request = interaction.withRequest;21 const response = interaction.willRespondWith;22 const interactionObject = {23 withRequest: {24 },25 willRespondWith: {26 }27 };28 return interactionObject;29};30module.exports = interactionToInteractionObject;
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!!