Skip to content

Instantly share code, notes, and snippets.

@q-ode
Created July 26, 2019 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save q-ode/119ded3766df4e6f036dcf75f36e3deb to your computer and use it in GitHub Desktop.
Save q-ode/119ded3766df4e6f036dcf75f36e3deb to your computer and use it in GitHub Desktop.
Sample Pact Generation using Jest
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