Skip to content

Commit 91ef75c

Browse files
committed
feat(typescript): fix pact-web and karma tests
1 parent 9ee9813 commit 91ef75c

6 files changed

Lines changed: 59 additions & 90 deletions

File tree

karma/jasmine/client-spec.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/*eslint-disable*/
2-
(function() {
2+
(function () {
33

4-
describe("Client", function() {
4+
describe("Client", function () {
55

66
var client, provider
77

8-
beforeAll(function(done) {
8+
beforeAll(function (done) {
99
client = example.createClient('http://localhost:1234')
10-
provider = Pact({ consumer: 'Karma Jasmine', provider: 'Hello' })
10+
provider = new Pact.PactWeb({ consumer: 'Karma Jasmine', provider: 'Hello' })
1111

1212
// required for slower Travis CI environment
1313
setTimeout(function () { done() }, 2000)
@@ -35,10 +35,10 @@
3535
body: { reply: "Hello" }
3636
}
3737
})
38-
.then(function () { done() }, function (err) { done.fail(err) })
38+
.then(function () { done() }, function (err) { done.fail(err) })
3939
})
4040

41-
it("should say hello", function(done) {
41+
it("should say hello", function (done) {
4242
//Run the tests
4343
client.sayHello()
4444
.then(function (data) {
@@ -51,11 +51,11 @@
5151
})
5252

5353
// verify with Pact, and reset expectations
54-
it('successfully verifies', function(done) {
54+
it('successfully verifies', function (done) {
5555
provider.verify()
56-
.then(function(a) {
56+
.then(function (a) {
5757
done()
58-
}, function(e) {
58+
}, function (e) {
5959
done.fail(e)
6060
})
6161
})
@@ -64,14 +64,14 @@
6464
describe("findFriendsByAgeAndChildren", function () {
6565

6666
beforeAll(function (done) {
67-
provider
67+
provider
6868
.addInteraction({
6969
uponReceiving: 'a request friends',
7070
withRequest: {
7171
method: 'GET',
7272
path: '/friends',
7373
query: {
74-
age: Pact.Matchers.term({generate: '30', matcher: '\\d+'}), //remember query params are always strings
74+
age: Pact.Matchers.term({ generate: '30', matcher: '\\d+' }), //remember query params are always strings
7575
children: ['Mary Jane', 'James'] // specify params with multiple values in an array
7676
},
7777
headers: { 'Accept': 'application/json' }
@@ -89,11 +89,11 @@
8989
.then(function () { done() }, function (err) { done.fail(err) })
9090
})
9191

92-
it("should return some friends", function(done) {
92+
it("should return some friends", function (done) {
9393
//Run the tests
9494
client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James'])
9595
.then(function (res) {
96-
expect(JSON.parse(res.responseText)).toEqual({friends: [{ name: 'Sue' }]})
96+
expect(JSON.parse(res.responseText)).toEqual({ friends: [{ name: 'Sue' }] })
9797
done()
9898
})
9999
.catch(function (err) {
@@ -103,11 +103,11 @@
103103

104104
// verify with Pact, and reset expectations
105105
// verify with Pact, and reset expectations
106-
it('successfully verifies', function(done) {
106+
it('successfully verifies', function (done) {
107107
provider.verify()
108-
.then(function(a) {
108+
.then(function (a) {
109109
done()
110-
}, function(e) {
110+
}, function (e) {
111111
done.fail(e)
112112
})
113113
})
@@ -136,10 +136,10 @@
136136
body: { reply: "Bye" }
137137
}
138138
})
139-
.then(function () { done() }, function (err) { done.fail(err) })
139+
.then(function () { done() }, function (err) { done.fail(err) })
140140
})
141141

142-
it("should unfriend me", function(done) {
142+
it("should unfriend me", function (done) {
143143
//Run the tests
144144
client.unfriendMe()
145145
.then(function (res) {
@@ -152,11 +152,11 @@
152152
})
153153

154154
// verify with Pact, and reset expectations
155-
it('successfully verifies', function(done) {
155+
it('successfully verifies', function (done) {
156156
provider.verify()
157-
.then(function(a) {
157+
.then(function (a) {
158158
done()
159-
}, function(e) {
159+
}, function (e) {
160160
done.fail(e)
161161
})
162162
})
@@ -178,14 +178,14 @@
178178
body: { error: "No friends :(" }
179179
}
180180
})
181-
.then(function () { done() }, function (err) { done.fail(err) })
181+
.then(function () { done() }, function (err) { done.fail(err) })
182182
})
183183

184184
it("returns an error message", function (done) {
185185
//Run the tests
186-
client.unfriendMe().then(function() {
186+
client.unfriendMe().then(function () {
187187
done(new Error('expected request to /unfriend me to fail'))
188-
}, function(e) {
188+
}, function (e) {
189189
expect(e.status).toEqual(404)
190190
expect(JSON.parse(e.responseText).error).toEqual('No friends :(')
191191
done()
@@ -195,11 +195,11 @@
195195

196196
// verify with Pact, and reset expectations
197197
// verify with Pact, and reset expectations
198-
it('successfully verifies', function(done) {
198+
it('successfully verifies', function (done) {
199199
provider.verify()
200-
.then(function(a) {
200+
.then(function (a) {
201201
done()
202-
}, function(e) {
202+
}, function (e) {
203203
done.fail(e)
204204
})
205205
})

karma/mocha/client-spec.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/*eslint-disable*/
2-
(function() {
2+
(function () {
33

4-
describe("Client", function() {
4+
describe("Client", function () {
55

66
var client, provider
77

8-
before(function(done) {
8+
before(function (done) {
99
client = example.createClient('http://localhost:1234')
10-
provider = Pact({ consumer: 'Karma Mocha', provider: 'Hello' })
10+
provider = new Pact.PactWeb({ consumer: 'Karma Mocha', provider: 'Hello' })
1111
// required for slower Travis CI environment
1212
setTimeout(function () { done() }, 1000)
1313
})
@@ -31,10 +31,10 @@
3131
body: { reply: "Hello" }
3232
}
3333
})
34-
.then(function () { done() }, function (err) { done(err) })
34+
.then(function () { done() }, function (err) { done(err) })
3535
})
3636

37-
it("should say hello", function(done) {
37+
it("should say hello", function (done) {
3838
//Run the tests
3939
client.sayHello()
4040
.then(function (data) {
@@ -47,20 +47,20 @@
4747
})
4848

4949
// verify with Pact, and reset expectations
50-
it('successfully verifies', function() { return provider.verify() })
50+
it('successfully verifies', function () { return provider.verify() })
5151
})
5252

5353
describe("findFriendsByAgeAndChildren", function () {
5454

5555
before(function (done) {
56-
provider
56+
provider
5757
.addInteraction({
5858
uponReceiving: 'a request friends',
5959
withRequest: {
6060
method: 'GET',
6161
path: '/friends',
6262
query: {
63-
age: Pact.Matchers.term({generate: '30', matcher: '\\d+'}), //remember query params are always strings
63+
age: Pact.Matchers.term({ generate: '30', matcher: '\\d+' }), //remember query params are always strings
6464
children: ['Mary Jane', 'James'] // specify params with multiple values in an array
6565
},
6666
headers: { 'Accept': 'application/json' }
@@ -78,11 +78,11 @@
7878
.then(function () { done() }, function (err) { done(err) })
7979
})
8080

81-
it("should return some friends", function(done) {
81+
it("should return some friends", function (done) {
8282
//Run the tests
8383
client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James'])
8484
.then(function (res) {
85-
expect(JSON.parse(res.responseText)).to.eql({friends: [{ name: 'Sue' }]})
85+
expect(JSON.parse(res.responseText)).to.eql({ friends: [{ name: 'Sue' }] })
8686
done()
8787
})
8888
.catch(function (err) {
@@ -91,12 +91,12 @@
9191
})
9292

9393
// verify with Pact, and reset expectations
94-
it('successfully verifies', function() { return provider.verify() })
94+
it('successfully verifies', function () { return provider.verify() })
9595
})
9696

9797
describe("unfriendMe", function () {
9898

99-
afterEach(function() {
99+
afterEach(function () {
100100
return provider.removeInteractions()
101101
})
102102

@@ -117,10 +117,10 @@
117117
body: { reply: "Bye" }
118118
}
119119
})
120-
.then(function () { done() }, function (err) { done(err) })
120+
.then(function () { done() }, function (err) { done(err) })
121121
})
122122

123-
it("should unfriend me", function(done) {
123+
it("should unfriend me", function (done) {
124124
//Run the tests
125125
client.unfriendMe()
126126
.then(function (res) {
@@ -132,7 +132,7 @@
132132
})
133133
})
134134

135-
it('successfully verifies', function() { return provider.verify() })
135+
it('successfully verifies', function () { return provider.verify() })
136136
})
137137

138138
// verify with Pact, and reset expectations
@@ -151,22 +151,22 @@
151151
body: {}
152152
}
153153
})
154-
.then(function () { done() }, function (err) { done(err) })
154+
.then(function () { done() }, function (err) { done(err) })
155155
})
156156

157157
it("returns an error message", function (done) {
158158
//Run the tests
159-
client.unfriendMe().then(function() {
159+
client.unfriendMe().then(function () {
160160
done(new Error('expected request to /unfriend me to fail'))
161-
}, function(e) {
161+
}, function (e) {
162162
expect(e).to.eql('No friends :(')
163163
done()
164164
})
165165

166166
})
167167

168168
// verify with Pact, and reset expectations
169-
it('successfully verifies', function() { return provider.verify() })
169+
it('successfully verifies', function () { return provider.verify() })
170170
})
171171
})
172172

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"test:e2e-examples": "cd examples/e2e && npm i && npm t",
2323
"test:karma": "npm run test:karma:jasmine && npm run test:karma:mocha",
2424
"test:karma:jasmine": "karma start ./karma/jasmine/karma.conf.js",
25-
"test:karma:mocha": "karma start ./karma/mocha/karma.conf.js"
25+
"test:karma:mocha": "karma start ./karma/mocha/karma.conf.js",
26+
"webpack": "webpack --config ./config/webpack.web.config.js"
2627
},
2728
"nyc": {
2829
"include": [
@@ -122,6 +123,7 @@
122123
"coveralls": "2.x",
123124
"documentation": "4.0.0-beta9",
124125
"enhanced-resolve": "^3.4.1",
126+
"es6-promise": "^4.1.1",
125127
"imports-loader": "0.x",
126128
"istanbul": "0.4.x",
127129
"jasmine-core": "2.x",

src/pact-web.d.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/pact-web.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict'
77

8-
import { isNil } from 'lodash';
8+
import { isEmpty } from 'lodash';
99
import { isPortAvailable } from './common/net';
1010
import { MockService, PactfileWriteMode } from './dsl/mockService';
1111
import { Interaction, InteractionObject } from './dsl/interaction';
@@ -16,13 +16,8 @@ import * as Matchers from './dsl/matchers';
1616
import * as Verifier from './dsl/verifier';
1717
import * as clc from 'cli-color';
1818
import { logger } from './common/logger';
19-
// TODO: is this still needed if TypeScript is transpiling down?
20-
// import { polyfill } from 'es6-promise';
21-
// polyfill();
22-
23-
24-
// TODO: Could probably use class inheritence or mixins
25-
// to reduce boilerplate code here
19+
import { polyfill } from 'es6-promise';
20+
polyfill();
2621

2722
/**
2823
* Creates a new {@link PactProvider}.
@@ -55,13 +50,13 @@ export class PactWeb {
5550
pactfileWriteMode: 'overwrite'
5651
} as PactOptions;
5752

58-
this.opts = { ...defaults, config } as PactOptionsComplete;
53+
this.opts = { ...defaults, ...config } as PactOptionsComplete;
5954

60-
if (isNil(this.opts.consumer)) {
55+
if (isEmpty(this.opts.consumer)) {
6156
throw new Error('You must specify a Consumer for this pact.');
6257
}
6358

64-
if (isNil(this.opts.provider)) {
59+
if (isEmpty(this.opts.provider)) {
6560
throw new Error('You must specify a Provider for this pact.');
6661
}
6762

src/pact.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module Pact
44
*/
55

6-
import { isNil } from 'lodash';
6+
import { isEmpty } from 'lodash';
77
import { isPortAvailable } from './common/net';
88
import { MockService, PactfileWriteMode } from './dsl/mockService';
99
import { Interaction, InteractionObject } from './dsl/interaction';
@@ -48,11 +48,11 @@ export class Pact {
4848

4949
this.opts = { ...defaults, ...config } as PactOptionsComplete;
5050

51-
if (this.opts.consumer === '') {
51+
if (isEmpty(this.opts.consumer)) {
5252
throw new Error('You must specify a Consumer for this pact.');
5353
}
5454

55-
if (this.opts.provider === '') {
55+
if (isEmpty(this.opts.provider)) {
5656
throw new Error('You must specify a Provider for this pact.');
5757
}
5858

0 commit comments

Comments
 (0)