Best JavaScript code snippet using pact-foundation-pact
messageConsumerPact.spec.ts
Source:messageConsumerPact.spec.ts
1/* tslint:disable:no-unused-expression no-empty */2import * as chai from "chai"3import * as chaiAsPromised from "chai-as-promised"4import {5 MessageConsumerPact,6 synchronousBodyHandler,7 asynchronousBodyHandler,8} from "./messageConsumerPact"9import { Message, MessageDescriptor } from "./dsl/message"10import * as sinonChai from "sinon-chai"11chai.use(sinonChai)12chai.use(chaiAsPromised)13const expect = chai.expect14describe("MessageConsumer", () => {15 let consumer: MessageConsumerPact16 beforeEach(() => {17 consumer = new MessageConsumerPact({18 consumer: "myconsumer",19 provider: "myprovider",20 })21 })22 const testMessage: Message = {23 contents: {24 foo: "bar",25 },26 }27 describe("#constructor", () => {28 it("creates a Consumer when all mandatory parameters are provided", () => {29 expect(consumer).to.be.a("object")30 expect(consumer).to.respondTo("verify")31 })32 })33 describe("#dsl", () => {34 describe("when a valid Message has been constructed", () => {35 it("the state should be valid", () => {36 consumer37 .given("some state")38 .expectsToReceive("A message about something")39 .withContent({ foo: "bar" })40 .withMetadata({ baz: "bat" })41 return expect(consumer.validate()).to.eventually.be.fulfilled42 })43 })44 describe("when a valid state has been given", () => {45 it("the state should be save id in v3 format", () => {46 consumer47 .given("some state")48 .expectsToReceive("A message about something")49 .withContent({ foo: "bar" })50 .withMetadata({ baz: "bat" })51 expect(consumer.json().providerStates).to.be.a("array")52 expect(consumer.json().providerStates).to.deep.eq([53 { name: "some state" },54 ])55 })56 })57 describe("when a valid Message has not been constructed", () => {58 it("the state should not be valid", () => {59 consumer60 .given("some state")61 .expectsToReceive("A message about something")62 .withMetadata({ baz: "bat" })63 return expect(consumer.validate()).to.eventually.be.rejected64 })65 })66 describe("when an empty description has been given", () => {67 it("it should throw an error", () => {68 expect(() => {69 consumer.expectsToReceive("")70 }).to.throw(Error)71 })72 })73 describe("when an empty content object has been given", () => {74 it("it should throw an error", () => {75 expect(() => {76 consumer.withContent({})77 }).to.throw(Error)78 })79 })80 describe("when an empty metadata object has been given", () => {81 it("it should throw an error", () => {82 expect(() => {83 consumer.withMetadata({})84 }).to.throw(Error)85 })86 })87 })88 describe("#verify", () => {89 describe("when given a valid handler and message", () => {90 it("verifies the consumer message", () => {91 const stubbedConsumer = new MessageConsumerPact({92 consumer: "myconsumer",93 provider: "myprovider",94 })95 const stub = stubbedConsumer as any96 // Stub out service factory97 stub.getServiceFactory = () => {98 return {99 createMessage: (opts: any) => Promise.resolve("message created"),100 }101 }102 stubbedConsumer103 .given("some state")104 .expectsToReceive("A message about something")105 .withContent({ foo: "bar" })106 .withMetadata({ baz: "bat" })107 return expect(108 stubbedConsumer.verify((m: Message) => Promise.resolve("yay!"))109 ).to.eventually.be.fulfilled110 })111 })112 })113 describe("#json", () => {114 it("returns a valid Message object", () => {115 consumer.withContent({ foo: "bar" })116 const m = consumer.json()117 expect(m.contents).to.deep.eq({ foo: "bar" })118 })119 })120 describe("#getServiceFactory", () => {121 it("returns a valid pact-node object", () => {122 const serviceFactory = (consumer as any).getServiceFactory()123 expect(serviceFactory).to.be.a("object")124 expect(serviceFactory).to.respondTo("createMessage")125 })126 })127 describe("handler transformers", () => {128 describe("#asynchronousbodyHandler", () => {129 describe("when given a function that succeeds", () => {130 it("returns a Handler object that returns a completed promise", () => {131 const failFn = (obj: any) => Promise.resolve("yay!")132 const hFn = asynchronousBodyHandler(failFn)133 return expect(hFn(testMessage)).to.eventually.be.fulfilled134 })135 })136 describe("when given a function that throws an Exception", () => {137 it("returns a Handler object that returns a rejected promise", () => {138 const failFn = (obj: any) => Promise.reject("fail")139 const hFn = asynchronousBodyHandler(failFn)140 return expect(hFn(testMessage)).to.eventually.be.rejected141 })142 })143 })144 describe("#synchronousbodyHandler", () => {145 describe("when given a function that succeeds", () => {146 it("returns a Handler object that returns a completed promise", () => {147 const failFn = (obj: any) => {148 /* do nothing! */149 }150 const hFn = synchronousBodyHandler(failFn)151 return expect(hFn(testMessage)).to.eventually.be.fulfilled152 })153 })154 describe("when given a function that throws an Exception", () => {155 it("returns a Handler object that returns a rejected promise", () => {156 const failFn = (obj: any) => {157 throw new Error("fail")158 }159 const hFn = synchronousBodyHandler(failFn)160 return expect(hFn(testMessage)).to.eventually.be.rejected161 })162 })163 })164 })...
Using AI Code Generation
1const pact = require('pact-foundation-pact-node');2const path = require('path');3const chai = require('chai');4const chaiAsPromised = require('chai-as-promised');5chai.use(chaiAsPromised);6const expect = chai.expect;7const MOCK_SERVER_PORT = 1234;8describe('Pact', () => {9 describe('Consumer', () => {10 it('should validate the expectations of a service', (done) => {11 const opts = {12 pactUrls: [path.resolve(process.cwd(), 'pacts', 'test_consumer-test_provider.json')],13 };14 pact.verifyPacts(opts).then(output => {15 expect(output).to.be.an('array');16 expect(output[0].message).to.eq('Pact Verification succeeded');17 expect(output[0].providerName).to.eq('test_provider');18 done();19 });20 });21 });22});
Using AI Code Generation
1var pact = require('pact-foundation-pact-mock-service');2var stubbedConsumer = pact.StubbedConsumer;3var consumer = new stubbedConsumer();4 .given("I have a list of users")5 .uponReceiving("a request for users")6 .withRequest("get", "/users")7 .willRespondWith(200, {"Content-Type": "application/json"}, [{"name": "John"}])8 .run(function(testComplete) {9 testComplete();10 });11var pact = require('pact-mock-service');12var Consumer = pact.Consumer;13var consumer = new Consumer();14 .given("I have a list of users")15 .uponReceiving("a request for users")16 .withRequest("get", "/users")17 .willRespondWith(200, {"Content-Type": "application/json"}, [{"name": "John"}])18 .run(function(testComplete) {19 testComplete();20 });
Using AI Code Generation
1var pact = require('pact-foundation/pact-node');2var opts = {3};4pact.stubPact(opts).then(function () {5 console.log("Success");6}).catch(function (e) {7 console.log("Error: ", e);8});
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!!