Created
July 26, 2019 10:36
-
-
Save q-ode/119ded3766df4e6f036dcf75f36e3deb to your computer and use it in GitHub Desktop.
Sample Pact Generation using Jest
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
import { Pact } from '@pact-foundation/pact'; | |
import path from 'path'; | |
import axios from 'axios'; | |
describe('sampleProvider', () => { | |
const port = 1234; | |
const providerBaseUrl = `http://localhost:${port}`; | |
const provider = new Pact({ | |
consumer: 'sample-consumer', | |
provider: 'sample-provider', | |
port, | |
dir: path.resolve(process.cwd(), '.contracts'), | |
pactfileWriteMode: 'update', | |
}); | |
beforeAll(async () => { | |
await provider.setup(); | |
}); | |
afterAll(async () => { | |
await provider.finalize(); | |
}); | |
it('returns the expected response', async () => { | |
await provider.addInteraction({ | |
uponReceiving: 'a request to the base endpoint', | |
withRequest: { | |
method: 'GET', | |
path: '/', | |
}, | |
willRespondWith: { | |
status: 200, | |
body: { | |
message: 'Hello world!', | |
}, | |
}, | |
state: undefined, // State handlers are not required but I think the type definition in the library is buggy | |
}); | |
// Execute your call | |
await axios.get(providerBaseUrl); | |
await provider.verify(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment