Skip to content

Commit

Permalink
feat(typescript): fix pact-web and karma tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed Sep 12, 2017
1 parent 9ee9813 commit 91ef75c
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 90 deletions.
54 changes: 27 additions & 27 deletions karma/jasmine/client-spec.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
})
})
Expand All @@ -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' }
Expand All @@ -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) {
Expand All @@ -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)
})
})
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
})
})
Expand All @@ -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()
Expand All @@ -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)
})
})
Expand Down
40 changes: 20 additions & 20 deletions karma/mocha/client-spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
Expand All @@ -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) {
Expand All @@ -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' }
Expand All @@ -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) {
Expand All @@ -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()
})

Expand All @@ -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) {
Expand All @@ -132,7 +132,7 @@
})
})

it('successfully verifies', function() { return provider.verify() })
it('successfully verifies', function () { return provider.verify() })
})

// verify with Pact, and reset expectations
Expand All @@ -151,22 +151,22 @@
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()
})

})

// verify with Pact, and reset expectations
it('successfully verifies', function() { return provider.verify() })
it('successfully verifies', function () { return provider.verify() })
})
})

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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",
Expand Down
28 changes: 0 additions & 28 deletions src/pact-web.d.ts

This file was deleted.

17 changes: 6 additions & 11 deletions src/pact-web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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}.
Expand Down Expand Up @@ -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.');
}

Expand Down
6 changes: 3 additions & 3 deletions src/pact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.');
}

Expand Down

0 comments on commit 91ef75c

Please sign in to comment.