From a62d00f6ae3f5eee5c7ffbf9f889e47d8c2e17c9 Mon Sep 17 00:00:00 2001 From: Matt Fellows Date: Tue, 7 Nov 2017 17:26:19 +1100 Subject: [PATCH] fix(examples): update mocha karma example with best practice #122 --- karma/mocha/client-spec.js | 139 +++++++++++++------------------------ 1 file changed, 48 insertions(+), 91 deletions(-) diff --git a/karma/mocha/client-spec.js b/karma/mocha/client-spec.js index 3714f40b0..027982c20 100644 --- a/karma/mocha/client-spec.js +++ b/karma/mocha/client-spec.js @@ -38,30 +38,22 @@ }) }) - it("should say hello", function (done) { + it("should say hello", function () { //Run the tests - client.sayHello() + return client.sayHello() .then(function (data) { expect(JSON.parse(data.responseText)).to.eql({ reply: "Hello" }) - done() - }) - .catch(function (err) { - done(err) + // verify with Pact, and reset expectations + return provider.verify() }) }) - - // verify with Pact, and reset expectations - it('successfully verifies', function () { - return provider.verify() - }) }) describe("findFriendsByAgeAndChildren", function () { - - before(function (done) { - provider + before(function () { + return provider .addInteraction({ uponReceiving: 'a request friends', withRequest: { @@ -92,33 +84,21 @@ } } }) - .then(function () { - done() - }, function (err) { - done(err) - }) }) - it("should return some friends", function (done) { + it("should return some friends", function () { //Run the tests - client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James']) + return client.findFriendsByAgeAndChildren('33', ['Mary Jane', 'James']) .then(function (res) { expect(JSON.parse(res.responseText)).to.eql({ friends: [{ name: 'Sue' }] }) - done() - }) - .catch(function (err) { - done(err) + // verify with Pact, and reset expectations + return provider.verify() }) }) - - // verify with Pact, and reset expectations - it('successfully verifies', function () { - return provider.verify() - }) }) describe("unfriendMe", function () { @@ -128,92 +108,69 @@ }) describe("when I have some friends", function () { - - before(function (done) { + before(function () { //Add interaction - provider.addInteraction({ - state: 'I am friends with Fred', - uponReceiving: 'a request to unfriend', - withRequest: { - method: 'PUT', - path: '/unfriendMe' + return provider.addInteraction({ + state: 'I am friends with Fred', + uponReceiving: 'a request to unfriend', + withRequest: { + method: 'PUT', + path: '/unfriendMe' + }, + willRespondWith: { + status: 200, + headers: { + "Content-Type": "application/json" }, - willRespondWith: { - status: 200, - headers: { - "Content-Type": "application/json" - }, - body: { - reply: "Bye" - } + body: { + reply: "Bye" } - }) - .then(function () { - done() - }, function (err) { - done(err) - }) + } + }) }) - it("should unfriend me", function (done) { + it("should unfriend me", function () { //Run the tests - client.unfriendMe() + return client.unfriendMe() .then(function (res) { expect(JSON.parse(res.responseText)).to.eql({ reply: "Bye" }) - done() - }) - .catch(function (err) { - done(err) + // verify with Pact, and reset expectations + return provider.verify() }) }) - - it('successfully verifies', function () { - return provider.verify() - }) }) // verify with Pact, and reset expectations describe("when there are no friends", function () { - before(function (done) { + before(function () { //Add interaction - provider.addInteraction({ - state: 'I have no friends', - uponReceiving: 'a request to unfriend', - withRequest: { - method: 'PUT', - path: '/unfriendMe' - }, - willRespondWith: { - status: 404, - body: {} - } - }) - .then(function () { - done() - }, function (err) { - done(err) - }) + return provider.addInteraction({ + state: 'I have no friends', + uponReceiving: 'a request to unfriend', + withRequest: { + method: 'PUT', + path: '/unfriendMe' + }, + willRespondWith: { + status: 404, + body: {} + } + }) }) - it("returns an error message", function (done) { + it("returns an error message", function () { //Run the tests - client.unfriendMe().then(function () { - done(new Error('expected request to /unfriend me to fail')) + return client.unfriendMe().then(function () { + throw new Error('expected request to /unfriend me to fail') }, function (e) { expect(e).to.eql('No friends :(') - done() + // verify with Pact, and reset expectations + return provider.verify() }) - - }) - - // verify with Pact, and reset expectations - it('successfully verifies', function () { - return provider.verify() }) }) }) - }) })()