Skip to content

Commit

Permalink
feat: remove need for pact-node in almost all uses, including examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Apr 18, 2020
1 parent ab7c2cc commit 4e22828
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 28 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TL;DR - you almost always want Pact JS.
| Node.js | Pact JS | |
| Browser testing | Pact Web | You probably still want Pact JS. See [Using Pact in non-Node environments](#using-pact-in-non-node-environments) \* |
| Isomorphic testing | Pact Web | You probably still want Pact JS. See [Using Pact in non-Node environments](#using-pact-in-non-node-environments) \* |
| Publishing to Pact Broker | Pact Node | Included in Pact JS distribution |
| Publishing to Pact Broker | Pact JS | |

\* The "I need to run it in the browser" question comes up occasionally. The question is this - for your JS code to be able to make a call to another API, is this dependent on browser-specific code? In most cases, people use tools like React/Angular which have libraries that work on the server and client side, in which case, these tests don't need to run in a browser and could instead be executed in a Node.js environment.

Expand Down Expand Up @@ -478,7 +478,7 @@ _Important Note_: You should only use this feature for things that can not be pe

### Publishing Pacts to a Broker

Sharing is caring - to simplify sharing Pacts between Consumers and Providers, we have created the [Pact Broker](https://pact.dius.com.au).
Sharing is caring - to simplify sharing Pacts between Consumers and Providers, we have created the [Pact Broker](https://pactflow.io).

The Broker:

Expand All @@ -490,17 +490,19 @@ The Broker:
- integrates with other systems, such as Slack or your CI server, via webhooks
- ...and much much [more](https://docs.pact.io/getting_started/sharing_pacts).

[Host your own](https://github.com/pact-foundation/pact_broker), or signup for a free hosted [Pact Broker](https://pact.dius.com.au).
[Host your own](https://github.com/pact-foundation/pact_broker), or signup for a free hosted [Pact Broker](https://pactflow.io).

```js
let pact = require('@pact-foundation/pact-node');
let opts = {
const { Publisher } = require("@pact-foundation/pact")
const opts = {
...
};

pact.publishPacts(opts).then(function () {
// do something
});
new Publisher(opts)
.publishPacts()
.then(() => {
// ...
})
```

#### Pact publishing options
Expand All @@ -524,7 +526,7 @@ If your broker has a self signed certificate, set the environment variable `SSL_

#### Publishing Verification Results to a Pact Broker

If you're using a Pact Broker (e.g. a hosted one at https://pact.dius.com.au), you can
If you're using a Pact Broker (e.g. a hosted one at https://pactflow.io), you can
publish your verification results so that consumers can query if they are safe
to release.

Expand Down
1 change: 1 addition & 0 deletions examples/e2e/test/consumer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const chai = require("chai")
const chaiAsPromised = require("chai-as-promised")
const expect = chai.expect
const { Pact, Matchers } = require("@pact-foundation/pact")
const pact = require("@pact-foundation/pact")
const LOG_LEVEL = process.env.LOG_LEVEL || "WARN"

chai.use(chaiAsPromised)
Expand Down
6 changes: 3 additions & 3 deletions examples/e2e/test/publish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pact = require("@pact-foundation/pact-node")
const { Publisher } = require("@pact-foundation/pact")
const path = require("path")
const opts = {
pactFilesOrDirs: [
Expand All @@ -18,8 +18,8 @@ const opts = {
: Math.floor(new Date() / 1000)),
}

pact
.publishPacts(opts)
new Publisher(opts)
.publishPacts()
.then(() => {
console.log("Pact contract publishing complete!")
console.log("")
Expand Down
6 changes: 3 additions & 3 deletions examples/graphql/publish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pact = require("@pact-foundation/pact-node")
const { Publisher } = require("@pact-foundation/pact")
const path = require("path")
const opts = {
pactFilesOrDirs: [
Expand All @@ -15,8 +15,8 @@ const opts = {
: Math.floor(new Date() / 1000)),
}

pact
.publishPacts(opts)
new Publisher(opts)
.publishPacts()
.then(() => {
console.log("Pact contract publishing complete!")
console.log("")
Expand Down
4 changes: 2 additions & 2 deletions examples/jest/publish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let publisher = require("@pact-foundation/pact-node")
let { Publisher } = require("@pact-foundation/pact")
let path = require("path")

let opts = {
Expand All @@ -9,4 +9,4 @@ let opts = {
consumerVersion: "2.0.0",
}

publisher.publishPacts(opts)
new Publisher(opts).publishPacts()
6 changes: 3 additions & 3 deletions examples/messages/publish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pact = require("@pact-foundation/pact-node")
const { Publisher } = require("@pact-foundation/pact")
const path = require("path")
const opts = {
pactFilesOrDirs: [
Expand All @@ -18,8 +18,8 @@ const opts = {
: Math.floor(new Date() / 1000)),
}

pact
.publishPacts(opts)
new Publisher(opts)
.publishPacts()
.then(() => {
console.log("Pact contract publishing complete!")
console.log("")
Expand Down
6 changes: 3 additions & 3 deletions examples/serverless/publish.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const pact = require("@pact-foundation/pact-node")
const { Publisher } = require("@pact-foundation/pact")
const path = require("path")
const opts = {
pactFilesOrDirs: [path.resolve(__dirname, "pacts/")],
Expand All @@ -13,8 +13,8 @@ const opts = {
: Math.floor(new Date() / 1000)),
}

pact
.publishPacts(opts)
new Publisher(opts)
.publishPacts()
.then(() => {
console.log("Pact contract publishing complete!")
console.log("")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"coverage": "nyc report --reporter=text-lcov | coveralls",
"deploy:package": "tar -czf pactjs.tar.gz config dist src package.json README.md LICENSE",
"deploy:prepare": "npm i --production && npm run deploy:package && ./scripts/create_npmrc_file.sh",
"dist": "npm run compile && webpack --config ./config/webpack.web.config.js",
"dist": "npm run compile && webpack --config ./config/webpack.web.config.js && cp package.json ./dist",
"jscpd": "jscpd -p src -r json -o jscpd.json",
"lint": "npm run lint:prettier:ts && npm run lint:prettier:js && tslint -c tslint.json '{src,test,examples}/**/*.ts' -e '**/node_modules/**/*.ts'",
"lint:fix": "prettier --parser typescript --write \"{src,test,examples}/**/*.ts\" && prettier --write \"{src,test,examples}/**/*.js\"",
Expand Down
8 changes: 4 additions & 4 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fi

npm run dist

${DIR}/prepare.sh

# Link the build so that the examples are always testing the
# current build, in it's properly exported format
(cd dist && npm link)
Expand All @@ -19,11 +21,9 @@ for i in examples/*; do
[ -e "$i" ] || continue # prevent failure if there are no examples
echo "--> running tests for: $i"
if [[ "$i" =~ "karma" ]]; then
echo " linking pact-web"
(cd "$i" && npm link @pact-foundation/pact-web && npm it)
(cd "$i" && npm i && npm link @pact-foundation/pact-web && npm t)
else
echo " linking pact"
(cd "$i" && npm link @pact-foundation/pact && npm it)
(cd "$i" && npm i && npm link @pact-foundation/pact && npm t)
fi
done

Expand Down
21 changes: 21 additions & 0 deletions src/dsl/publisher.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* tslint:disable:no-unused-expression no-empty no-string-literal*/
import * as chai from "chai"
import * as chaiAsPromised from "chai-as-promised"

import { Publisher } from "./publisher"
chai.use(chaiAsPromised)

const expect = chai.expect

describe("Publisher", () => {
describe("#constructor", () => {
it("constructs a valid Pubisher class", () => {
const p = new Publisher({
consumerVersion: "1.0.0",
pactBroker: "http://foo.com",
pactFilesOrDirs: [],
})
expect(p).to.have.nested.property("opts.consumerVersion")
})
})
})
14 changes: 14 additions & 0 deletions src/dsl/publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Pact Publisher service
* @module Publisher
*/
import { qToPromise } from "../common/utils"
import publisher, { PublisherOptions } from "@pact-foundation/pact-node"

export class Publisher {
constructor(private opts: PublisherOptions) {}

public publishPacts(): Promise<string[]> {
return qToPromise<string[]>(publisher.publishPacts(this.opts))
}
}
7 changes: 7 additions & 0 deletions src/pact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,10 @@ export * from "./dsl/interaction"
* @static
*/
export * from "./dsl/mockService"

/**
* Exposes {@link Publisher}
* @memberof Pact
* @static
*/
export * from "./dsl/publisher"

0 comments on commit 4e22828

Please sign in to comment.