Skip to content

Commit

Permalink
Merge f2213ff into ab7c2cc
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Apr 18, 2020
2 parents ab7c2cc + f2213ff commit 08c0a89
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 77 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
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()
2 changes: 0 additions & 2 deletions examples/messages/provider/message-provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ describe("Message provider tests", () => {
return Promise.resolve(`state set to create a dog`)
},
},
consumer: "MyJSMessageConsumer",
log: path.resolve(process.cwd(), "logs"),
logLevel: "info",
provider: "MyJSMessageProvider",
providerVersion: "1.0.0",
Expand Down
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
6 changes: 4 additions & 2 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 @@ -20,10 +22,10 @@ for i in examples/*; do
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
38 changes: 13 additions & 25 deletions src/dsl/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import { PactfileWriteMode } from "./mockService"
import { MessageProviders, StateHandlers } from "../pact"
import { VerifierOptions as PactNodeVerifierOptions } from "@pact-foundation/pact-node"

export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"

Expand Down Expand Up @@ -58,20 +59,6 @@ export interface MandatoryPactOptions {
export type PactOptionsComplete = PactOptions & MandatoryPactOptions

export interface MessageProviderOptions {
// The name of the consumer
consumer: string

// The name of the provider
provider: string

providerVersion?: string

// Pacts to Verify
pactUrls?: string[]

// Directory to log to
log?: string

// Log level
logLevel?: LogLevel

Expand All @@ -80,19 +67,20 @@ export interface MessageProviderOptions {

// Prepare any provider states
stateHandlers?: StateHandlers

// Choices: 'overwrite' | 'update', 'none', defaults to 'overwrite'
pactfileWriteMode?: PactfileWriteMode

pactBrokerUsername?: string
pactBrokerPassword?: string
customProviderHeaders?: string[]
publishVerificationResult?: boolean
pactBrokerUrl?: string
tags?: string[]
timeout?: number
}

type ExcludedPactNodeVerifierKeys = Exclude<
keyof PactNodeVerifierOptions,
"providerBaseUrl"
>
export type PactNodeVerificationExcludedOptions = Pick<
PactNodeVerifierOptions,
ExcludedPactNodeVerifierKeys
>

export type PactMessageProviderOptions = PactNodeVerificationExcludedOptions &
MessageProviderOptions

export interface MessageConsumerOptions {
// The name of the consumer
consumer: string
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))
}
}
3 changes: 0 additions & 3 deletions src/messageProviderPact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe("MesageProvider", () => {

beforeEach(() => {
provider = new MessageProviderPact({
consumer: "myconsumer",
logLevel: "error",
messageProviders: {
successfulRequest: () => Promise.resolve("yay"),
Expand All @@ -56,7 +55,6 @@ describe("MesageProvider", () => {
})
it("creates a Provider with default log level if not specified", () => {
provider = new MessageProviderPact({
consumer: "myconsumer",
messageProviders: {},
provider: "myprovider",
})
Expand Down Expand Up @@ -129,7 +127,6 @@ describe("MesageProvider", () => {
describe("when given a state that does not have a handler", () => {
it("returns an empty promise", () => {
provider = new MessageProviderPact({
consumer: "myconsumer",
messageProviders: {},
provider: "myprovider",
})
Expand Down
4 changes: 2 additions & 2 deletions src/messageProviderPact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { omit, isEmpty } from "lodash"
import { MessageDescriptor } from "./dsl/message"
import logger from "./common/logger"
import { VerifierOptions } from "@pact-foundation/pact-node"
import { MessageProviderOptions } from "./dsl/options"
import { PactMessageProviderOptions } from "./dsl/options"
import serviceFactory from "@pact-foundation/pact-node"
import * as express from "express"
import * as http from "http"
Expand All @@ -22,7 +22,7 @@ const bodyParser = require("body-parser")
* of the interaction to respond - just in this case, not immediately.
*/
export class MessageProviderPact {
constructor(private config: MessageProviderOptions) {
constructor(private config: PactMessageProviderOptions) {
if (config.logLevel && !isEmpty(config.logLevel)) {
serviceFactory.logLevel(config.logLevel)
logger.level(config.logLevel)
Expand Down
16 changes: 0 additions & 16 deletions src/pact-web.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ chai.use(chaiAsPromised)
describe("PactWeb", () => {
let pact: PactWeb
const fullOpts = {
consumer: "A",
cors: false,
host: "127.0.0.1",
logLevel: "info",
pactfileWriteMode: "overwrite",
port: 1234,
provider: "B",
spec: 2,
ssl: false,
} as PactOptionsComplete
Expand All @@ -36,20 +34,6 @@ describe("PactWeb", () => {
sinon.restore()
})

describe("#constructor", () => {
it("throws Error when consumer not provided", () => {
expect(() => {
new PactWeb({ consumer: "", provider: "provider" })
}).not.to.throw(Error, "You must specify a Consumer for this pact.")
})

it("throws Error when provider not provided", () => {
expect(() => {
new PactWeb({ consumer: "someconsumer", provider: "" })
}).not.to.throw(Error, "You must specify a Provider for this pact.")
})
})

describe("#addInteraction", () => {
const interaction: InteractionObject = {
state: "i have a list of projects",
Expand Down
4 changes: 1 addition & 3 deletions src/pact-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ export class PactWeb {
public server: any
public opts: PactWebOptionsComplete

constructor(config?: PactOptions) {
constructor(config?: PactWebOptions) {
const defaults = {
consumer: "",
cors: false,
host: "127.0.0.1",
pactfileWriteMode: "overwrite",
port: 1234,
provider: "",
spec: 2,
ssl: false,
} as PactOptions
Expand Down
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 08c0a89

Please sign in to comment.