Skip to content

Commit

Permalink
feat(example): adding example for the AVA test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
lirantal committed Aug 4, 2017
1 parent 099b268 commit 65e8314
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -441,6 +441,7 @@ Learn everything in Pact JS in 60 minutes: https://github.com/DiUS/pact-workshop
## Examples

* [Complete Example (Node env)](https://github.com/pact-foundation/pact-js/tree/master/examples/e2e)
* [Pact with AVA (Node env)](https://github.com/pact-foundation/pact-js/tree/master/examples/ava)
* [Pact with Jest (Node env)](https://github.com/pact-foundation/pact-js/tree/master/examples/jest)
* [Pact with Mocha](https://github.com/pact-foundation/pact-js/tree/master/examples/mocha)
* [Pact with Karma + Jasmine](https://github.com/pact-foundation/pact-js/tree/master/karma/jasmine)
Expand Down
15 changes: 15 additions & 0 deletions examples/ava/index.js
@@ -0,0 +1,15 @@
'use strict'

const axios = require('axios')

exports.getMeDogs = (endpoint) => {
const url = endpoint.url
const port = endpoint.port

return axios.request({
method: 'GET',
baseURL: `${url}:${port}`,
url: '/dogs',
headers: { 'Accept': 'application/json' }
})
}
14 changes: 14 additions & 0 deletions examples/ava/package.json
@@ -0,0 +1,14 @@
{
"name": "avajs-pact-example",
"version": "1.0.0",
"description": "Ava Pact example",
"main": "index.js",
"scripts": {
"test": "ava"
},
"license": "MIT",
"devDependencies": {
"ava": "^0.21.0",
"axios": "^0.14.0"
}
}
56 changes: 56 additions & 0 deletions examples/ava/test/index.spec.js
@@ -0,0 +1,56 @@
'use strict'

const path = require('path')
const test = require('ava')
const Pact = require('../../../src/pact.js')
const getMeDogs = require('../index').getMeDogs

const url = 'http://localhost'
const port = 8989

const provider = Pact({
port: port,
log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
dir: path.resolve(process.cwd(), 'pacts'),
spec: 2,
consumer: 'MyConsumer',
provider: 'MyProvider'
})

const EXPECTED_BODY = [{dog: 1}]

test.before('setting up Dog API expectations', async () => {

await provider.setup()
const interaction = {
state: 'i have a list of projects',
uponReceiving: 'a request for projects',
withRequest: {
method: 'GET',
path: '/dogs',
headers: {'Accept': 'application/json'}
},
willRespondWith: {
status: 200,
headers: {'Content-Type': 'application/json'},
body: EXPECTED_BODY
}
}

await provider.addInteraction(interaction)
})

test('Dog API returns correct response', async t => {
t.plan(2)

const urlAndPort = {url: url, port: port}
const response = await getMeDogs(urlAndPort)
t.deepEqual(response.data, EXPECTED_BODY)

// verify with Pact, and reset expectations
await t.notThrows(provider.verify())
})

test.always.after('pact.js mock server graceful shutdown', async () => {
await provider.finalize()
})

0 comments on commit 65e8314

Please sign in to comment.