Tooling integration - MSW
Official PactFlow adapter
Create MSW (mock-service-worker) mocks and generate pact contracts from recorded interactions.
With @pactflow/pact-msw-adapter - npm
One of the quickest ways to see this in action in a full CI/CD flow, is to pick the MSW consumer, in our mix & match Bi-Directional Quick-Start Guide
Reference Linksβ
- GitHub https://github.com/pactflow/pact-msw-adapter
- NPM @pactflow/pact-msw-adapter
- PactFlow MSW Bi-Directional demo Project
- Initial Proposal GitHub Issue
Installβ
- Install MSW
- Install @pactflow/pact-msw-adapter
The following will install both msw
and @pactflow/pact-msw-adapter
- npm
- yarn
npm install --save-dev msw @pactflow/pact-msw-adapter
yarn add --dev msw @pactflow/pact-msw-adapter
Define Mocksβ
Define your MSW Mocks
- rest
- graphql
MSW Quick Start Reference - Rest-API
loading...
loading...
MSW Quick Start Reference - GraphQL
Not currently tested, please feel free to help contribute or let us know if you want to see it!
Integrate MSWβ
MSW Quick Start Reference - Integrate MSW with your code
- browser
- server
MSW Quick Start Reference - Browser
loading...
loading...
MSW Quick Start Reference - Node
loading...
Setup pact-msw-adapterβ
We need to setup pact-msw-adapter in our test framework setup hooks.
Please read the reference guide, for up-to-date information on setupPactMswAdapter
configuration options:
- browser
- server
The adapter uses by default nodeβs filesystem to write pact files to disk. This makes it incompatible with browser environments where fs is not available. To overcome this, pact-msw-adapter allows for defining custom functions for writing files to disk.
This example uses Cypress's cy.writeFile
method
loading...
This example uses Jest
loading...
Write your testsβ
No additional magic is needed. You can just start writing your tests.
- browser
- server
This example uses Cypress
loading...
This example uses Jest
loading...
Run your tests in the usual manner for your framework.
You should now see a Pact file generated in your specified folder; the default is ./msw-generated-pacts
- These are now ready for upload to the PactFlow Platform.