-
Notifications
You must be signed in to change notification settings - Fork 8
/
product.pact.test.js
82 lines (72 loc) · 2.71 KB
/
product.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
/* tslint:disable:no-unused-expression object-literal-sort-keys max-classes-per-file no-empty */
const { MessageProviderPact, providerWithMetadata } = require("@pact-foundation/pact");
const { Product } = require("./product");
const { createEvent } = require("./product.event");
const cp = require("child_process");
describe("Message provider tests", () => {
let revision;
let branch;
try {
revision = cp
.execSync("git rev-parse HEAD", { stdio: "pipe" })
.toString()
.trim();
} catch (Error) {
console.log(Error);
throw new TypeError(
"Couldn't find a git commit hash, is this a git directory?"
);
}
try {
branch = cp
.execSync("git rev-parse --abbrev-ref HEAD", { stdio: "pipe" })
.toString()
.trim();
} catch (Error) {
throw new TypeError("Couldn't find a git branch, is this a git directory?");
}
const baseOpts = {
logLevel: process.env.PACT_LOG_LEVEL ? process.env.PACT_LOG_LEVEL : "INFO",
providerVersionBranch: process.env.GIT_BRANCH ?? branch,
providerVersion: process.env.GIT_COMMIT ?? revision,
};
// For builds triggered by a 'contract content changed' webhook,
// just verify the changed pact. The URL will bave been passed in
// from the webhook to the CI job.
const pactChangedOpts = {
pactUrls: [process.env.PACT_URL],
};
// For 'normal' provider builds, fetch `master` and `prod` pacts for this provider
const fetchPactsDynamicallyOpts = {
provider: "pactflow-example-provider-js-sns",
// consumerVersionTags: ["master", "prod", "main"], //the old way of specifying which pacts to verify
consumerVersionSelectors: [
{ mainBranch: true },
{ deployedOrReleased: true },
{ matchingBranch: true }
], // the new way of specifying which pacts to verify
// Specifying a particular consumer, and the latest pact (non determinstic - used for demonstration!)
// consumerVersionSelectors: [{ consumer: 'pactflow-example-consumer-js-sns', latest: true } ],
pactBrokerUrl: process.env.PACT_BROKER_BASE_URL ?? "http://localhost:8000",
enablePending: true,
includeWipPactsSince: '2020-01-01'
};
const opts = {
...baseOpts,
...(process.env.PACT_URL ? pactChangedOpts : fetchPactsDynamicallyOpts),
...(process.env.PACT_PUBLISH_VERIFICATION_RESULTS === "true"
? { publishVerificationResult: true }
: {}),
messageProviders: {
'a product event update': providerWithMetadata(() => createEvent(new Product("42", "food", "pizza"), "UPDATED"), {
topic: 'products',
}),
},
};
const p = new MessageProviderPact(opts);
describe("send an event", () => {
it("a product event update", () => {
return p.verify();
});
});
});