Skip to content
This repository has been archived by the owner on Oct 16, 2023. It is now read-only.

Commit

Permalink
feat(App): enable user to add an Array of interactions at once
Browse files Browse the repository at this point in the history
master

master
  • Loading branch information
Tarcio Saraiva committed Jul 7, 2016
1 parent e8d2983 commit 8931656
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ var PactOpts = {

PactConsumer(PactOpts, function () {

// this is wrapped in a beforeEach block
// thus it runs before all your verify's
addInteraction({
// this is wrapped in a before() block
// it takes an Array of interactions
addInteractions([{
state: 'i have a list of projects',
uponReceiving: 'a request for projects',
withRequest: {
Expand All @@ -112,7 +112,7 @@ PactConsumer(PactOpts, function () {
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: { reply: 'hello' }
}
})
}])

function requestProjects () {
return request.get('http://localhost:' + PactOpts.providerPort + '/projects').set({ 'Accept': 'application/json' })
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
]
},
"dependencies": {
"@pact-foundation/pact-node": "4.4.3",
"@pact-foundation/pact-node": "4.5.0",
"escape-string-regexp": "1.0.5",
"mocha": "2.5.3",
"pact": "pact-foundation/pact-js"
Expand All @@ -75,11 +75,8 @@
"coveralls": "2.11.9",
"express": "4.14.0",
"istanbul": "0.4.4",
"jscpd": "0.6.2",
"jscpd": "0.6.3",
"standard": "7.1.2",
"superagent": "2.0.0"
},
"optionalDependencies": {
"mitm": "1.2.1"
}
}
23 changes: 13 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var Suite = require('mocha/lib/suite')
var escapeRe = require('escape-string-regexp')
var wrapper = require('@pact-foundation/pact-node')
var Common = require('mocha/lib/interfaces/common')
var Promise = require('bluebird')

/**
* BDD-style interface mixed with Pact:
Expand Down Expand Up @@ -162,23 +163,25 @@ module.exports = Mocha.interfaces['bdd'] = function (suite) {
return test
}

context.addInteraction = function (interactionObj) {
context.addInteractions = function (interactions) {
var pactSuite = suites[0]
context.beforeEach(function (done) {
pactSuite.pact.addInteraction(interactionObj)
.then(function () {
done()
})
context.before(function (done) {
var interactionsPromise = interactions.map(function (interaction) {
return pactSuite.pact.addInteraction(interaction)
})

Promise.all(interactionsPromise).then(function () {
done()
})
})
}

context.finalizePact = function () {
var pactSuite = suites[0]
context.after(function (done) {
pactSuite.pact.finalize()
.then(function () {
done()
})
pactSuite.pact.finalize().then(function () {
done()
})
})
}

Expand Down
6 changes: 4 additions & 2 deletions test/consumer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var PactOpts = {

PactConsumer(PactOpts, function () {

addInteraction({
// run wrapped in a before() block
addInteractions([{
state: 'i have a list of projects',
uponReceiving: 'a request for projects',
withRequest: {
Expand All @@ -22,7 +23,7 @@ PactConsumer(PactOpts, function () {
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: { reply: 'hello' }
}
})
}])

function requestProjects () {
return request.get('http://localhost:' + PactOpts.providerPort + '/projects').set({ 'Accept': 'application/json' })
Expand All @@ -33,6 +34,7 @@ PactConsumer(PactOpts, function () {
done()
})

// run wrapped in an after() block
finalizePact()

})
4 changes: 4 additions & 0 deletions test/specHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ var mockServer = wrapper.createServer({

mockServer.start().then(function () {
run()

process.on('SIGINT', function () {
wrapper.removeAllServers()
})
})

0 comments on commit 8931656

Please sign in to comment.