Javascript

Pact JS is currently compliant to Pact Specification Version 2.0.

Quickstart guide (Consumer)

For a Consumer using Jest and node (for example, an app created with create-react-app):

First, you’ll need to add the Pact library:

  1. npm install --save-dev '@pact-foundation/pact'

Then, add the following script to your package.json:

  1. scripts: {
  2. ...
  3. "pactTest":
  4. "export NODE_ENV=pactTest && jest --testRegex \"/*(.test.pact.js)\" --runInBand --setupFiles ./pactSetup.js --setupTestFrameworkScriptFile ./pactTestWrapper.js"
  5. }

You’ll also need to give Pact some metadata about your setup. This is done with pactSetup.js:

  1. const path = require('path');
  2. const { Pact } = require('@pact-foundation/pact');
  3. global.port = 8989;
  4. global.provider = new Pact({
  5. port: global.port,
  6. log: path.resolve(process.cwd(), 'logs', 'mockserver-integration.log'),
  7. dir: path.resolve(process.cwd(), 'pacts'),
  8. spec: 2,
  9. cors: true,
  10. pactfileWriteMode: 'update',
  11. consumer: /* the name of your consumer */,
  12. provider: /* the name of your provider */
  13. });

Global setup for Jest is handled with pactTestWrapper.js:

  1. jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000; //* This is to give the pact mock server time to start
  2. beforeAll(() => provider.setup()); // Create mock provider
  3. afterEach(() => provider.verify()); // Ensure the mock provider verifies expected interactions for each test
  4. afterAll(() => provider.finalize()); // Tear down the mock and write the pact

Then you can write tests of the form <filename>.test.pact.js:

  1. import { Pact } from '@pact-foundation/pact';
  2. import { api } from /* wherever */; // This is your client-side API layer
  3. describe('The API', () => {
  4. let url = 'http://localhost';
  5. // Copy this block once per interaction under test
  6. describe(/* The API interaction being tested in words (string) */, () => {
  7. beforeEach(() => {
  8. const interaction = {
  9. uponReceiving: /* Describe the request in words (string) */,
  10. withRequest: {
  11. method: /* 'GET' or 'POST' or whatever (string) */,
  12. path: /* '/foo/bar' (string) */,
  13. query: /* '?query=parameters' (string) */,
  14. headers: {
  15. Accept: 'application/json'
  16. /* etc */
  17. }
  18. },
  19. willRespondWith: {
  20. status: 200,
  21. headers: {
  22. 'Content-Type': 'application/json'
  23. /* etc */
  24. },
  25. body: /* describe the body (object using the Pact DSL)*/
  26. }
  27. };
  28. return provider.addInteraction(interaction);
  29. });
  30. // add expectations
  31. it(/* describe the test */, done => {
  32. api(url).someCall()
  33. .then(response => {
  34. expect(response).toEqual(/* check the response here, using the default values provided to the Pact DSL */);
  35. })
  36. .then(done);
  37. });
  38. });
  39. });

Once this is all setup, you can run your first pact test:

  1. $ npm run pactTest

If successful, this will produce a pact file in ./pacts/. If the test was unsuccessful, you can view detailed output in ./logs/mockserver-integration.log.

More information

If you need more information, the Pact JS readme has detailed information.

Additionally, these complete examples are useful guides: