-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathuserApiContract.pact.test.js
112 lines (91 loc) · 3.4 KB
/
userApiContract.pact.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @jest-environment node
*/
import { Matchers, Pact } from "@pact-foundation/pact";
import { Publisher } from "@pact-foundation/pact-node";
import path from "path";
import {fetchUserData} from "./client/userDataClient";
import packageJson from '../package.json';
const MOCK_SERVER_PORT = 4711;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
describe("fetch user data", () => {
// create mock server
const pact = new Pact({
consumer: "user-data-ui",
provider: "user-data-provider",
port: MOCK_SERVER_PORT,
log: path.resolve(process.cwd(), "dist/logs", "pact.log"),
dir: path.resolve(process.cwd(), "dist/pacts"),
logLevel: "WARN",
spec: 2,
cors: true
});
// define contract
describe("can fetch User Data", () => {
beforeEach((done) => pact
.setup()
.then(() => {
// define expected response
const expectedResponse = {
firstName: Matchers.like("aValidFirstName"),
lastName: Matchers.like("aValidLastName"),
age: Matchers.integer(100)
};
// define request
return pact.addInteraction({
state: "some user available",
uponReceiving: "a user request",
withRequest: {
method: "GET",
path: "/user",
headers: {
Accept: Matchers.term({
matcher: "application/json",
generate: "application/json"
})
},
},
willRespondWith: {
status: 200,
headers: {"Content-Type": "application/json;charset=UTF-8"},
body: expectedResponse
}
});
})
.then(() => done())
);
// validate contract definition
it("can load user data", () => {
expect.assertions(3);
const url = `http://localhost:${MOCK_SERVER_PORT}/user`;
const promise = fetchUserData(url);
return promise.then(response => {
expect(response.status).toBe(200);
expect(response.headers['content-type']).toBe("application/json;charset=UTF-8");
expect(response.data).toMatchObject({
firstName: "aValidFirstName",
lastName: "aValidLastName",
age: 100
});
});
});
});
afterAll(() => {
// create contract / pact file
pact.finalize();
// publish pact to broker
publishContract();
});
});
const publishContract = () => {
const options = {
pactFilesOrDirs: [path.resolve(process.cwd(), "dist/pacts")],
pactBroker: "http://localhost:80",
consumerVersion: packageJson.version,
tags: [packageJson.name]
};
console.log("💡 trying to publish pact with following options:\n", options);
new Publisher(options).publish().then(() => {
console.log("✅ successfully published contract to broker")
});
};