Matt (pactflow.io / pact-js / pact-go)
provider
and you are using a DogApiClient
class to send requests to your Dog Service
, you can dynamically configure your API client as follows:
provider.setup().then(opts => {
dogService = new DogApiClient({ `http://${opts.host}`, port: opts.port })
})
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/18/2022, 12:31 PM/* eslint-disable no-undef */
/* eslint-disable no-console */
jest.unmock("axios");
import axios from "axios";
import MockAdapter from "axios-mock-adapter";
import { Pact, Matchers } from "@pact-foundation/pact";
import { API } from "./api";
const path = require("path");
const { string, boolean } = Matchers;
jest.setTimeout(30000);
let mockService;
const mock = new MockAdapter(axios);
const expectedResponse = {
status: true,
message: "İşlem başarılı.",
};
const pactProvider = new Pact({
port: 3001,
consumer: "mcontent-consumer",
provider: "mcontent-provider",
logLevel: "debug",
spec: 2,
cors: true,
});
beforeAll(() => pactProvider.setup().then(() => {
mockService = new API(pactProvider.mockService.baseUrl)
}));
afterAll(() => pactProvider.finalize());
describe("Pact Contract Testing", () => {
describe("retrieving a merchant by id", () => {
test("Test deneme", async () => {
try {
await pactProvider.addInteraction({
state: "a merchant with Merchant ID exists",
uponReceiving: "a request to get a merchant",
withRequest: {
method: "GET",
path: pactProvider.mockService.baseUrl + "/merchants",
},
willRespondWith: {
status: 404,
body: {
status: boolean(true),
message: string("İşlem başarılı."),
},
},
});
mock.onGet("<http://127.0.0.1:3001/merchants>").reply(200, expectedResponse, { headers: {} });
const response = await mockService.getMerchantById();
// Assert - did we get the expected response
expect(response).toStrictEqual(expectedResponse);
await pactProvider.verify();
} catch (error) {
console.log("error", error);
}
});
// afterEach(() => pactProvider.verify().then((err) => console.log(err).catch((err) => console.log(err))));
});
});
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/18/2022, 12:32 PMimport axios from "axios";
export class API {
constructor(url) {
this.url = url;
}
async getMerchantById() {
console.log("this.url", this.url);
return axios.get(this.url + "/merchants").then((data) => data.data);
}
}
export default API;
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/18/2022, 12:32 PM[2022-05-18 12:31:18.699 +0000] INFO (78491 on THB-ARG-437.local): pact-node@10.17.2: Creating Pact Server with options:
{"timeout":30000,"consumer":"mcontent-consumer","cors":true,"dir":"/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/pacts","host":"127.0.0.1","log":"/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/logs/pact.log","logLevel":"DEBUG","pactfileWriteMode":"overwrite","provider":"mcontent-provider","spec":2,"ssl":false,"port":3001,"pactFileWriteMode":"overwrite"}
[2022-05-18 12:31:18.712 +0000] DEBUG (78491 on THB-ARG-437.local): pact-node@10.17.2: Starting pact binary '/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.83/pact/bin/pact-mock-service', with arguments [service --consumer mcontent-consumer --cors true --pact_dir /Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/pacts --host 127.0.0.1 --log /Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/logs/pact.log --log-level DEBUG --provider mcontent-provider --pact_specification_version 2 --ssl false --port 3001 --pact-file-write-mode overwrite]
[2022-05-18 12:31:18.718 +0000] DEBUG (78491 on THB-ARG-437.local): pact-node@10.17.2: Created '/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.83/pact/bin/pact-mock-service' process with PID: 78509
[2022-05-18 12:31:19.491 +0000] DEBUG (78491 on THB-ARG-437.local): pact-node@10.17.2: INFO WEBrick 1.3.1
INFO ruby 2.2.2 (2015-04-13) [x86_64-darwin13]
[2022-05-18 12:31:19.492 +0000] DEBUG (78491 on THB-ARG-437.local): pact-node@10.17.2: INFO WEBrick::HTTPServer#start: pid=78509 port=3001
[2022-05-18 12:31:19.748 +0000] INFO (78491 on THB-ARG-437.local): pact@9.17.3: Setting up Pact with Consumer "mcontent-consumer" and Provider "mcontent-provider"
using mock service on Port: "3001"
console.log
this.url <http://127.0.0.1:3001>
at API._callee$ (src/universal/partials/AboutSeller/pact_test/api.js:9:13)
console.error
at node_modules/@pact-foundation/src/httpPact.ts:151:17
console.error
Pact verification failed!
at node_modules/@pact-foundation/src/httpPact.ts:152:17
console.error
Actual interactions do not match expected interactions for mock MockService.
Missing requests:
GET <http://127.0.0.1:3001/merchants>
See /Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/logs/pact.log for details.
at node_modules/@pact-foundation/src/httpPact.ts:153:17
console.log
error Error: Pact verification failed - expected interactions did not match actual.
at new VerificationError (/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/node_modules/@pact-foundation/pact/src/errors/verificationError.js:19:42)
at /Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/node_modules/@pact-foundation/src/httpPact.ts:157:17
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at _callee$ (src/universal/partials/AboutSeller/pact_test/aboutSeller.spec.js:61:17)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/18/2022, 12:33 PMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/18/2022, 12:39 PMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 9:53 AMMissing requests
issue @Matt (pactflow.io / pact-js / pact-go)Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 9:53 AMMatt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 10:14 AMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 10:14 AMMatt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:10 AMjest.unmock("axios")
on the top of page. How to change this code according to Pact
?Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:18 AMI, [2022-05-19T14:17:51.552228 #94845] INFO -- : Registered expected interaction GET /product/10
D, [2022-05-19T14:17:51.552399 #94845] DEBUG -- : {
"description": "get product with ID 10",
"providerState": "product with ID 10 exists",
"request": {
"method": "GET",
"path": "/product/10",
"headers": {
"Accept": "application/json"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
}
},
"metadata": null
}
W, [2022-05-19T14:17:51.574747 #94845] WARN -- : Verifying - actual interactions do not match expected interactions.
Missing requests:
GET /product/10
W, [2022-05-19T14:17:51.574788 #94845] WARN -- : Missing requests:
GET /product/10
I, [2022-05-19T14:17:51.587009 #94845] INFO -- : Cleared interactions
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:18 AMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:18 AMimport path from "path";
import { Pact, Matchers } from "@pact-foundation/pact";
import API from "./api";
const { like } = Matchers;
const provider = new Pact({
consumer: "FrontendWebsite",
provider: "ProductService",
log: path.resolve(process.cwd(), "logs", "pact.log"),
logLevel: "debug",
dir: path.resolve(process.cwd(), "pacts"),
spec: 2,
});
describe("API Pact test", () => {
beforeAll(() => provider.setup());
afterEach(() => provider.verify());
afterAll(() => provider.finalize());
describe("getting one product", () => {
it("ID 10 exists", async () => {
// "it" requires not "test"
// set up Pact interactions
await provider.addInteraction({
state: "product with ID 10 exists",
uponReceiving: "get product with ID 10",
withRequest: {
method: "GET",
path: "/product/10",
headers: {
Accept: "application/json",
},
},
willRespondWith: {
status: 200,
headers: {
"Content-Type": "application/json",
},
body: {},
},
});
const newApi = new API(provider.mockService.baseUrl);
// make request to Pact mock server
const response = await newApi.getProduct("10");
expect(response).toEqual({});
});
});
});
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:20 AMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
pact.log
file say?Matt (pactflow.io / pact-js / pact-go)
api.js
?Matt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:22 AMI, [2022-05-19T14:22:18.662359 #95104] INFO -- : Registered expected interaction GET /product/10
D, [2022-05-19T14:22:18.662506 #95104] DEBUG -- : {
"description": "get product with ID 10",
"providerState": "product with ID 10 exists",
"request": {
"method": "GET",
"path": "/product/10",
"headers": {
"Accept": "application/json"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
}
},
"metadata": null
}
W, [2022-05-19T14:22:18.682967 #95104] WARN -- : Verifying - actual interactions do not match expected interactions.
Missing requests:
GET /product/10
W, [2022-05-19T14:22:18.683009 #95104] WARN -- : Missing requests:
GET /product/10
I, [2022-05-19T14:22:18.694772 #95104] INFO -- : Cleared interactions
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:23 AMimport axios from "axios";
export class API {
constructor(url) {
if (url === undefined || url === "") {
url = "<http://localhost:3001>";
}
this.url = url;
console.log('this.url', this.url)
}
async getProduct(id) {
return axios.get(this.url + "/products/" + id).then((r) => r.data);
}
}
export default API;
Matt (pactflow.io / pact-js / pact-go)
getProduct
method sends requests to /products
not /product
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:25 AMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:25 AMI, [2022-05-19T14:25:28.546777 #95323] INFO -- : Registered expected interaction GET /products/10
D, [2022-05-19T14:25:28.546928 #95323] DEBUG -- : {
"description": "get product with ID 10",
"providerState": "product with ID 10 exists",
"request": {
"method": "GET",
"path": "/products/10",
"headers": {
"Accept": "application/json"
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
}
},
"metadata": null
}
W, [2022-05-19T14:25:28.569317 #95323] WARN -- : Verifying - actual interactions do not match expected interactions.
Missing requests:
GET /products/10
W, [2022-05-19T14:25:28.569359 #95323] WARN -- : Missing requests:
GET /products/10
I, [2022-05-19T14:25:28.581871 #95323] INFO -- : Cleared interactions
Matt (pactflow.io / pact-js / pact-go)
3001
still - can you confirm / print out the URL that API
thinks it’s sending requests to?Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:32 AM[2022-05-19 11:32:29.785 +0000] INFO (95814 on THB-ARG-437.home): pact-node@10.17.2: Creating Pact Server with options:
{"timeout":30000,"consumer":"FrontendWebsite","cors":false,"dir":"/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/pacts","host":"127.0.0.1","log":"/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/logs/pact.log","logLevel":"DEBUG","pactfileWriteMode":"overwrite","provider":"ProductService","spec":2,"ssl":false,"pactFileWriteMode":"overwrite"}
[2022-05-19 11:32:29.795 +0000] DEBUG (95814 on THB-ARG-437.home): pact-node@10.17.2: Starting pact binary '/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.83/pact/bin/pact-mock-service', with arguments [service --consumer FrontendWebsite --cors false --pact_dir /Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/pacts --host 127.0.0.1 --log /Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/logs/pact.log --log-level DEBUG --provider ProductService --pact_specification_version 2 --ssl false --pact-file-write-mode overwrite]
[2022-05-19 11:32:29.801 +0000] DEBUG (95814 on THB-ARG-437.home): pact-node@10.17.2: Created '/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/node_modules/@pact-foundation/pact-node/standalone/darwin-1.88.83/pact/bin/pact-mock-service' process with PID: 95823
[2022-05-19 11:32:30.314 +0000] DEBUG (95814 on THB-ARG-437.home): pact-node@10.17.2: INFO WEBrick 1.3.1
INFO ruby 2.2.2 (2015-04-13) [x86_64-darwin13]
[2022-05-19 11:32:30.315 +0000] DEBUG (95814 on THB-ARG-437.home): pact-node@10.17.2: INFO WEBrick::HTTPServer#start: pid=95823 port=64311
[2022-05-19 11:32:30.315 +0000] INFO (95814 on THB-ARG-437.home): pact-node@10.17.2: Pact running on port 64311
FAIL test_pact/api.pact.spec.js
API Pact test
getting one product
✕ ID 10 exists (43 ms)
● API Pact test › getting one product › ID 10 exists
Pact verification failed - expected interactions did not match actual.
at new VerificationError (node_modules/@pact-foundation/pact/src/errors/verificationError.js:19:42)
at node_modules/@pact-foundation/src/httpPact.ts:157:17
Test Suites: 1 failed, 1 passed, 2 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 3.17 s
Ran all test suites matching /\/test_pact/i.
Watch Usage: Press w to show more.[2022-05-19 11:32:30.831 +0000] INFO (95814 on THB-ARG-437.home): pact@9.17.3: Setting up Pact with Consumer "FrontendWebsite" and Provider "ProductService"
using mock service on Port: "64311"
console.log
this.url <http://127.0.0.1:64311>
at new API (test_pact/api.js:9:13)
console.error
at node_modules/@pact-foundation/src/httpPact.ts:151:17
console.error
Pact verification failed!
at node_modules/@pact-foundation/src/httpPact.ts:152:17
console.error
Actual interactions do not match expected interactions for mock MockService.
Missing requests:
GET /products/10
See /Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/logs/pact.log for details.
at node_modules/@pact-foundation/src/httpPact.ts:153:17
[2022-05-19 11:32:30.879 +0000] INFO (95814 on THB-ARG-437.home): pact@9.17.3: Pact File Written
[2022-05-19 11:32:30.880 +0000] INFO (95814 on THB-ARG-437.home): pact-node@10.17.2: Removing Pact process with PID: 95823
[2022-05-19 11:32:30.881 +0000] DEBUG (95814 on THB-ARG-437.home): pact-node@10.17.2: INFO going to shutdown ...
INFO WEBrick::HTTPServer#start done.
[2022-05-19 11:32:30.882 +0000] INFO (95814 on THB-ARG-437.home): pact-node@10.17.2: Deleting Pact Server with options:
{"timeout":30000,"consumer":"FrontendWebsite","cors":false,"dir":"/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/pacts","host":"127.0.0.1","log":"/Users/bsafranbolulu/Desktop/Projects/MerchantContent/mc-frontend/logs/pact.log","logLevel":"DEBUG","pactfileWriteMode":"overwrite","provider":"ProductService","spec":2,"ssl":false,"port":64311,"pactFileWriteMode":"overwrite"}
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:33 AMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:33 AMusing mock service on Port: "64311"
console.log
this.url <http://127.0.0.1:64311>
Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 11:40 AMMatt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:14 PM@pact-foundation/node
right?Matt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
@pact-foundation/pact
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:18 PMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:18 PMMatt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:20 PMMatt (pactflow.io / pact-js / pact-go)
Matt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:21 PMMatt (pactflow.io / pact-js / pact-go)
Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:23 PMjest.unmock("axios")
import API from "./api";
import nock from "nock";
describe("API", () => {
test("get product ID 50", async () => {
const product = {
id: "50",
type: "CREDIT_CARD",
name: "28 Degrees",
version: "v1",
};
const url = "<http://localhost:3001>"
const api = new API(url)
nock(api.url).get("/products/50").reply(200, product, { "Access-Control-Allow-Origin": "*" });
const respProduct = await api.getProduct("50");
expect(respProduct).toEqual(product);
});
});
Tests are not working without the code I added on the top.Berk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:23 PMBerk Safranbolulu (Hepsiburada/Bilgi Teknolojileri)
05/19/2022, 12:24 PMaxios
when we’re using jest.unmock()
method. That’s why I asked before, what should I write as a parameter on jest.unmock()
method If I want to use it for pact?
Matt (pactflow.io / pact-js / pact-go)
We need to pass a parameterwell, it depends on what it mocks. I don’t know what tools you’re using so I can’t say (I know a bit more now, of course)when we’re usingaxios
method. That’s why I asked before, what should I write as a parameter onjest.unmock()
method If I want to use it forjest.unmock()
pact?