-
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: initial ts refactor for TS example
- Loading branch information
Showing
9 changed files
with
210 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters