From 91ef75c55a7921f658f9119d695a078783330b0a Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Wed, 13 Sep 2017 08:36:54 +1000 Subject: [PATCH] feat(typescript): fix pact-web and karma tests --- karma/jasmine/client-spec.js | 54 ++++++++++++++++++------------------ karma/mocha/client-spec.js | 40 +++++++++++++------------- package.json | 4 ++- src/pact-web.d.ts | 28 ------------------- src/pact-web.ts | 17 ++++-------- src/pact.ts | 6 ++-- 6 files changed, 59 insertions(+), 90 deletions(-) delete mode 100644 src/pact-web.d.ts diff --git a/karma/jasmine/client-spec.js b/karma/jasmine/client-spec.js index 9ad05e25b..95d025dd2 100644 --- a/karma/jasmine/client-spec.js +++ b/karma/jasmine/client-spec.js @@ -1,13 +1,13 @@ /*eslint-disable*/ -(function() { +(function () { - describe("Client", function() { + describe("Client", function () { var client, provider - beforeAll(function(done) { + beforeAll(function (done) { client = example.createClient('http://localhost:1234') - provider = Pact({ consumer: 'Karma Jasmine', provider: 'Hello' }) + provider = new Pact.PactWeb({ consumer: 'Karma Jasmine', provider: 'Hello' }) // required for slower Travis CI environment setTimeout(function () { done() }, 2000) @@ -35,10 +35,10 @@ body: { reply: "Hello" } } }) - .then(function () { done() }, function (err) { done.fail(err) }) + .then(function () { done() }, function (err) { done.fail(err) }) }) - it("should say hello", function(done) { + it("should say hello", function (done) { //Run the tests client.sayHello() .then(function (data) { @@ -51,11 +51,11 @@ }) // verify with Pact, and reset expectations - it('successfully verifies', function(done) { + it('successfully verifies', function (done) { provider.verify() - .then(function(a) { + .then(function (a) { done() - }, function(e) { + }, function (e) { done.fail(e) }) }) @@ -64,14 +64,14 @@ describe("findFriendsByAgeAndChildren", function () { beforeAll(function (done) { - provider + provider .addInteraction({ uponReceiving: 'a request friends', withRequest: { method: 'GET', path: '/friends', query: { - age: Pact.Matchers.term({generate: '30', matcher: '\\d+'}), //remember query params are always strings + age: Pact.Matchers.term({ generate: '30', matcher: '\\d+' }), //remember query params are always strings children: ['Mary Jane', 'James'] // specify params with multiple values in an array }, headers: { 'Accept': 'application/json' } @@ -89,11 +89,11 @@ .then(function () { done() }, function (err) { done.fail(err) }) }) - it("should return some friends", function(done) { + it("should return some friends", function (done) { //Run the tests client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James']) .then(function (res) { - expect(JSON.parse(res.responseText)).toEqual({friends: [{ name: 'Sue' }]}) + expect(JSON.parse(res.responseText)).toEqual({ friends: [{ name: 'Sue' }] }) done() }) .catch(function (err) { @@ -103,11 +103,11 @@ // verify with Pact, and reset expectations // verify with Pact, and reset expectations - it('successfully verifies', function(done) { + it('successfully verifies', function (done) { provider.verify() - .then(function(a) { + .then(function (a) { done() - }, function(e) { + }, function (e) { done.fail(e) }) }) @@ -136,10 +136,10 @@ body: { reply: "Bye" } } }) - .then(function () { done() }, function (err) { done.fail(err) }) + .then(function () { done() }, function (err) { done.fail(err) }) }) - it("should unfriend me", function(done) { + it("should unfriend me", function (done) { //Run the tests client.unfriendMe() .then(function (res) { @@ -152,11 +152,11 @@ }) // verify with Pact, and reset expectations - it('successfully verifies', function(done) { + it('successfully verifies', function (done) { provider.verify() - .then(function(a) { + .then(function (a) { done() - }, function(e) { + }, function (e) { done.fail(e) }) }) @@ -178,14 +178,14 @@ body: { error: "No friends :(" } } }) - .then(function () { done() }, function (err) { done.fail(err) }) + .then(function () { done() }, function (err) { done.fail(err) }) }) it("returns an error message", function (done) { //Run the tests - client.unfriendMe().then(function() { + client.unfriendMe().then(function () { done(new Error('expected request to /unfriend me to fail')) - }, function(e) { + }, function (e) { expect(e.status).toEqual(404) expect(JSON.parse(e.responseText).error).toEqual('No friends :(') done() @@ -195,11 +195,11 @@ // verify with Pact, and reset expectations // verify with Pact, and reset expectations - it('successfully verifies', function(done) { + it('successfully verifies', function (done) { provider.verify() - .then(function(a) { + .then(function (a) { done() - }, function(e) { + }, function (e) { done.fail(e) }) }) diff --git a/karma/mocha/client-spec.js b/karma/mocha/client-spec.js index 3f8b54442..94643759a 100644 --- a/karma/mocha/client-spec.js +++ b/karma/mocha/client-spec.js @@ -1,13 +1,13 @@ /*eslint-disable*/ -(function() { +(function () { - describe("Client", function() { + describe("Client", function () { var client, provider - before(function(done) { + before(function (done) { client = example.createClient('http://localhost:1234') - provider = Pact({ consumer: 'Karma Mocha', provider: 'Hello' }) + provider = new Pact.PactWeb({ consumer: 'Karma Mocha', provider: 'Hello' }) // required for slower Travis CI environment setTimeout(function () { done() }, 1000) }) @@ -31,10 +31,10 @@ body: { reply: "Hello" } } }) - .then(function () { done() }, function (err) { done(err) }) + .then(function () { done() }, function (err) { done(err) }) }) - it("should say hello", function(done) { + it("should say hello", function (done) { //Run the tests client.sayHello() .then(function (data) { @@ -47,20 +47,20 @@ }) // verify with Pact, and reset expectations - it('successfully verifies', function() { return provider.verify() }) + it('successfully verifies', function () { return provider.verify() }) }) describe("findFriendsByAgeAndChildren", function () { before(function (done) { - provider + provider .addInteraction({ uponReceiving: 'a request friends', withRequest: { method: 'GET', path: '/friends', query: { - age: Pact.Matchers.term({generate: '30', matcher: '\\d+'}), //remember query params are always strings + age: Pact.Matchers.term({ generate: '30', matcher: '\\d+' }), //remember query params are always strings children: ['Mary Jane', 'James'] // specify params with multiple values in an array }, headers: { 'Accept': 'application/json' } @@ -78,11 +78,11 @@ .then(function () { done() }, function (err) { done(err) }) }) - it("should return some friends", function(done) { + it("should return some friends", function (done) { //Run the tests client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James']) .then(function (res) { - expect(JSON.parse(res.responseText)).to.eql({friends: [{ name: 'Sue' }]}) + expect(JSON.parse(res.responseText)).to.eql({ friends: [{ name: 'Sue' }] }) done() }) .catch(function (err) { @@ -91,12 +91,12 @@ }) // verify with Pact, and reset expectations - it('successfully verifies', function() { return provider.verify() }) + it('successfully verifies', function () { return provider.verify() }) }) describe("unfriendMe", function () { - afterEach(function() { + afterEach(function () { return provider.removeInteractions() }) @@ -117,10 +117,10 @@ body: { reply: "Bye" } } }) - .then(function () { done() }, function (err) { done(err) }) + .then(function () { done() }, function (err) { done(err) }) }) - it("should unfriend me", function(done) { + it("should unfriend me", function (done) { //Run the tests client.unfriendMe() .then(function (res) { @@ -132,7 +132,7 @@ }) }) - it('successfully verifies', function() { return provider.verify() }) + it('successfully verifies', function () { return provider.verify() }) }) // verify with Pact, and reset expectations @@ -151,14 +151,14 @@ body: {} } }) - .then(function () { done() }, function (err) { done(err) }) + .then(function () { done() }, function (err) { done(err) }) }) it("returns an error message", function (done) { //Run the tests - client.unfriendMe().then(function() { + client.unfriendMe().then(function () { done(new Error('expected request to /unfriend me to fail')) - }, function(e) { + }, function (e) { expect(e).to.eql('No friends :(') done() }) @@ -166,7 +166,7 @@ }) // verify with Pact, and reset expectations - it('successfully verifies', function() { return provider.verify() }) + it('successfully verifies', function () { return provider.verify() }) }) }) diff --git a/package.json b/package.json index 9dcebabd5..b681e1532 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,8 @@ "test:e2e-examples": "cd examples/e2e && npm i && npm t", "test:karma": "npm run test:karma:jasmine && npm run test:karma:mocha", "test:karma:jasmine": "karma start ./karma/jasmine/karma.conf.js", - "test:karma:mocha": "karma start ./karma/mocha/karma.conf.js" + "test:karma:mocha": "karma start ./karma/mocha/karma.conf.js", + "webpack": "webpack --config ./config/webpack.web.config.js" }, "nyc": { "include": [ @@ -122,6 +123,7 @@ "coveralls": "2.x", "documentation": "4.0.0-beta9", "enhanced-resolve": "^3.4.1", + "es6-promise": "^4.1.1", "imports-loader": "0.x", "istanbul": "0.4.x", "jasmine-core": "2.x", diff --git a/src/pact-web.d.ts b/src/pact-web.d.ts deleted file mode 100644 index b68918732..000000000 --- a/src/pact-web.d.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { InteractionObject } from './dsl/interaction'; -import * as _Matchers from './dsl/matchers'; -import {PactfileWriteMode} from './dsl/mockService'; - -declare function pact(opts: pact.PactWebOptions): pact.PactWebProvider; - -declare namespace pact { - export interface PactWebOptions { - consumer: string; - provider: string; - port?: number; - host?: string; - ssl?: boolean; - pactfileWriteMode?: PactfileWriteMode; - } - - export interface PactWebProvider { - addInteraction(interactionObj: InteractionObject): Promise; - verify(): Promise; - finalize(): Promise; - writePact(): Promise; - removeInteractions(): Promise; - } - - export const Matchers: typeof _Matchers; -} - -export = pact; diff --git a/src/pact-web.ts b/src/pact-web.ts index a4ba2c6fa..c9dc1df4a 100644 --- a/src/pact-web.ts +++ b/src/pact-web.ts @@ -5,7 +5,7 @@ 'use strict' -import { isNil } from 'lodash'; +import { isEmpty } from 'lodash'; import { isPortAvailable } from './common/net'; import { MockService, PactfileWriteMode } from './dsl/mockService'; import { Interaction, InteractionObject } from './dsl/interaction'; @@ -16,13 +16,8 @@ import * as Matchers from './dsl/matchers'; import * as Verifier from './dsl/verifier'; import * as clc from 'cli-color'; import { logger } from './common/logger'; -// TODO: is this still needed if TypeScript is transpiling down? -// import { polyfill } from 'es6-promise'; -// polyfill(); - - -// TODO: Could probably use class inheritence or mixins -// to reduce boilerplate code here +import { polyfill } from 'es6-promise'; +polyfill(); /** * Creates a new {@link PactProvider}. @@ -55,13 +50,13 @@ export class PactWeb { pactfileWriteMode: 'overwrite' } as PactOptions; - this.opts = { ...defaults, config } as PactOptionsComplete; + this.opts = { ...defaults, ...config } as PactOptionsComplete; - if (isNil(this.opts.consumer)) { + if (isEmpty(this.opts.consumer)) { throw new Error('You must specify a Consumer for this pact.'); } - if (isNil(this.opts.provider)) { + if (isEmpty(this.opts.provider)) { throw new Error('You must specify a Provider for this pact.'); } diff --git a/src/pact.ts b/src/pact.ts index 71306f211..a5e36633b 100644 --- a/src/pact.ts +++ b/src/pact.ts @@ -3,7 +3,7 @@ * @module Pact */ -import { isNil } from 'lodash'; +import { isEmpty } from 'lodash'; import { isPortAvailable } from './common/net'; import { MockService, PactfileWriteMode } from './dsl/mockService'; import { Interaction, InteractionObject } from './dsl/interaction'; @@ -48,11 +48,11 @@ export class Pact { this.opts = { ...defaults, ...config } as PactOptionsComplete; - if (this.opts.consumer === '') { + if (isEmpty(this.opts.consumer)) { throw new Error('You must specify a Consumer for this pact.'); } - if (this.opts.provider === '') { + if (isEmpty(this.opts.provider)) { throw new Error('You must specify a Provider for this pact.'); }