Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(example): adding example for the AVA test framework
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' } | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
}) |