Skip to content

Instantly share code, notes, and snippets.

@christopher-francisco
Last active May 18, 2018 14:46
Show Gist options
  • Save christopher-francisco/0662f8d4b2c1289e4a5fc49a2348131c to your computer and use it in GitHub Desktop.
Save christopher-francisco/0662f8d4b2c1289e4a5fc49a2348131c to your computer and use it in GitHub Desktop.
Jest + PactJS
export default {
getCustomers(port) {
return fetch(`http://localhost:${port}`)
.then(response => response.json())
},
};
/**
* @jest-environment node
*/
import {
addFetchNode,
removeNodeFetch,
getPort,
createPact,
} from './pact-utils';
import apiClient from './api-client';
describe('apiClient', () => {
let provider;
let port;
beforeEach(async () => {
// there is no `fetch` when using `node` environment, so I just manually add `node-fetch` to `global.fetch`
// I'm not sure I can use Pact-JS with jsdom, so that I wouldn't have to do this??
addNodeFetch();
port = getPort(); // returns a port++, so that each test case has a different port
provider = createPact(port); // returns a new Pact() with configs and the passed port
await provider.setup();
});
afterEach(async () => {
removeNodeFetch();
await provider.verify();
await provider.finalize();
});
it('returns a list of customers', async () => {
provider.addInteraction({ /* interaction here */ });
// I still don't have a clean way to load the port, so I'm passing it for now.
const customers = await apiClient.getCustomers(port);
// expectations here..
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment