Skip to content

Commit

Permalink
wip: initial ts refactor for TS example
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Feb 8, 2018
1 parent fc5eb62 commit 1c1506d
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 9 deletions.
20 changes: 20 additions & 0 deletions examples/typescript/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import axios from "axios";

export class DogService {
private url: string;
private port: number;

constructor(endpoint: any) {
this.url = endpoint.url;
this.port = endpoint.port;
}

public getMeDogs = (): Promise<any> => {
return axios.request({
baseURL: `${this.url}:${this.port}`,
headers: { Accept: "application/json" },
method: "GET",
url: "/dogs",
});
}
}
20 changes: 20 additions & 0 deletions examples/typescript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "typescript-pact",
"version": "1.0.0",
"description": "TypeScript Pact Example",
"main": "index.js",
"scripts": {
"build": "tsc",
"test": "nyc --check-coverage --reporter=html --reporter=text-summary mocha"
},
"author": "",
"license": "MIT",
"devDependencies": {
"@types/mocha": "^2.2.41",
"mocha": "3.x",
"nyc": "^11.2.0"
},
"dependencies": {
"axios": "^0.17.1"
}
}
93 changes: 93 additions & 0 deletions examples/typescript/test/get-dog.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* tslint:disable:no-unused-expression object-literal-sort-keys max-classes-per-file no-empty */
import * as chai from "chai";
import * as chaiAsPromised from "chai-as-promised";
import path = require("path");
import * as sinon from "sinon";
import * as sinonChai from "sinon-chai";
import pact = require("../../../dist/pact");
import { InteractionObject, Interaction } from "../../../dist/pact";

const Pact = pact.Pact;
const expect = chai.expect;
const proxyquire = require("proxyquire").noCallThru();
import { DogService } from "../index";

chai.use(sinonChai);
chai.use(chaiAsPromised);

describe("The Dog API", () => {
const url = "http://localhost";
const port = 8989;
const dogService = new DogService({ url, port });

const provider = new Pact({
port,
log: path.resolve(process.cwd(), "logs", "mockserver-integration.log"),
dir: path.resolve(process.cwd(), "pacts"),
spec: 2,
consumer: "MyConsumer",
provider: "MyProvider",
pactfileWriteMode: "merge",
});

const EXPECTED_BODY = [{ dog: 1 }, { dog: 2 }];

before(() => provider.setup());

after(() => provider.finalize());

describe("get /dogs", () => {
before(() => {
const interaction = new Interaction()
.given("I have a list of dogs")
.uponReceiving("a request for all dogs")
.withRequest({
method: "GET",
path: "/dogs",
headers: {
Accept: "application/json",
},
})
.willRespondWith({
status: 200,
headers: {
"Content-Type": "application/json",
},
body: EXPECTED_BODY,
});
return provider.addInteraction(interaction);
// before(() => {
// const interaction = {
// state: "i have a list of dogs",
// uponReceiving: "a request for all dogs",
// withRequest: {
// method: "GET",
// path: "/dogs",
// headers: {
// Accept: "application/json",
// },
// },
// willRespondWith: {
// status: 200,
// headers: {
// "Content-Type": "application/json",
// },
// body: EXPECTED_BODY,
// },
// } as InteractionObject;
// return provider.addInteraction(interaction);
// });

it("returns the correct response", (done) => {
dogService
.getMeDogs()
.then((response: any) => {
expect(response.data).to.eql(EXPECTED_BODY);
done();
}, done);
});

// verify with Pact, and reset expectations
afterEach(() => provider.verify());
});
});
6 changes: 6 additions & 0 deletions examples/typescript/test/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import wrapper from "@pact-foundation/pact-node";

// used to kill any left over mock server instances
process.on("SIGINT", () => {
wrapper.removeAllServers();
});
11 changes: 11 additions & 0 deletions examples/typescript/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--bail
--compilers ts:ts-node/register
--require ./test/helper.ts
--require ts-node/register
--require source-map-support/register
--full-trace
--reporter spec
--colors
--recursive
--timeout 10000
test/*.spec.ts
32 changes: 32 additions & 0 deletions examples/typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist",
"baseUrl": "src",
"sourceMap": true,
"noLib": false,
"noImplicitReturns": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"moduleResolution": "node",
"noEmitOnError": true,
"emitDecoratorMetadata": true,
"declaration": true,
"experimentalDecorators": true,
"target": "es5",
"lib": [
"es2016",
"dom"
]
},
"include": [
"./**/*"
],
"exclude": [
"./node_modules/**/*",
"./dist",
"./examples",
"test/*.spec.ts"
]
}
12 changes: 12 additions & 0 deletions examples/typescript/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {
"interface-name": [true, "never-prefix"],
"no-var-requires": false
},
"rulesDirectory": []
}
2 changes: 2 additions & 0 deletions src/dsl/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ export class Interaction {
headers: responseOpts.headers || undefined,
status: responseOpts.status,
}, isNil) as ResponseOptions;

return this;
}

/**
Expand Down
23 changes: 14 additions & 9 deletions src/pact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,22 @@ export class Pact {
* @param {Interaction} interactionObj
* @returns {Promise}
*/
public addInteraction(interactionObj: InteractionObject): Promise<string> {
const interaction = new Interaction();

if (interactionObj.state) {
interaction.given(interactionObj.state);
public addInteraction(interactionObj: InteractionObject | Interaction): Promise<string> {
let interaction: Interaction;

// tslint:disable:no-angle-bracket-type-assertion
if (<InteractionObject>(<any>interactionObj).state) {
interaction = new Interaction();
if (interactionObj.state) {
interaction.given(interactionObj.state);
}

interaction
.uponReceiving(interactionObj.uponReceiving)
.withRequest(interactionObj.withRequest)
.willRespondWith(interactionObj.willRespondWith);
}

interaction
.uponReceiving(interactionObj.uponReceiving)
.withRequest(interactionObj.withRequest)
.willRespondWith(interactionObj.willRespondWith);

return this.mockService.addInteraction(interaction);
}
Expand Down

0 comments on commit 1c1506d

Please sign in to comment.