Skip to content

Instantly share code, notes, and snippets.

@smucode
Created February 15, 2018 21:39
Show Gist options
  • Select an option

  • Save smucode/411ce41a039e365d23b5c0ab6feaec0e to your computer and use it in GitHub Desktop.

Select an option

Save smucode/411ce41a039e365d23b5c0ab6feaec0e to your computer and use it in GitHub Desktop.
const { Pact, Matchers } = require('@pact-foundation/pact')
const path = require('path')
const R = require('ramda')
const { eachLike, term, somethingLike: like } = Matchers
const providerPort = 1234
//eslint-disable-next-line fp/no-mutation
module.exports.encodeQuery = R.pipe(JSON.stringify, encodeURIComponent)
const provider = new Pact({
consumer: 'frontend',
provider: 'api',
providerPort,
log: path.resolve(process.cwd(), 'test/logs', 'pact.log'),
dir: path.resolve(process.cwd(), 'test/pacts'),
logLevel: 'WARN',
spec: 2,
pactfileWriteMode: 'update'
})
// apiTest({
// state: '',
// interactions: [
// {
// request: {
// path: '',
// method: 'POST',
// body: {}
// },
// response: {
// status: 200,
// body: {}
// }
// }
// ],
// tests: [
// {
// name: '',
// elmResponsePort: '',
// elmRequestPort: '',
// elmRequestData: []
// }
// ]
// })
//eslint-disable-next-line fp/no-mutation
module.exports.apiTest = config =>
(config.disabled ? xdescribe : describe)(config.state, () => {
//eslint-disable-next-line
before(() =>
provider.setup().then(() =>
config.interactions.forEach(i =>
provider.addInteraction({
state: config.state,
uponReceiving: i.request.method + ':' + i.request.path,
withRequest: {
method: i.request.method,
path: i.request.path,
...(i.request.body ? { body: i.request.body } : {}),
...(i.request.query ? { query: i.request.query } : {}),
headers: {
Accept: 'application/json',
...(i.request.method === 'POST'
? { 'Content-Type': 'application/json' }
: {})
}
},
willRespondWith: {
status: i.response.status,
headers: { 'Content-Type': 'application/json; charset=utf-8' },
body: i.response.body
}
})
)
)
)
config.tests.forEach(t =>
it(t.name, () => {
return new Promise((resolve, reject) => {
// eslint-disable-next-line
global.XMLHttpRequest = require('xhr2')
const app = require('elm-pact-test-api').Main.worker()
app.ports[t.elmResponsePort].subscribe(
status => (status === 'OK' ? resolve() : reject(status))
)
app.ports[t.elmRequestPort].send(t.elmRequestData)
})
})
)
//eslint-disable-next-line
after(() => provider.verify() && provider.finalize())
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment