Skip to content

Commit a62d00f

Browse files
committed
fix(examples): update mocha karma example with best practice #122
1 parent 93cba30 commit a62d00f

File tree

1 file changed

+48
-91
lines changed

1 file changed

+48
-91
lines changed

karma/mocha/client-spec.js

Lines changed: 48 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,22 @@
3838
})
3939
})
4040

41-
it("should say hello", function (done) {
41+
it("should say hello", function () {
4242
//Run the tests
43-
client.sayHello()
43+
return client.sayHello()
4444
.then(function (data) {
4545
expect(JSON.parse(data.responseText)).to.eql({
4646
reply: "Hello"
4747
})
48-
done()
49-
})
50-
.catch(function (err) {
51-
done(err)
48+
// verify with Pact, and reset expectations
49+
return provider.verify()
5250
})
5351
})
54-
55-
// verify with Pact, and reset expectations
56-
it('successfully verifies', function () {
57-
return provider.verify()
58-
})
5952
})
6053

6154
describe("findFriendsByAgeAndChildren", function () {
62-
63-
before(function (done) {
64-
provider
55+
before(function () {
56+
return provider
6557
.addInteraction({
6658
uponReceiving: 'a request friends',
6759
withRequest: {
@@ -92,33 +84,21 @@
9284
}
9385
}
9486
})
95-
.then(function () {
96-
done()
97-
}, function (err) {
98-
done(err)
99-
})
10087
})
10188

102-
it("should return some friends", function (done) {
89+
it("should return some friends", function () {
10390
//Run the tests
104-
client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James'])
91+
return client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James'])
10592
.then(function (res) {
10693
expect(JSON.parse(res.responseText)).to.eql({
10794
friends: [{
10895
name: 'Sue'
10996
}]
11097
})
111-
done()
112-
})
113-
.catch(function (err) {
114-
done(err)
98+
// verify with Pact, and reset expectations
99+
return provider.verify()
115100
})
116101
})
117-
118-
// verify with Pact, and reset expectations
119-
it('successfully verifies', function () {
120-
return provider.verify()
121-
})
122102
})
123103

124104
describe("unfriendMe", function () {
@@ -128,92 +108,69 @@
128108
})
129109

130110
describe("when I have some friends", function () {
131-
132-
before(function (done) {
111+
before(function () {
133112
//Add interaction
134-
provider.addInteraction({
135-
state: 'I am friends with Fred',
136-
uponReceiving: 'a request to unfriend',
137-
withRequest: {
138-
method: 'PUT',
139-
path: '/unfriendMe'
113+
return provider.addInteraction({
114+
state: 'I am friends with Fred',
115+
uponReceiving: 'a request to unfriend',
116+
withRequest: {
117+
method: 'PUT',
118+
path: '/unfriendMe'
119+
},
120+
willRespondWith: {
121+
status: 200,
122+
headers: {
123+
"Content-Type": "application/json"
140124
},
141-
willRespondWith: {
142-
status: 200,
143-
headers: {
144-
"Content-Type": "application/json"
145-
},
146-
body: {
147-
reply: "Bye"
148-
}
125+
body: {
126+
reply: "Bye"
149127
}
150-
})
151-
.then(function () {
152-
done()
153-
}, function (err) {
154-
done(err)
155-
})
128+
}
129+
})
156130
})
157131

158-
it("should unfriend me", function (done) {
132+
it("should unfriend me", function () {
159133
//Run the tests
160-
client.unfriendMe()
134+
return client.unfriendMe()
161135
.then(function (res) {
162136
expect(JSON.parse(res.responseText)).to.eql({
163137
reply: "Bye"
164138
})
165-
done()
166-
})
167-
.catch(function (err) {
168-
done(err)
139+
// verify with Pact, and reset expectations
140+
return provider.verify()
169141
})
170142
})
171-
172-
it('successfully verifies', function () {
173-
return provider.verify()
174-
})
175143
})
176144

177145
// verify with Pact, and reset expectations
178146
describe("when there are no friends", function () {
179-
before(function (done) {
147+
before(function () {
180148
//Add interaction
181-
provider.addInteraction({
182-
state: 'I have no friends',
183-
uponReceiving: 'a request to unfriend',
184-
withRequest: {
185-
method: 'PUT',
186-
path: '/unfriendMe'
187-
},
188-
willRespondWith: {
189-
status: 404,
190-
body: {}
191-
}
192-
})
193-
.then(function () {
194-
done()
195-
}, function (err) {
196-
done(err)
197-
})
149+
return provider.addInteraction({
150+
state: 'I have no friends',
151+
uponReceiving: 'a request to unfriend',
152+
withRequest: {
153+
method: 'PUT',
154+
path: '/unfriendMe'
155+
},
156+
willRespondWith: {
157+
status: 404,
158+
body: {}
159+
}
160+
})
198161
})
199162

200-
it("returns an error message", function (done) {
163+
it("returns an error message", function () {
201164
//Run the tests
202-
client.unfriendMe().then(function () {
203-
done(new Error('expected request to /unfriend me to fail'))
165+
return client.unfriendMe().then(function () {
166+
throw new Error('expected request to /unfriend me to fail')
204167
}, function (e) {
205168
expect(e).to.eql('No friends :(')
206-
done()
169+
// verify with Pact, and reset expectations
170+
return provider.verify()
207171
})
208-
209-
})
210-
211-
// verify with Pact, and reset expectations
212-
it('successfully verifies', function () {
213-
return provider.verify()
214172
})
215173
})
216174
})
217-
218175
})
219176
})()

0 commit comments

Comments
 (0)