Skip to content

Commit 375b463

Browse files
committed
added example for a node message provider
1 parent 38d9b4e commit 375b463

9 files changed

Lines changed: 125 additions & 59 deletions

pact/pact-node-messages/package-lock.json

Lines changed: 0 additions & 53 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pact/pact-node-messages/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test:consumer": "mocha consumer/*.spec.js"
7+
"test:consumer": "mocha src/consumer/*.spec.js",
8+
"test:provider": "mocha src/provider/*.spec.js",
9+
"publish:pact": "node pact/publish.js"
810
},
911
"author": "Tom Hombergs",
1012
"license": "MIT",
1113
"devDependencies": {
1214
"@pact-foundation/pact": "^7.0.3",
13-
"chai": "^4.2.0",
1415
"mocha": "^5.2.0"
1516
}
1617
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let publisher = require('@pact-foundation/pact-node');
2+
let path = require('path');
3+
4+
let opts = {
5+
providerBaseUrl: 'http://localhost:8080',
6+
pactFilesOrDirs: [path.resolve(process.cwd(), 'pacts')],
7+
pactBroker: 'https://adesso.pact.dius.com.au/',
8+
pactBrokerUsername: process.env.PACT_USERNAME,
9+
pactBrokerPassword: process.env.PACT_PASSWORD,
10+
consumerVersion: '2.0.0'
11+
};
12+
13+
publisher.publishPacts(opts).then(() => console.log("Pacts successfully published"));
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"consumer": {
3+
"name": "node-message-consumer"
4+
},
5+
"provider": {
6+
"name": "node-message-provider"
7+
},
8+
"messages": [
9+
{
10+
"description": "a hero created message",
11+
"providerStates": [
12+
13+
],
14+
"contents": {
15+
"id": 42,
16+
"name": "Superman",
17+
"superpower": "flying",
18+
"universe": "DC"
19+
},
20+
"matchingRules": {
21+
"body": {
22+
"$.id": {
23+
"matchers": [
24+
{
25+
"match": "type"
26+
}
27+
]
28+
},
29+
"$.name": {
30+
"matchers": [
31+
{
32+
"match": "type"
33+
}
34+
]
35+
},
36+
"$.superpower": {
37+
"matchers": [
38+
{
39+
"match": "type"
40+
}
41+
]
42+
},
43+
"$.universe": {
44+
"matchers": [
45+
{
46+
"match": "regex",
47+
"regex": "^(DC|Marvel)$"
48+
}
49+
]
50+
}
51+
}
52+
},
53+
"metaData": {
54+
"content-type": "application/json"
55+
}
56+
}
57+
],
58+
"metadata": {
59+
"pactSpecification": {
60+
"version": "3.0.0"
61+
}
62+
}
63+
}

pact/pact-node-messages/common/hero-created-message.js renamed to pact/pact-node-messages/src/common/hero-created-message.js

File renamed without changes.

pact/pact-node-messages/consumer/hero-event-handler.js renamed to pact/pact-node-messages/src/consumer/hero-event-handler.js

File renamed without changes.

pact/pact-node-messages/consumer/hero-event-handler.spec.js renamed to pact/pact-node-messages/src/consumer/hero-event-handler.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe("message consumer", () => {
1414

1515
describe("'hero created' message Handler", () => {
1616

17-
it("should accept a valid 'hero created' message", (done) => {
18-
return messagePact
19-
.expectsToReceive("a 'hero created' message")
17+
it("should accept a valid hero created message", (done) => {
18+
messagePact
19+
.expectsToReceive("a hero created message")
2020
.withContent({
2121
id: Matchers.like(42),
2222
name: Matchers.like("Superman"),
@@ -27,7 +27,7 @@ describe("message consumer", () => {
2727
"content-type": "application/json",
2828
})
2929
.verify(synchronousBodyHandler(handleHeroCreatedEvent))
30-
.then(done());
30+
.then(done, (error) => done(error));
3131
});
3232

3333
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const HeroCreatedMessage = require('../common/hero-created-message');
2+
3+
function produceHeroCreatedEvent(id) {
4+
return new Promise((resolve, reject) => {
5+
resolve({foo: "this is an invalid message that does not match the contract!"});
6+
});
7+
}
8+
9+
module.exports = produceHeroCreatedEvent;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const {MessageProviderPact} = require('@pact-foundation/pact');
2+
const produceHeroCreatedEvent = require('./hero-event-producer');
3+
const path = require('path');
4+
5+
describe("message producer", () => {
6+
7+
const messagePact = new MessageProviderPact({
8+
messageProviders: {
9+
"a hero created message": () => produceHeroCreatedEvent(42),
10+
},
11+
log: path.resolve(process.cwd(), "logs"),
12+
logLevel: "debug",
13+
provider: "node-message-provider",
14+
pactBrokerUrl: "https://adesso.pact.dius.com.au",
15+
pactBrokerUsername: process.env.PACT_USERNAME,
16+
pactBrokerPassword: process.env.PACT_PASSWORD,
17+
publishVerificationResult: true,
18+
providerVersion: '1.0.0',
19+
tags: ['latest']
20+
});
21+
22+
23+
describe("'hero created' message producer", () => {
24+
25+
it("should create a valid hero created message", (done) => {
26+
messagePact
27+
.verify()
28+
.then(done, (error) => done(error));
29+
}).timeout(5000);
30+
31+
});
32+
33+
});

0 commit comments

Comments
 (0)