Skip to content

Commit

Permalink
feat: remove supertest, add random ports
Browse files Browse the repository at this point in the history
  • Loading branch information
YOU54F committed May 31, 2019
1 parent 5ef90b3 commit af5123d
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 88 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tslint.json
RELEASING.md
CONTRIBUTING.md
commitlint.config.js
jestpact.tar.gz
49 changes: 2 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@
### Features

- [x] Jest Adaptor For Pact
- [ ] Assign random ports but pass port back to user for thier http agent
- [x] Jest Adaptor For Pact with SuperTest
- [x] With added Supertest types
- [ ] Assign random ports
- [X] Assign random ports and pass port back to user for thier http agent
- [x] use postman-pact to generate postman collections for pact contracts
- [x] example publish / tagging to pact-broker
- [x] example verification
- [x] example pact stub service docker templates
- [x] Now ships with pact and jest as dependencies
- [X] BREAKING CHANGE - Removed supertest and types in v1.0.0

## `Jest-Pact` Roadmap

- [ ] Remove supertest and types
- [ ] user configurable paths for log/pact output dirs
- [ ] integration with Jest's API to make setup and teardown of pact tests very simple
- [ ] Ensure that jest-pact plays well with jest's default of watch-mode
Expand All @@ -47,14 +44,6 @@ pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, provider => {
}
```
with supertest
```ts
pactWith({ consumer: 'MyConsumer', provider: 'MyProvider' }, (provider, client) => {
// regular pact tests go here
}
```
## Configuration
```ts
Expand Down Expand Up @@ -115,40 +104,6 @@ pactWith(
);
```
## Example with SuperTest
You can use superagent as your http agent, it has a great assertion engine and as we instantiate the pact mock and http agent at the same time, we can assign random ports and take advantage of jests parallel execution.
```ts
pactWithSuperTest(
{ consumer: "MyConsumer", provider: "pactWithSuperTest" },
async (provider: any, client: any) => {
test("should accept a valid get request to get a pet", async () => {
const postValidRequest: InteractionObject = {
state: "A pet 1 exists",
uponReceiving: "A get request to get a pet",
willRespondWith: {
status: 200
},
withRequest: {
method: "GET",
path: "/v2/pet/1",
headers: { api_key: "[]" }
}
};

await provider.addInteraction(postValidRequest);
await client
.get("/v2/pet/1")
.set("api_key", "[]")
.expect(200);

await provider.verify();
});
}
);
```
### Examples of usage of `jest-pact`
See [Jest-Pact-Typescript](https://github.com/YOU54F/jest-pact-typescript) which showcases a full consumer workflow written in Typescript with Jest, using this adaptor
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"lint-staged": "8.1.7",
"prettier": "1.17.1",
"rimraf": "2.6.3",
"standard-version": "^6.0.1",
"standard-version": "^6.0.1",
"supertest": "4.0.2",
"ts-jest": "24.0.2",
"ts-node": "8.2.0",
Expand Down
8 changes: 1 addition & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as pact from "@pact-foundation/pact";
import * as path from "path";
import * as supertest from "supertest";

export interface PactOptions {
provider: string;
Expand All @@ -21,7 +20,7 @@ export declare type LogLevel =
export declare type PactFileWriteMode = "overwrite" | "update" | "merge";

const applyDefaults = (options: PactOptions) => ({
port: options.port || 8282,
port: options.port,
log: path.resolve(
process.cwd(),
"pact/logs",
Expand Down Expand Up @@ -54,8 +53,3 @@ export const pactWith = (options: PactOptions, tests: any) =>
describe(`Pact between ${options.consumer} and ${options.provider}`, () => {
tests(setupProvider(applyDefaults(options)));
});

export const pactWithSuperTest = (options: PactOptions, tests: any) =>
pactWith(options, (provider: pact.Pact) => {
tests(provider, supertest(getProviderBaseUrl(provider)));
});
31 changes: 0 additions & 31 deletions src/test/pactwith.supertest.test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/pactwith.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ pactWith({ consumer: "MyConsumer", provider: "pactWith2" }, (provider: any) => {
});

describe("provider object", () => {
test("should show the default port in the URL", () => {
expect(provider.mockService.baseUrl).toMatch(new RegExp(`8282$`));
test("should show the randomly assigned port in the URL", () => {
expect(provider.mockService.baseUrl).toMatch(new RegExp(`\\d{4,5}$`));
});
});
});

0 comments on commit af5123d

Please sign in to comment.