@pact-foundation/pact 10.0.0-beta.58 → 10.0.0-beta.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/CHANGELOG.md +36 -2
  2. package/CODE_OF_CONDUCT.md +1 -1
  3. package/MIGRATION.md +24 -1
  4. package/README.md +174 -1371
  5. package/package.json +26 -38
  6. package/src/common/logger.js +5 -1
  7. package/src/common/logger.js.map +1 -1
  8. package/src/common/net.d.ts +1 -0
  9. package/src/common/net.js +16 -2
  10. package/src/common/net.js.map +1 -1
  11. package/src/common/request.d.ts +0 -1
  12. package/src/common/request.js +78 -40
  13. package/src/common/request.js.map +1 -1
  14. package/src/common/spec.d.ts +2 -0
  15. package/src/common/spec.js +22 -0
  16. package/src/common/spec.js.map +1 -0
  17. package/src/dsl/apolloGraphql.spec.js +24 -0
  18. package/src/dsl/apolloGraphql.spec.js.map +1 -1
  19. package/src/dsl/graphql.d.ts +2 -2
  20. package/src/dsl/graphql.js +1 -0
  21. package/src/dsl/graphql.js.map +1 -1
  22. package/src/dsl/graphql.spec.js +77 -2
  23. package/src/dsl/graphql.spec.js.map +1 -1
  24. package/src/dsl/interaction.d.ts +13 -7
  25. package/src/dsl/interaction.js +30 -1
  26. package/src/dsl/interaction.js.map +1 -1
  27. package/src/dsl/interaction.spec.js +21 -31
  28. package/src/dsl/interaction.spec.js.map +1 -1
  29. package/src/dsl/matchers.d.ts +27 -32
  30. package/src/dsl/matchers.js +24 -23
  31. package/src/dsl/matchers.js.map +1 -1
  32. package/src/dsl/matchers.spec.js +101 -96
  33. package/src/dsl/matchers.spec.js.map +1 -1
  34. package/src/dsl/message.d.ts +19 -13
  35. package/src/dsl/mockService.d.ts +1 -46
  36. package/src/dsl/mockService.js +0 -72
  37. package/src/dsl/mockService.js.map +1 -1
  38. package/src/dsl/verifier/index.js +5 -1
  39. package/src/dsl/verifier/index.js.map +1 -1
  40. package/src/dsl/verifier/proxy/index.js +5 -1
  41. package/src/dsl/verifier/proxy/index.js.map +1 -1
  42. package/src/dsl/verifier/proxy/parseBody.d.ts +7 -0
  43. package/src/dsl/verifier/proxy/parseBody.js +22 -0
  44. package/src/dsl/verifier/proxy/parseBody.js.map +1 -0
  45. package/src/dsl/{mockService.spec.d.ts → verifier/proxy/parseBody.spec.d.ts} +0 -0
  46. package/src/dsl/verifier/proxy/parseBody.spec.js +118 -0
  47. package/src/dsl/verifier/proxy/parseBody.spec.js.map +1 -0
  48. package/src/dsl/verifier/proxy/proxy.js +19 -4
  49. package/src/dsl/verifier/proxy/proxy.js.map +1 -1
  50. package/src/dsl/verifier/proxy/stateHandler/index.js +5 -1
  51. package/src/dsl/verifier/proxy/stateHandler/index.js.map +1 -1
  52. package/src/dsl/verifier/proxy/stateHandler/stateHandler.spec.js +5 -1
  53. package/src/dsl/verifier/proxy/stateHandler/stateHandler.spec.js.map +1 -1
  54. package/src/dsl/verifier/proxy/types.d.ts +1 -0
  55. package/src/dsl/verifier/verifier.js +8 -1
  56. package/src/dsl/verifier/verifier.js.map +1 -1
  57. package/src/dsl/verifier/verifier.spec.js +6 -2
  58. package/src/dsl/verifier/verifier.spec.js.map +1 -1
  59. package/src/httpPact/ffi.d.ts +15 -0
  60. package/src/httpPact/ffi.js +91 -0
  61. package/src/httpPact/ffi.js.map +1 -0
  62. package/src/{dsl/publisher.spec.d.ts → httpPact/ffi.spec.d.ts} +0 -0
  63. package/src/httpPact/ffi.spec.js +81 -0
  64. package/src/httpPact/ffi.spec.js.map +1 -0
  65. package/src/httpPact/index.d.ts +10 -10
  66. package/src/httpPact/index.js +146 -81
  67. package/src/httpPact/index.js.map +1 -1
  68. package/src/httpPact/index.spec.js +117 -220
  69. package/src/httpPact/index.spec.js.map +1 -1
  70. package/src/httpPact/tracing.js +4 -4
  71. package/src/httpPact/tracing.js.map +1 -1
  72. package/src/index.d.ts +1 -7
  73. package/src/index.js +8 -9
  74. package/src/index.js.map +1 -1
  75. package/src/messageConsumerPact.d.ts +34 -17
  76. package/src/messageConsumerPact.js +90 -67
  77. package/src/messageConsumerPact.js.map +1 -1
  78. package/src/messageConsumerPact.spec.js +0 -46
  79. package/src/messageConsumerPact.spec.js.map +1 -1
  80. package/src/messageProviderPact.js +6 -2
  81. package/src/messageProviderPact.js.map +1 -1
  82. package/src/messageProviderPact.spec.js +22 -3
  83. package/src/messageProviderPact.spec.js.map +1 -1
  84. package/src/v3/display.d.ts +6 -0
  85. package/src/v3/display.js +83 -0
  86. package/src/v3/display.js.map +1 -0
  87. package/src/v3/index.d.ts +1 -6
  88. package/src/v3/index.js +6 -7
  89. package/src/v3/index.js.map +1 -1
  90. package/src/v3/matchers.d.ts +6 -5
  91. package/src/v3/matchers.js +7 -1
  92. package/src/v3/matchers.js.map +1 -1
  93. package/src/v3/matchers.spec.js +5 -1
  94. package/src/v3/matchers.spec.js.map +1 -1
  95. package/src/v3/pact.d.ts +5 -71
  96. package/src/v3/pact.js +84 -131
  97. package/src/v3/pact.js.map +1 -1
  98. package/src/v3/types.d.ts +81 -0
  99. package/src/v3/types.js +11 -0
  100. package/src/v3/types.js.map +1 -0
  101. package/src/v3/xml/xmlElement.spec.js +36 -49
  102. package/src/v3/xml/xmlElement.spec.js.map +1 -1
  103. package/test/helper.js +5 -1
  104. package/test/helper.js.map +1 -1
  105. package/src/dsl/mockService.spec.js +0 -124
  106. package/src/dsl/mockService.spec.js.map +0 -1
  107. package/src/dsl/publisher.d.ts +0 -10
  108. package/src/dsl/publisher.js +0 -22
  109. package/src/dsl/publisher.js.map +0 -1
  110. package/src/dsl/publisher.spec.js +0 -43
  111. package/src/dsl/publisher.spec.js.map +0 -1
  112. package/src/v3/verifier.d.ts +0 -12
  113. package/src/v3/verifier.js +0 -23
  114. package/src/v3/verifier.js.map +0 -1
  115. package/v3/index.d.ts +0 -1
  116. package/v3/index.js +0 -16
  117. package/v3/index.js.map +0 -1
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
+ <span align="center">
2
+
3
+ ![logo](https://user-images.githubusercontent.com/53900/121775784-0191d200-cbcd-11eb-83dd-adc001b94519.png)
4
+
1
5
  # Pact JS
2
6
 
3
7
  <!-- Please use absolute URLs for all links as the content of this page is synced to docs.pact.io -->
@@ -12,1446 +16,245 @@
12
16
  [![license](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/pact-foundation/pact-js/blob/master/LICENSE)
13
17
  [![slack](https://slack.pact.io/badge.svg)](https://slack.pact.io)
14
18
 
15
- Implementation of the consumer driven contract library [Pact](https://docs.pact.io) for Javascript.
16
-
17
- From the [Pact website](http://docs.pact.io/):
18
-
19
- > The Pact family of frameworks provide support for [Consumer Driven Contracts](http://martinfowler.com/articles/consumerDrivenContracts.html) testing.
20
-
21
- > A Contract is a collection of agreements between a client (Consumer) and an API (Provider) that describes the interactions that can take place between them.
19
+ #### Fast, easy and reliable testing for your APIs and microservices.
22
20
 
23
- > Consumer Driven Contracts is a pattern that drives the development of the Provider from its Consumers point of view.
24
-
25
- > Pact is a testing tool that guarantees those Contracts are satisfied.
26
-
27
- Read [Getting started with Pact] for more information for beginners.
21
+ </span>
28
22
 
23
+ <br />
29
24
  <p align="center">
30
- <a href="https://asciinema.org/a/105793">
31
- <img width="880" src="https://raw.githubusercontent.com/pact-foundation/pact-js/master/.github/pact.svg?sanitize=true&t=1"></img>
32
- </a>
25
+ <a href="https://docs.pact.io"><img src="https://user-images.githubusercontent.com/53900/180370118-f11c61f3-4ae0-496f-98fa-052fdfad409e.gif" alt="Pact JS Demo"/></a>
33
26
  </p>
27
+ <br />
34
28
 
35
- <!-- TOC -->
36
-
37
- - [Pact JS](#pact-js)
38
- - [Installation](#installation)
39
- - [Do Not Track](#do-not-track)
40
- - [Which Library/Package should I use?](#which-librarypackage-should-i-use)
41
- - [Using Pact JS](#using-pact-js)
42
- - [HTTP API Testing](#http-api-testing)
43
- - [Consumer Side Testing](#consumer-side-testing)
44
- - [API](#api)
45
- - [Example](#example)
46
- - [Provider API Testing](#provider-api-testing)
47
- - [Verification Options](#verification-options)
48
- - [API with Provider States](#api-with-provider-states)
49
- - [Before and After Hooks](#before-and-after-hooks)
50
- - [Pending Pacts](#pending-pacts)
51
- - [WIP Pacts](#wip-pacts)
52
- - [Verifying multiple contracts with the same tag (e.g. for Mobile use cases)](#verifying-multiple-contracts-with-the-same-tag-eg-for-mobile-use-cases)
53
- - [Modify Requests Prior to Verification (Request Filters)](#modify-requests-prior-to-verification-request-filters)
54
- - [Lifecycle of a provider verification](#lifecycle-of-a-provider-verification)
55
- - [Publishing Pacts to a Broker](#publishing-pacts-to-a-broker)
56
- - [Publish in npm scripts](#publish-in-npm-scripts)
57
- - [Publish in a custom script](#publish-in-a-custom-script)
58
- - [Pact publishing options](#pact-publishing-options)
59
- - [Publishing Verification Results to a Pact Broker](#publishing-verification-results-to-a-pact-broker)
60
- - [Asynchronous API Testing](#asynchronous-api-testing)
61
- - [Consumer](#consumer)
62
- - [Provider (Producer)](#provider-producer)
63
- - [Pact Broker Integration](#pact-broker-integration)
64
- - [Matching](#matching)
65
- - [Match common formats](#match-common-formats)
66
- - [Match based on type](#match-based-on-type)
67
- - [Match based on arrays](#match-based-on-arrays)
68
- - [Match by regular expression](#match-by-regular-expression)
69
- - [A note about typescript](#a-note-about-typescript)
70
- - [GraphQL API](#graphql-api)
71
- - [Tutorial (60 minutes)](#tutorial-60-minutes)
72
- - [Examples](#examples)
73
- - [HTTP APIs](#http-apis)
74
- - [Asynchronous APIs](#asynchronous-apis)
75
- - [Pact JS V3](#pact-js-v3)
76
- - [Using the V3 matching rules](#using-the-v3-matching-rules)
77
- - [Array contains matcher](#array-contains-matcher)
78
- - [Provider State Injected Values](#provider-state-injected-values)
79
- - [Using Pact with XML](#using-pact-with-xml)
80
- - [Verifying providers with VerifierV3](#verifying-providers-with-verifierv3)
81
- - [Verification Options with VerifierV3](#verification-options-with-verifierv3)
82
- - [Request Filters](#request-filters)
83
- - [Provider state callbacks](#provider-state-callbacks)
84
- - [Debugging issues with Pact-JS V3](#debugging-issues-with-pact-js-v3)
85
- - [Debugging](#debugging)
86
- - [Troubleshooting / FAQs](#troubleshooting--faqs)
87
- - [Corporate Proxies / Firewalls](#corporate-proxies--firewalls)
88
- - [Alpine + Docker](#alpine--docker)
89
- - [Parallel tests](#parallel-tests)
90
- - [Splitting tests across multiple files](#splitting-tests-across-multiple-files)
91
- - [Test fails when it should pass](#test-fails-when-it-should-pass)
92
- - [Test intermittent failures](#test-intermittent-failures)
93
- - [Re-run specific verification failures](#re-run-specific-verification-failures)
94
- - [Timeout](#timeout)
95
- - [Usage with Jest](#usage-with-jest)
96
- - [Usage with Angular](#usage-with-angular)
97
- - [Contributing](#contributing)
98
- - [Contact](#contact)
99
-
100
- <!-- /TOC -->
101
-
102
- ## Installation
103
-
104
- ```
105
- npm i -S @pact-foundation/pact@latest
106
- ```
107
- <<<<<<< HEAD
108
- =======
109
-
110
- >>>>>>> master
111
- Make sure the `ignore-scripts` option is disabled, pact uses npm scripts to download further dependencies.
112
-
113
- ### Do Not Track
114
-
115
- In order to get better statistics as to who is using Pact, we have an anonymous tracking event that triggers when Pact installs for the first time. The only things we [track](https://github.com/pact-foundation/pact-js-core/blob/master/standalone/install.ts#L132-L143) are your type of OS, and the version information for the package being installed. No PII data is sent as part of this request. To respect your privacy, you can disable tracking by simply adding a 'do not track' flag within your package.json file or setting the environment variable `PACT_DO_NOT_TRACK=1`:
116
-
117
- ```json
118
- {
119
- "name": "some-project",
120
- ...
121
- "config": {
122
- "pact_do_not_track": true
123
- },
124
- ...
125
- }
126
- ```
127
-
128
- See the [Changelog] for versions and their history.
129
-
130
- ## Which Library/Package should I use?
29
+ <table>
30
+ <tr>
31
+ <td>
131
32
 
132
- TL;DR - you almost always want Pact JS.
33
+ **Pact** is the de-facto API contract testing tool. Replace expensive and brittle end-to-end integration tests with fast, reliable and easy to debug unit tests.
133
34
 
134
- | Purpose | Library | Comments |
135
- | ------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
136
- | Synchronous / HTTP APIs | Pact JS | |
137
- | Asynchronous APIs | Pact JS | |
138
- | Node.js | Pact JS | |
139
- | Browser testing | Pact Web | You probably still want Pact JS. See [Using Pact in non-Node environments](#using-pact-in-non-node-environments) \* |
140
- | Isomorphic testing | Pact Web | You probably still want Pact JS. See [Using Pact in non-Node environments](#using-pact-in-non-node-environments) \* |
141
- | Publishing to Pact Broker | Pact JS | |
35
+ - Lightning fast
36
+ - 🎈 Effortless full-stack integration testing - from the front-end to the back-end
37
+ - 🔌 Supports HTTP/REST and event-driven systems
38
+ - 🛠️ Configurable mock server
39
+ - 😌 Powerful matching rules prevents brittle tests
40
+ - 🤝 Integrates with Pact Broker / Pactflow for powerful CI/CD workflows
41
+ - 🔡 Supports 12+ languages
142
42
 
143
- \* The "I need to run it in the browser" question comes up occasionally. The question is this - for your JS code to be able to make a call to another API, is this dependent on browser-specific code? In most cases, people use tools like React/Angular which have libraries that work on the server and client side, in which case, these tests don't need to run in a browser and could instead be executed in a Node.js environment.
43
+ **Why use Pact?**
144
44
 
145
- ## Using Pact JS
45
+ Contract testing with Pact lets you:
146
46
 
147
- Pact supports [synchronous request-response style HTTP interactions](#http-api-testing) and [asynchronous interactions](#asynchronous-api-testing) with JSON-formatted payloads.
47
+ - Test locally
48
+ - 🚀 Deploy faster
49
+ - ⬇️ Reduce the lead time for change
50
+ - 💰 Reduce the cost of API integration testing
51
+ - 💥 Prevent breaking changes
52
+ - 🔎 Understand your system usage
53
+ - 📃 Document your APIs for free
54
+ - 🗄 Remove the need for complex data fixtures
55
+ - 🤷‍♂️ Reduce the reliance on complex test environments
148
56
 
149
- ## HTTP API Testing
57
+ Watch our [series](https://www.youtube.com/playlist?list=PLwy9Bnco-IpfZ72VQ7hce8GicVZs7nm0i) on the problems with end-to-end integrated tests, and how contract testing can help.
150
58
 
151
- ### Consumer Side Testing
59
+ </td>
60
+ </tr>
61
+ </table>
152
62
 
153
- To use the library on your tests, add the pact dependency:
63
+ ![----------](https://raw.githubusercontent.com/pactumjs/pactum/master/assets/rainbow.png)
154
64
 
155
- ```javascript
156
- const { Pact } = require("@pact-foundation/pact")
157
- ```
158
-
159
- The `Pact` class provides the following high-level APIs, they are listed in the order in which they typically get called in the lifecycle of testing a consumer:
160
-
161
- #### API
162
-
163
- <details><summary>Consumer API</summary>
164
-
165
- | API | Options | Returns | Description |
166
- | ------------------- | ----------------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
167
- | `new Pact(options)` | See constructor options below | `Object` | Creates a Mock Server test double of your Provider API. If you need multiple Providers for a scenario, you can create as many as these as you need. |
168
- | `setup()` | n/a | `Promise` | Start the Mock Server and wait for it to be available. You would normally call this only once in a `beforeAll(...)` type clause |
169
- | `addInteraction()` | `Object` | `Promise` | Register an expectation on the Mock Server, which must be called by your test case(s). You can add multiple interactions per server, and each test would normally contain one or more of these. These will be validated and written to a pact if successful. |
170
- | `verify()` | n/a | `Promise` | Verifies that all interactions specified. This should be called once per test, to ensure your expectations were correct |
171
- | `finalize()` | n/a | `Promise` | Records the interactions registered to the Mock Server into the pact file and shuts it down. You would normally call this only once in an `afterAll(...)` type clause. |
172
-
173
- </details>
174
-
175
- <details><summary>Constructor</summary>
176
-
177
- | Parameter | Required? | Type | Description |
178
- | ------------------- | --------- | ------- | -------------------------------------------------------------------------------------------------------- |
179
- | `consumer` | yes | string | The name of the consumer |
180
- | `provider` | yes | string | The name of the provider |
181
- | `port` | no | number | The port to run the mock service on, defaults to 1234 |
182
- | `host` | no | string | The host to run the mock service, defaults to 127.0.0.1 |
183
- | `ssl` | no | boolean | SSL flag to identify the protocol to be used (default false, HTTP) |
184
- | `sslcert` | no | string | Path to SSL certificate to serve on the mock service |
185
- | `sslkey` | no | string | Path to SSL key to serve on the mock service |
186
- | `dir` | no | string | Directory to output pact files |
187
- | `log` | no | string | File to log to |
188
- | `logLevel` | no | string | Log level: one of 'trace', 'debug', 'info', 'error', 'fatal' or 'warn' |
189
- | `spec` | no | number | Pact specification version (defaults to 2) |
190
- | `cors` | no | boolean | Allow CORS OPTION requests to be accepted, defaults to false |
191
- | `pactfileWriteMode` | no | string | Control how the Pact files are written. Choices: 'overwrite' 'update' or 'none'. Defaults to 'overwrite' |
192
- | `timeout` | no | number | The time to wait for the mock server to start up in milliseconds. Defaults to 30 seconds (30000) |
193
-
194
- </details>
65
+ ## Documentation
195
66
 
196
- #### Example
197
-
198
- The first step is to create a test for your API Consumer. The example below uses [Mocha](https://mochajs.org), and demonstrates the basic approach:
199
-
200
- 1. Create the Pact object
201
- 1. Start the Mock Provider that will stand in for your actual Provider
202
- 1. Add the interactions you expect your consumer code to make when executing the tests
203
- 1. Write your tests - the important thing here is that you test the outbound _collaborating_ function which calls the Provider, and not just issue raw http requests to the Provider. This ensures you are testing your actual running code, just like you would in any other unit test, and that the tests will always remain up to date with what your consumer is doing.
204
- 1. Validate the expected interactions were made between your consumer and the Mock Service
205
- 1. Generate the pact(s)
206
-
207
- Check out the `examples` folder for examples with Karma Jasmine, Mocha and Jest. The example below is taken from the [integration spec](https://github.com/pact-foundation/pact-js/blob/master/src/pact.integration.spec.ts).
208
-
209
- ```javascript
210
- const path = require("path")
211
- const chai = require("chai")
212
- const { Pact } = require("@pact-foundation/pact")
213
- const chaiAsPromised = require("chai-as-promised")
214
- const expect = chai.expect
215
-
216
- chai.use(chaiAsPromised)
217
-
218
- describe("Pact", () => {
219
- // (1) Create the Pact object to represent your provider
220
- const provider = new Pact({
221
- consumer: "TodoApp",
222
- provider: "TodoService",
223
- port: 1234,
224
- log: path.resolve(process.cwd(), "logs", "pact.log"),
225
- dir: path.resolve(process.cwd(), "pacts"),
226
- logLevel: "INFO",
227
- })
228
-
229
- // this is the response you expect from your Provider
230
- const EXPECTED_BODY = [
231
- {
232
- id: 1,
233
- name: "Project 1",
234
- due: "2016-02-11T09:46:56.023Z",
235
- tasks: [
236
- { id: 1, name: "Do the laundry", done: true },
237
- { id: 2, name: "Do the dishes", done: false },
238
- { id: 3, name: "Do the backyard", done: false },
239
- { id: 4, name: "Do nothing", done: false },
240
- ],
241
- },
242
- ]
243
-
244
- const todoApp = new TodoApp()
245
-
246
- context("when there are a list of projects", () => {
247
- describe("and there is a valid user session", () => {
248
- before(() =>
249
- provider
250
- // (2) Start the mock server
251
- .setup()
252
- // (3) add interactions to the Mock Server, as many as required
253
- .then(() =>
254
- provider.addInteraction({
255
- // The 'state' field specifies a "Provider State"
256
- state: "i have a list of projects",
257
- uponReceiving: "a request for projects",
258
- withRequest: {
259
- method: "GET",
260
- path: "/projects",
261
- headers: { Accept: "application/json" },
262
- },
263
- willRespondWith: {
264
- status: 200,
265
- headers: { "Content-Type": "application/json" },
266
- body: EXPECTED_BODY,
267
- },
268
- })
269
- )
270
- )
271
- })
272
-
273
- // (4) write your test(s)
274
- it("generates a list of TODOs for the main screen", async () => {
275
- const projects = await todoApp.getProjects() // <- this method would make the remote http call
276
- expect(projects).to.be.a("array")
277
- expect(projects).to.have.deep.property("projects[0].id", 1)
278
- })
279
-
280
- // (5) validate the interactions you've registered and expected occurred
281
- // this will throw an error if it fails telling you what went wrong
282
- // This should be performed once per interaction test
283
- afterEach(() => provider.verify())
284
- })
285
-
286
- // (6) write the pact file for this consumer-provider pair,
287
- // and shutdown the associated mock server.
288
- // You should do this only _once_ per Provider you are testing,
289
- // and after _all_ tests have run for that suite
290
- after(() => provider.finalize())
291
- })
292
- ```
67
+ This readme offers an basic introduction to the library. The full documentation for Pact JS and the rest of the framework is available at https://docs.pact.io/.
293
68
 
294
- ### Provider API Testing
69
+ - [Installation](#installation)
70
+ - [Consumer Testing](/docs/consumer.md)
71
+ - [Matching](/docs/matching.md)
72
+ - [Provider Testing](/docs/provider.md)
73
+ - [Event Driven Systems](/docs/messages.md)
74
+ - [GraphQL](/docs/graphql.md)
75
+ - [XML](/docs/xml.md)
76
+ - [Examples](https://github.com/pact-foundation/pact-js/tree/master/examples/)
77
+ - [Migration guide](/MIGRATION.md)
78
+ - [Troubleshooting](/docs/troubleshooting.md)
295
79
 
296
- <details><summary>Provider API</summary>
80
+ ### Tutorial (60 minutes)
297
81
 
298
- Once you have created Pacts for your Consumer, you need to validate those Pacts against your Provider. The Verifier object provides the following API for you to do so:
82
+ Learn the key Pact JS features in 60 minutes: https://github.com/pact-foundation/pact-workshop-js
299
83
 
300
- | API | Options | Returns | Description |
301
- | ------------------ | :-------: | --------- | --------------------- |
302
- | `verifyProvider()` | See below | `Promise` | Start the Mock Server |
84
+ ## Need Help
303
85
 
304
- </details>
305
-
306
- 1. Start your local Provider service.
307
- 1. Optionally, instrument your API with ability to configure [provider states](https://github.com/pact-foundation/pact-provider-verifier/)
308
- 1. Then run the Provider side verification step
309
-
310
- ```js
311
- const { Verifier } = require('@pact-foundation/pact');
312
- const opts = {
313
- ...
314
- };
315
-
316
- new Verifier(opts).verifyProvider().then(function () {
317
- // do something
318
- });
319
- ```
320
-
321
- #### Verification Options
322
-
323
- <details><summary>Verification Options</summary>
324
-
325
- | Parameter | Required? | Type | Description |
326
- | --------------------------- | --------- | ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --- | --------------------------------------------------- |
327
- | `providerBaseUrl` | true | string | Running API provider host endpoint. |
328
- | `pactBrokerUrl` | false | string | Base URL of the Pact Broker from which to retrieve the pacts. Required if `pactUrls` not given. |
329
- | `provider` | false | string | Name of the provider if fetching from a Broker |
330
- | `consumerVersionSelectors` | false | ConsumerVersionSelector\|array | Using [Selectors](https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors/) is a way we specify which pacticipants and versions we want to use when configuring verifications. |
331
- | `consumerVersionTags` | false | string\|array | Retrieve the latest pacts with given tag(s) |
332
- | `providerVersionTags` | false | string\|array | Tag(s) to apply to the provider application |
333
- | `includeWipPactsSince` | false | string | Includes pact marked as WIP since this date. String in the format %Y-%m-%d or %Y-%m-%dT%H:%M:%S.000%:z |
334
- | `pactUrls` | false | array | Array of local pact file paths or HTTP-based URLs. Required if _not_ using a Pact Broker. |
335
- | `providerStatesSetupUrl` | false | string | Deprecated (use URL to send PUT requests to setup a given provider state |
336
- | `stateHandlers` | false | object | Map of "state" to a function that sets up a given provider state. See docs below for more information |
337
- | `requestFilter` | false | function ([Express middleware](https://expressjs.com/en/guide/using-middleware.html)) | Function that may be used to alter the incoming request or outgoing response from the verification process. See below for use. |
338
- | `beforeEach` | false | function | Function to execute prior to each interaction being validated |
339
- | `afterEach` | false | function | Function to execute after each interaction has been validated |
340
- | `pactBrokerUsername` | false | string | Username for Pact Broker basic authentication |
341
- | `pactBrokerPassword` | false | string | Password for Pact Broker basic authentication |
342
- | `pactBrokerToken` | false | string | Bearer token for Pact Broker authentication |
343
- | `publishVerificationResult` | false | boolean | Publish verification result to Broker (_NOTE_: you should only enable this during CI builds) |
344
- | `providerVersion` | false | string | Provider version, required to publish verification result to Broker. Optional otherwise. |
345
- | `enablePending` | false | boolean | Enable the [pending pacts](https://docs.pact.io/pending) feature. |
346
- | `timeout` | false | number | The duration in ms we should wait to confirm verification process was successful. Defaults to 30000. |
347
- | `logLevel` | false | string | not used, log level is set by [environment variable](#debugging-issues-with-pact-js-v3) |
348
-
349
- </details>
350
-
351
- To dynamically retrieve pacts from a Pact Broker for a provider, provide the broker URL, the name of the provider, and the consumer version tags that you want to verify:
352
-
353
- ```js
354
- const opts = {
355
- pactBroker: "http://my-broker",
356
- provider: "Animal Profile Service",
357
- consumerVersionTags: ["master", "test", "prod"],
358
- }
359
- ```
360
-
361
- To verify a pact at a specific URL (eg. when running a pact verification triggered by a 'contract content changed' webhook, or when verifying a pact from your local machine, or a network location that's not the Pact Broker, set just the `pactUrls`, eg:
362
-
363
- ```js
364
- const opts = {
365
- pactUrls: [process.env.PACT_URL],
366
- }
367
- ```
368
-
369
- To publish the verification results back to the Pact Broker, you need to enable the 'publish' flag, set the provider version and optional provider version tags:
370
-
371
- ```js
372
- const opts = {
373
- publishVerificationResult: true, //generally you'd do something like `process.env.CI === 'true'`
374
- providerVersion: "version", //recommended to be the git sha
375
- providerVersionTags: ["tag"], //optional, recommended to be the git branch
376
- }
377
- ```
378
-
379
- If your broker has a self signed certificate, set the environment variable `SSL_CERT_FILE` (or `SSL_CERT_DIR`) pointing to a copy of your certificate.
380
-
381
- Read more about [Verifying Pacts](https://docs.pact.io/getting_started/verifying_pacts).
382
-
383
- #### API with Provider States
384
-
385
- If you have defined any `state`s in your consumer tests, the `Verifier` can put the provider into the right state prior to sending the request. For example, the provider can use the state to mock away certain database queries. To support this, set up a handler for each `state` using hooks on the `stateHandlers` property. Here is an example from our [e2e suite](https://github.com/pact-foundation/pact-js/blob/master/examples/e2e/test/provider.spec.js):
386
-
387
- ```js
388
- const opts = {
389
- ...
390
- stateHandlers: {
391
- [null]: () => {
392
- // This is the "default" state handler, when no state is given
393
- }
394
- "Has no animals": () => {
395
- animalRepository.clear()
396
- return Promise.resolve(`Animals removed from the db`)
397
- },
398
- "Has some animals": () => {
399
- importData()
400
- return Promise.resolve(`Animals added to the db`)
401
- },
402
- "Has an animal with ID 1": () => {
403
- importData()
404
- return Promise.resolve(`Animals added to the db`)
405
- }
406
- }
407
- }
408
-
409
- return new Verifier(opts).verifyProvider().then(...)
410
- ```
411
-
412
- As you can see, for each state ("Has no animals", ...), we configure the local datastore differently. If this option is not configured, the `Verifier` will ignore the provider states defined in the pact and log a warning.
413
-
414
- Read more about [Provider States](https://docs.pact.io/getting_started/provider_states).
415
-
416
- #### Before and After Hooks
86
+ - [Join](http://slack.pact.io) our community [slack workspace](http://pact-foundation.slack.com/).
87
+ - Stack Overflow: https://stackoverflow.com/questions/tagged/pact
88
+ - Say 👋 on Twitter: [@pact_up]
417
89
 
418
- Sometimes, it's useful to be able to do things before or after a test has run, such as reset a database, log a metric etc. A `beforeEach` hook runs on each verification before any other part of the Pact test lifecycle, and a `afterEach` hook runs as the last step before returning the verification result back to the test.
90
+ ## Installation
419
91
 
420
- You can add them to your verification options as follows:
92
+ ```shell
93
+ npm i -S @pact-foundation/pact@latest
421
94
 
422
- ```js
423
- const opts = {
424
- ...
425
- beforeEach: () => {
426
- console.log('I run before everything else')
427
- },
428
-
429
- afterEach: () => {
430
- console.log('I run after everything else has finished')
431
- }
432
- }
95
+ # 🚀 now write some tests!
433
96
  ```
434
97
 
435
- If the hook errors, the test will fail. See the lifecycle of an interaction below.
436
-
437
- #### Pending Pacts
438
-
439
- _NOTE_: This feature is available on [Pactflow] by default, and requires [configuration](https://docs.pact.io/pact_broker/advanced_topics/wip_pacts) if using a self-hosted broker.
440
-
441
- Pending pacts is a feature that allows consumers to publish new contracts or changes to existing contracts without breaking Provider's builds. It does so by flagging the contract as "unverified" in the Pact Broker the first time a contract is published. A Provider can then enable a behaviour (via `enablePending: true`) that will still perform a verification (and thus share the results back to the broker) but _not_ fail the verification step itself.
442
-
443
- This enables safe introduction of new contracts into the system, without breaking Provider builds, whilst still providing feedback to Consumers as per before.
444
-
445
- See the [docs](https://docs.pact.io/pending) and this [article](http://blog.pact.io/2020/02/24/how-we-have-fixed-the-biggest-problem-with-the-pact-workflow/) for more background.
446
-
447
- #### WIP Pacts
98
+ *NOTE: Make sure the `ignore-scripts` option is disabled, pact uses npm scripts to compile native dependencies and won't function without it.*
448
99
 
449
- _NOTE_: This feature is available on [Pactflow] by default, and requires [configuration](https://docs.pact.io/pact_broker/advanced_topics/wip_pacts) if using a self-hosted broker.
450
-
451
- WIP Pacts builds upon pending pacts, enabling provider tests to pull in _any_ contracts applicable to the provider regardless of the `tag` it was given. This is useful, because often times consumers won't follow the exact same tagging convention and so their workflow would be interrupted. This feature enables any pacts determined to be "work in progress" to be verified by the Provider, without causing a build failure. You can enable this behaviour by specifying a valid timestamp for `includeWipPactsSince`. This sets the start window for which new WIP pacts will be pulled down for verification, regardless of the tag.
452
-
453
- See the [docs](https://docs.pact.io/wip) and this [article](http://blog.pact.io/2020/02/24/introducing-wip-pacts/) for more background.
454
-
455
- #### Verifying multiple contracts with the same tag (e.g. for Mobile use cases)
100
+ ### Do Not Track
456
101
 
457
- Tags may be used to indicate a particular version of an application has been deployed to an environment - e.g. `prod`, and are critical in configuring can-i-deploy checks for CI/CD pipelines. In the majority of cases, only one version of an application is deployed to an environment at a time. For example, an API and a Website are usually deployed in replacement of an existing system, and any transition period is quite short lived.
102
+ In order to get better statistics as to who is using Pact, we have an anonymous tracking event that triggers when Pact installs for the first time. The only things we [track](https://docs.pact.io/metrics) are your type of OS, and the version information for the package being installed. No PII data is sent as part of this request. You can disable tracking by setting the environment variable `PACT_DO_NOT_TRACK=1`:
458
103
 
459
- Mobile is an exception to this rule - it is common to have multiple versions of an application that are in "production" simultaneously. To support this workflow, we have a feature known as [consumer version selectors](https://docs.pact.io/pact_broker/advanced_topics/consumer_version_selectors/). Using selectors, we can verify that _all_ pacts with a given tag should be verified. The following selectors ask the broker to "find all pacts with tag 'prod' and the latest pact for 'master'":
104
+ ![----------](https://raw.githubusercontent.com/pactumjs/pactum/master/assets/rainbow.png)
460
105
 
461
- ```js
462
- consumerVersionSelectors: [
463
- {
464
- tag: "prod",
465
- all: true,
466
- },
467
- {
468
- tag: "master",
469
- latest: true,
470
- },
471
- ]
472
- ```
106
+ ## Usage
473
107
 
474
- _NOTE: Using the `all` flag requires you to ensure you delete any tags associated with application versions that are no longer in production (e.g. if decommissioned from the app store)_
108
+ ### Consumer package
475
109
 
476
- #### Modify Requests Prior to Verification (Request Filters)
110
+ The main consumer interface are the `PactV3` class and `MatchersV3` exports of the `@pact-foundation/pact` package.
477
111
 
478
- Sometimes you may need to add things to the requests that can't be persisted in a pact file. Examples of these are authentication tokens with a small life span. e.g. an OAuth bearer token: `Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42`.
112
+ #### Writing a Consumer test
479
113
 
480
- For these cases, we the ability to modify a request/response and modify the payload. The flag to achieve this is `requestFilter`.
114
+ Pact is a consumer-driven contract testing tool, which is a fancy way of saying that the API `Consumer` writes a test to set out its assumptions and needs of its API `Provider`(s). By unit testing our API client with Pact, it will produce a `contract` that we can share to our `Provider` to confirm these assumptions and prevent breaking changes.
481
115
 
482
- **Example API with Authorization**
116
+ In this example, we are going to be testing our User API client, responsible for communicating with the `UserAPI` over HTTP. It currently has a single method `GetUser(id)` that will return a `*User`.
483
117
 
484
- For example, to have an `Authorization` bearer token header sent as part of the verification request, set the `verifyProvider` options as per below:
118
+ Pact tests have a few key properties. We'll demonstrate a common example using the 3A `Arrange/Act/Assert` pattern.
485
119
 
486
120
  ```js
487
- let token
488
- const opts = {
489
- provider: 'Animal Profile Service',
490
- ...
491
- stateHandlers: {
492
- "is authenticated": () => {
493
- token = "1234"
494
- Promise.resolve(`Valid bearer token generated`)
495
- },
496
- "is not authenticated": () => {
497
- token = ""
498
- Promise.resolve(`Expired bearer token generated`)
499
- }
500
- },
501
-
502
- // this middleware is executed for each request, allowing `token` to change between invocations
503
- // it is common to pair this with `stateHandlers` as per above, that can set/expire the token
504
- // for different test cases
505
- requestFilter: (req, res, next) => {
506
- req.headers["Authorization"] = `Bearer: ${token}`
507
- next()
508
- },
509
- }
510
-
511
- return new Verifier(opts).verifyProvider().then(...)
512
- ```
513
-
514
- As you can see, this is your opportunity to modify\add to headers being sent to the Provider API, for example to create a valid time-bound token.
515
-
516
- _Important Note_: You should only use this feature for things that can not be persisted in the pact file. By modifying the request, you are potentially modifying the contract from the consumer tests!
517
-
518
- #### Lifecycle of a provider verification
519
-
520
- For each _interaction_ in a pact file, the order of execution is as follows:
521
-
522
- `BeforeEach` -> `State Handler` -> `Request Filter (request phase)` -> `Execute Provider Test` -> `Request Filter (response phase)` -> `AfterEach`
523
-
524
- If any of the middleware or hooks fail, the tests will also fail.
525
-
526
- ### Publishing Pacts to a Broker
527
-
528
- Sharing is caring - to simplify sharing Pacts between Consumers and Providers, we have created the [Pact Broker](https://pactflow.io).
529
-
530
- The Broker:
531
-
532
- - versions your contracts
533
- - tells you which versions of your applications can be deployed safely together
534
- - allows you to deploy your services independently
535
- - provides API documentation of your applications that is guaranteed to be up-to date
536
- - visualises the relationships between your services
537
- - integrates with other systems, such as Slack or your CI server, via webhooks
538
- - ...and much much [more](https://docs.pact.io/getting_started/sharing_pacts).
539
-
540
- [Host your own](https://github.com/pact-foundation/pact_broker), or signup for a free hosted [Pact Broker](https://pactflow.io).
541
-
542
- #### Publish in npm scripts
543
-
544
- The easiest way to publish pacts to the broker is via an npm script in your package.json:
121
+ import { PactV3, MatchersV3 } from '@pact-foundation/pact';
545
122
 
546
- ```
547
-
548
- "test:publish": "./node_modules/.bin/pact-broker publish <YOUR_PACT_FILES_OR_DIR> --consumer-app-version=\"$(npx @pact-foundation/absolute-version)\" --auto-detect-version-properties --broker-base-url=https://your-broker-url.example.com"
549
- ```
550
-
551
- For a full list of the options, see the [CLI usage instructions](https://github.com/pact-foundation/pact-ruby-standalone/releases).
552
- All CLI binaries are available in npm scripts when using pact-js.
553
-
554
- If you want to pass your username and password to the broker without including
555
- them in scripts, you can provide it via the environment variables
556
- `PACT_BROKER_USERNAME` and `PACT_BROKER_PASSWORD`. If your broker supports an
557
- access token instead of a password, use the environment variable
558
- `PACT_BROKER_TOKEN`.
559
-
560
- #### Publish in a custom script
561
-
562
- If you require finer control over your pact publication, you can programatically publish in a custom script:
123
+ // Create a 'pact' between the two applications in the integration we are testing
124
+ const provider = new PactV3({
125
+ dir: path.resolve(process.cwd(), 'pacts'),
126
+ consumer: 'MyConsumer',
127
+ provider: 'MyProvider',
128
+ });
563
129
 
564
- ```js
565
- const { Publisher } = require("@pact-foundation/pact")
566
- const opts = {
567
- ...
130
+ // API Client that will fetch dogs from the Dog API
131
+ // This is the target of our Pact test
132
+ public getMeDogs = (from: string): AxiosPromise => {
133
+ return axios.request({
134
+ baseURL: this.url,
135
+ params: { from },
136
+ headers: { Accept: 'application/json' },
137
+ method: 'GET',
138
+ url: '/dogs',
139
+ });
568
140
  };
569
141
 
570
- new Publisher(opts)
571
- .publishPacts()
572
- .then(() => {
573
- // ...
574
- })
575
- ```
576
-
577
- #### Pact publishing options
578
-
579
- <details><summary>Publishing Options</summary>
580
-
581
- | Parameter | Required | Type | Description |
582
- | -------------------- | :------: | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
583
- | `providerBaseUrl` | `false` | string | Running API provider host endpoint. |
584
- | `pactFilesOrDirs` | `true` | array of strings | Array of local Pact files or directories containing pact files. Path must be absolute. Required. |
585
- | `pactBroker` | `true` | string | The base URL of the Pact Broker. eg. https://test.pactflow.io. Required. |
586
- | `pactBrokerToken` | `false` | string | Bearer token for Pact Broker authentication. Optional. If using Pactflow, you likely need this option |
587
- | `pactBrokerUsername` | `false` | string | Username for Pact Broker basic authentication. Optional. If using Pactflow, you most likely need to use `pactBrokerToken` |
588
- | `pactBrokerPassword` | `false` | string | Password for Pact Broker basic authentication. Optional. If using Pactflow, you most likely need to use `pactBrokerToken` |
589
- | `consumerVersion` | `true` | string | The consumer application version; e.g. '1.0.0-cac389f'. ([See more info on versioning](https://docs.pact.io/getting_started/versioning_in_the_pact_broker)) |
590
- | `tags` | `false` | array of strings | Tag your pacts, often used with your branching, release or environment strategy e.g. ['prod', 'test'] |
591
-
592
- </details>
593
-
594
- If your broker has a self signed certificate, set the environment variable `SSL_CERT_FILE` (or `SSL_CERT_DIR`) pointing to a copy of your certificate.
595
-
596
- #### Publishing Verification Results to a Pact Broker
597
-
598
- If you're using a Pact Broker (e.g. a hosted one at https://pactflow.io), you can
599
- publish your verification results so that consumers can query if they are safe
600
- to release.
601
-
602
- It looks like this:
603
-
604
- ![screenshot of verification result](https://cloud.githubusercontent.com/assets/53900/25884085/2066d98e-3593-11e7-82af-3b41a20af8e5.png)
605
-
606
- To publish the verification results back to the Pact Broker, you need to enable the 'publish' flag, set the provider version and optional provider version tags:
607
-
608
- ```js
609
- const opts = {
610
- publishVerificationResult: true, //recommended to only publish from CI by setting the value to `process.env.CI === 'true'`
611
- providerVersion: "version", //recommended to be the git sha eg. process.env.MY_CI_COMMIT
612
- providerVersionTags: ["tag"], //optional, recommended to be the git branch eg. process.env.MY_CI_BRANCH
613
- }
614
- ```
615
-
616
- ## Asynchronous API Testing
617
-
618
- _Since version `v6.0.0` or later_
619
-
620
- Modern distributed architectures are increasingly integrated in a decoupled, asynchronous fashion. Message queues such as ActiveMQ, RabbitMQ, SQS, Kafka and Kinesis are common, often integrated via small and frequent numbers of microservices (e.g. lambda.).
621
-
622
- Furthermore, the web has things like WebSockets which involve bidirectional messaging.
623
-
624
- Pact supports these use cases, by abstracting away the protocol and focussing on the messages passing between them.
625
-
626
- For further reading and introduction into this topic, see this [article](https://dius.com.au/2017/09/22/contract-testing-serverless-and-asynchronous-applications/)
627
- and our [asynchronous examples](#asynchronous-apis) for a more detailed overview of these concepts.
628
-
629
- ### Consumer
630
-
631
- A Consumer is the system that will be reading a message from a queue or some other intermediary - like a DynamoDB table or S3 bucket -
632
- and be able to handle it.
633
-
634
- From a Pact testing point of view, Pact takes the place of the intermediary (MQ/broker etc.) and confirms whether or not the consumer is able to handle a request.
635
-
636
- The following test creates a contract for a Dog API handler:
637
-
638
- ```js
639
- const path = require("path")
640
- const {
641
- MessageConsumerPact,
642
- synchronousBodyHandler,
643
- } = require("@pact-foundation/pact")
644
-
645
- // 1 Dog API Handler
646
- const dogApiHandler = function (dog) {
647
- if (!dog.id && !dog.name && !dog.type) {
648
- throw new Error("missing fields")
649
- }
650
-
651
- // do some other things to dog...
652
- // e.g. dogRepository.save(dog)
653
- return
654
- }
655
-
656
- // 2 Pact Message Consumer
657
- const messagePact = new MessageConsumerPact({
658
- consumer: "MyJSMessageConsumer",
659
- dir: path.resolve(process.cwd(), "pacts"),
660
- pactfileWriteMode: "update",
661
- provider: "MyJSMessageProvider",
662
- })
663
-
664
- describe("receive dog event", () => {
665
- it("accepts a valid dog", () => {
666
- // 3 Consumer expectations
667
- return (
668
- messagePact
669
- .given("some state")
670
- .expectsToReceive("a request for a dog")
671
- .withContent({
672
- id: like(1),
673
- name: like("rover"),
674
- type: term({ generate: "bulldog", matcher: "^(bulldog|sheepdog)$" }),
675
- })
676
- .withMetadata({
677
- "content-type": "application/json",
678
- })
679
-
680
- // 4 Verify consumers' ability to handle messages
681
- .verify(synchronousBodyHandler(dogApiHandler))
682
- )
683
- })
684
- })
685
- ```
686
-
687
- **Explanation**:
688
-
689
- 1. The Dog API - a contrived API handler example. Expects a dog object and throws an `Error` if it can't handle it.
690
- - In most applications, some form of transactionality exists and communication with a MQ/broker happens.
691
- - It's important we separate out the protocol bits from the message handling bits, so that we can test that in isolation.
692
- 1. Creates the MessageConsumer class
693
- 1. Setup the expectations for the consumer - here we expect a `dog` object with three fields
694
- 1. Pact will send the message to your message handler. If the handler returns a successful promise, the message is saved, otherwise the test fails. There are a few key things to consider:
695
- - The actual request body that Pact will send, will be contained within a [Message](https://github.com/pact-foundation/pact-js/tree/master/src/dsl/message.ts) object along with other context, so the body must be retrieved via `content` attribute.
696
- - All handlers to be tested must be of the shape `(m: Message) => Promise<any>` - that is, they must accept a `Message` and return a `Promise`. This is how we get around all of the various protocols, and will often require a lightweight adapter function to convert it.
697
- - In this case, we wrap the actual dogApiHandler with a convenience function `synchronousBodyHandler` provided by Pact, which Promisifies the handler and extracts the contents.
698
-
699
- ### Provider (Producer)
700
-
701
- A Provider (Producer in messaging parlance) is the system that will be putting a message onto the queue.
702
-
703
- As per the Consumer case, Pact takes the position of the intermediary (MQ/broker) and checks to see whether or not the Provider sends a message that matches the Consumer's expectations.
704
-
705
- ```js
706
- const path = require("path")
707
- const { MessageProviderPact } = require("@pact-foundation/pact")
708
-
709
- // 1 Messaging integration client
710
- const dogApiClient = {
711
- createDog: () => {
712
- return new Promise((resolve, reject) => {
713
- resolve({
714
- id: 1,
715
- name: "fido",
716
- type: "bulldog",
142
+ const dogExample = { dog: 1 };
143
+ const EXPECTED_BODY = MatchersV3.eachLike(dogExample);
144
+
145
+ describe('GET /dogs', () => {
146
+ it('returns an HTTP 200 and a list of docs', () => {
147
+ // Arrange: Setup our expected interactions
148
+ //
149
+ // We use Pact to mock out the backend API
150
+ provider
151
+ .given('I have a list of dogs')
152
+ .uponReceiving('a request for all dogs with the builder pattern')
153
+ .withRequest({
154
+ method: 'GET',
155
+ path: '/dogs',
156
+ query: { from: 'today' },
157
+ headers: { Accept: 'application/json' },
717
158
  })
718
- })
719
- },
720
- }
721
-
722
- describe("Message provider tests", () => {
723
- // 2 Pact setup
724
- const p = new MessageProviderPact({
725
- messageProviders: {
726
- "a request for a dog": () => dogApiClient.createDog(),
727
- },
728
- provider: "MyJSMessageProvider",
729
- providerVersion: "1.0.0",
730
- pactUrls: [
731
- path.resolve(
732
- process.cwd(),
733
- "pacts",
734
- "myjsmessageconsumer-myjsmessageprovider.json"
735
- ),
736
- ],
737
- })
738
-
739
- // 3 Verify the interactions
740
- describe("Dog API Client", () => {
741
- it("sends some dogs", () => {
742
- return p.verify()
743
- })
744
- })
745
- })
746
- ```
747
-
748
- **Explanation**:
749
-
750
- 1. Our API client contains a single function `createDog` which is responsible for generating the message that will be sent to the consumer via some message queue
751
- 1. We configure Pact to stand-in for the queue. The most important bit here is the `messageProviders` block
752
- - Similar to the Consumer tests, we map the various interactions that are going to be verified as denoted by their `description` field. In this case, `a request for a dog`, maps to the `createDog` handler. Notice how this matches the original Consumer test.
753
- 1. We can now run the verification process. Pact will read all of the interactions specified by its consumer, and invoke each function that is responsible for generating that message.
754
-
755
- ### Pact Broker Integration
756
-
757
- As per HTTP APIs, you can [publish contracts and verification results to a Broker](#publishing-pacts-to-a-broker).
758
-
759
- ## Matching
760
-
761
- Matching makes your tests more expressive making your tests less brittle.
762
-
763
- Rather than use hard-coded values which must then be present on the Provider side,
764
- you can use regular expressions and type matches on objects and arrays to validate the
765
- structure of your APIs.
766
-
767
- _NOTE: Make sure to start the mock service via the `Pact` declaration with the option `specification: 2` to get access to these features._
768
-
769
- ### Match common formats
770
-
771
- Often times, you find yourself having to re-write regular expressions for common formats. We've created a number of them for you to save you the time:
772
-
773
- <details><summary>Matchers API</summary>
774
-
775
- | method | description |
776
- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------- |
777
- | `boolean` | Match a boolean value (using equality) |
778
- | `string` | Match a string value |
779
- | `integer` | Will match all numbers that are integers (both ints and longs) |
780
- | `decimal` | Will match all real numbers (floating point and decimal) |
781
- | `hexadecimal` | Will match all hexadecimal encoded strings |
782
- | `iso8601Date` | Will match string containing basic ISO8601 dates (e.g. 2016-01-01) |
783
- | `iso8601DateTime` | Will match string containing ISO 8601 formatted dates (e.g. 2015-08-06T16:53:10+01:00) |
784
- | `iso8601DateTimeWithMillis` | Will match string containing ISO 8601 formatted dates, enforcing millisecond precision (e.g. 2015-08-06T16:53:10.123+01:00) |
785
- | `rfc3339Timestamp` | Will match a string containing an RFC3339 formatted timestapm (e.g. Mon, 31 Oct 2016 15:21:41 -0400) |
786
- | `iso8601Time` | Will match string containing times (e.g. T22:44:30.652Z) |
787
- | `ipv4Address` | Will match string containing IP4 formatted address |
788
- | `ipv6Address` | Will match string containing IP6 formatted address |
789
- | `uuid` | Will match strings containing UUIDs |
790
- | `email` | Will match strings containing Email address |
791
-
792
- </details>
793
-
794
- ### Match based on type
795
-
796
- ```javascript
797
- const { like, string } = Matchers
798
-
799
- provider.addInteraction({
800
- state: "Has some animals",
801
- uponReceiving: "a request for an animal",
802
- withRequest: {
803
- method: "GET",
804
- path: "/animals/1",
805
- },
806
- willRespondWith: {
807
- status: 200,
808
- headers: {
809
- "Content-Type": "application/json; charset=utf-8",
810
- },
811
- body: {
812
- id: 1,
813
- name: string("Billy"),
814
- address: like({
815
- street: "123 Smith St",
816
- suburb: "Smithsville",
817
- postcode: 7777,
818
- }),
819
- },
820
- },
821
- })
822
- ```
823
-
824
- Note that you can wrap a `like` around a single value or an object. When wrapped around an object, all values and child object values will be matched according to types, unless overridden by something more specific like a `term`.
825
-
826
- [flexible-matching]: https://github.com/realestate-com-au/pact/wiki/Regular-expressions-and-type-matching-with-Pact
827
-
828
- ### Match based on arrays
829
-
830
- Matching provides the ability to specify flexible length arrays. For example:
831
-
832
- ```javascript
833
- pact.eachLike(obj, { min: 3 })
834
- ```
835
-
836
- Where `obj` can be any javascript object, value or Pact.Match. It takes optional argument (`{ min: 3 }`) where min is greater than 0 and defaults to 1 if not provided.
837
-
838
- Below is an example that uses all of the Pact Matchers.
839
-
840
- ```javascript
841
- const { somethingLike: like, term, eachLike } = pact
842
-
843
- const animalBodyExpectation = {
844
- id: 1,
845
- first_name: "Billy",
846
- last_name: "Goat",
847
- animal: "goat",
848
- age: 21,
849
- gender: term({
850
- matcher: "F|M",
851
- generate: "M",
852
- }),
853
- location: {
854
- description: "Melbourne Zoo",
855
- country: "Australia",
856
- post_code: 3000,
857
- },
858
- eligibility: {
859
- available: true,
860
- previously_married: false,
861
- },
862
- children: eachLike({ name: "Sally", age: 2 }),
863
- }
864
-
865
- // Define animal list payload, reusing existing object matcher
866
- // Note that using eachLike ensure that all values are matched by type
867
- const animalListExpectation = eachLike(animalBodyExpectation, {
868
- min: MIN_ANIMALS,
869
- })
870
-
871
- provider.addInteraction({
872
- state: "Has some animals",
873
- uponReceiving: "a request for all animals",
874
- withRequest: {
875
- method: "GET",
876
- path: "/animals/available",
877
- },
878
- willRespondWith: {
879
- status: 200,
880
- headers: {
881
- "Content-Type": "application/json; charset=utf-8",
882
- },
883
- body: animalListExpectation,
884
- },
885
- })
886
- ```
887
-
888
- ### Match by regular expression
889
-
890
- If none of the above matchers or formats work, you can write your own regex matcher.
891
-
892
- The underlying mock service is written in Ruby, so the regular expression must be in a Ruby format, not a Javascript format.
893
-
894
- ```javascript
895
- const { term } = pact
896
-
897
- provider.addInteraction({
898
- state: "Has some animals",
899
- uponReceiving: "a request for an animal",
900
- withRequest: {
901
- method: "GET",
902
- path: "/animals/1",
903
- },
904
- willRespondWith: {
905
- status: 200,
906
- headers: {
907
- "Content-Type": "application/json; charset=utf-8",
908
- },
909
- body: {
910
- id: 100,
911
- name: "billy",
912
- gender: term({
913
- matcher: "F|M",
914
- generate: "F",
915
- }),
916
- },
917
- },
918
- })
919
- ```
920
-
921
- ### A note about typescript
922
-
923
- Because of the way interfaces work in typescript, if you are
924
- passing a typed object to a matcher, and that type is an interface (say `Foo`):
925
-
926
-
927
- ```javascript
928
- interface Foo {
929
- a: string;
930
- }
931
-
932
- const f: Foo = { a: "broken example" };
933
-
934
-
935
- provider.addInteraction({
936
- uponReceiving: "a post with foo",
937
- withRequest: {
938
- method: "POST",
939
- path: "/",
940
- body: like(f) // Type 'Matcher<Foo>' is not assignable to type 'AnyTemplate'.
941
- },
942
- ...
943
- })
944
- ```
945
-
946
- then you may run into the following message:
947
-
948
- ```
949
- Type 'Matcher<Foo>' is not assignable to type 'AnyTemplate'.
950
- ```
951
-
952
- This is one of the rare places where `type` differs from `interface`. You have two options:
953
-
954
- 1. Use `type Foo = {` instead of `interface Foo {`
955
- 2. If this is not possible, Pact exports a workaround wrapper type called `InterfaceToTemplate<YourTypeHere>`. Use it like this:
956
- ```
957
- const f: InterfaceToTemplate<Foo> = { a: "working example" };
958
- ```
959
-
960
-
961
-
962
-
963
- ## GraphQL API
964
-
965
- GraphQL is simply an abstraction over HTTP and may be tested via Pact. There are two wrapper APIs available for GraphQL specific testing: `GraphQLInteraction` and `ApolloGraphQLInteraction`.
966
-
967
- These are both lightweight wrappers over the standard DSL in order to make GraphQL testing a bit nicer.
968
-
969
- See the [history](https://github.com/pact-foundation/pact-js/issues/254#issuecomment-442185695), and below for an example.
970
-
971
- ## Tutorial (60 minutes)
972
-
973
- Learn everything in Pact JS in 60 minutes: https://github.com/pact-foundation/pact-workshop-js.
974
-
975
- The workshop takes you through all of the key concepts using a React consumer and an Express API.
976
-
977
- ## Examples
978
-
979
- ### HTTP APIs
980
-
981
- - [Complete Example (Node env)](https://github.com/pact-foundation/pact-js/tree/master/examples/e2e)
982
- - [Pact with AVA (Node env)](https://github.com/pact-foundation/pact-js/tree/master/examples/ava)
983
- - [Pact with Jest (Node env)](https://github.com/pact-foundation/pact-js/tree/master/examples/jest)
984
- - [Pact with TypeScript + Mocha](https://github.com/pact-foundation/pact-js/tree/master/examples/typescript)
985
- - [Pact with Mocha](https://github.com/pact-foundation/pact-js/tree/master/examples/mocha)
986
- - [Pact with GraphQL](https://github.com/pact-foundation/pact-js/tree/master/examples/graphql)
987
- - [Pact with Karma + Jasmine](https://github.com/pact-foundation/pact-js/tree/master/examples/karma/jasmine)
988
- - [Pact with Karma + Mocha](https://github.com/pact-foundation/pact-js/tree/master/examples/karma/mocha)
989
- - [Pact with React + Jest](https://github.com/pact-foundation/pact-workshop-js)
990
-
991
- ### Asynchronous APIs
992
-
993
- - [Asynchronous messages](https://github.com/pact-foundation/pact-js/tree/master/examples/messages)
994
- - [Serverless](https://github.com/pact-foundation/pact-js/tree/master/examples/serverless)
995
- ## Pact JS V3
996
-
997
- An initial beta version of Pact-JS with support for V3 specification features and XML matching has
998
- been released. Current support is for Node 10, 12 and 14. Thanks to the folks at [Align Tech](https://www.aligntech.com/) for sponsoring this work.
999
-
1000
- To install it:
1001
-
1002
- ```console
1003
- npm i @pact-foundation/pact@beta
1004
- ```
1005
-
1006
- For examples on how to use it, see [examples/v3/e2e](https://github.com/pact-foundation/pact-js/tree/feat/v3.0.0/examples/v3/e2e) and [examples/v3/todo-consumer](https://github.com/pact-foundation/pact-js/tree/feat/v3.0.0/examples/v3/todo-consumer) in the `v3.0.0` branch.
1007
-
1008
- **NOTE: The API of this implementation is likely to change. See this [discussion](https://github.com/pact-foundation/pact-js/discussions/681) for more**
1009
-
1010
- If migrating between beta versions, please see [MIGRATION.md](MIGRATION.md) for guidelines.
1011
-
1012
- ### Using the V3 matching rules
1013
-
1014
- There are a number of new matchers that can be used, like `integer` and `timestamp`. There are defined in the `MatchersV3` class that needs to be used with `PactV3` DSL.
1015
-
1016
- For example:
1017
-
1018
- ```javascript
1019
- const { PactV3, MatchersV3 } = require("@pact-foundation/pact/v3")
1020
- const {
1021
- eachLike,
1022
- atLeastLike,
1023
- integer,
1024
- timestamp,
1025
- boolean,
1026
- string,
1027
- regex,
1028
- like,
1029
- } = MatchersV3
1030
-
1031
- const animalBodyExpectation = {
1032
- id: integer(1),
1033
- available_from: timestamp("yyyy-MM-dd'T'HH:mm:ss.SSSX"),
1034
- first_name: string("Billy"),
1035
- last_name: string("Goat"),
1036
- animal: string("goat"),
1037
- age: integer(21),
1038
- gender: regex("F|M", "M"),
1039
- location: {
1040
- description: string("Melbourne Zoo"),
1041
- country: string("Australia"),
1042
- post_code: integer(3000),
1043
- },
1044
- eligibility: {
1045
- available: boolean(true),
1046
- previously_married: boolean(false),
1047
- },
1048
- interests: eachLike("walks in the garden/meadow"),
1049
- }
1050
- ```
1051
-
1052
- | Matcher | Parameters | Description |
1053
- | ---------------------- | -------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
1054
- | `like` | template | Applies the `type` matcher to value, which requires values to have the same type as the template |
1055
- | `eachLike` | template | Applies the `type` matcher to each value in an array, ensuring they match the template. Note that this matcher does not validate the length of the array, and the items within it |
1056
- | `atLeastOneLike` | template, count: number = 1 | Behaves like the `eachLike` matcher, but also applies a minimum length validation of one on the length of the array. The optional `count` parameter controls the number of examples generated. |
1057
- | `atLeastLike` | template, min: number, count?: number | Just like `atLeastOneLike`, but the minimum length is configurable. |
1058
- | `atMostLike` | template, max: number, count?: number | Behaves like the `eachLike` matcher, but also applies a maximum length validation on the length of the array. The optional `count` parameter controls the number of examples generated. |
1059
- | `constrainedArrayLike` | template, min: number, max: number, count?: number | Behaves like the `eachLike` matcher, but also applies a minimum and maximum length validation on the length of the array. The optional `count` parameter controls the number of examples generated. |
1060
- | `boolean` | example: boolean | Matches boolean values (true, false) |
1061
- | `integer` | example?: number | Value that must be an integer (must be a number and have no decimal places). If the example value is omitted, a V3 Random number generator will be used. |
1062
- | `decimal` | example?: number | Value that must be a decimal number (must be a number and have at least one digit in the decimal places). If the example value is omitted, a V3 Random number generator will be used. |
1063
- | `number` | example?: number | Value that must be a number. If the example value is omitted, a V3 Random number generator will be used. |
1064
- | `string` | example: string | Value that must be a string. |
1065
- | `regex` | pattern, example: string | Value that must match the given regular expression. |
1066
- | `equal` | example | Value that must be equal to the example. This is mainly used to reset the matching rules which cascade. |
1067
- | `timestamp` | format: string, example?: string | String value that must match the provided datetime format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) for details on the format string. If the example value is omitted, a value will be generated using a Timestamp generator and the current system date and time. |
1068
- | `time` | format: string, example?: string | String value that must match the provided time format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) for details on the format string. If the example value is omitted, a value will be generated using a Time generator and the current system time. |
1069
- | `date` | format: string, example?: string | String value that must match the provided date format string. See [Java SimpleDateFormat](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html) for details on the format string. If the example value is omitted, a value will be generated using a Date generator and the current system date. |
1070
- | `includes` | value: string | Value that must include the example value as a substring. |
1071
- | `nullValue` | | Value that must be null. This will only match the JSON Null value. For other content types, it will match if the attribute is missing. |
1072
- |`arrayContaining`| variants... | Matches the items in an array against a number of variants. Matching is successful if each variant occurs once in the array. Variants may be objects containing matching rules. |
1073
- | `eachKeyLike` | key: string, template: any | Object where the keys itself is ignored, but the values must match a particular shape. Variants may be objects containing matching rules |
1074
- |`fromProviderState`| expression: string, exampleValue: string | Sets a type matcher and a provider state generator. See the section below. |
1075
-
1076
- #### Array contains matcher
1077
-
1078
- The array contains matcher function allows you to match the actual list against a list of required variants. These work
1079
- by matching each item against each of the variants, and the matching succeeds if each variant matches at least one item. Order of
1080
- items in the list is not important.
1081
-
1082
- The variants can have a totally different structure, and can have their own matching rules to apply. For an example of how
1083
- these can be used to match a hypermedia format like Siren, see [Example Pact + Siren project](https://github.com/pactflow/example-siren).
1084
-
1085
- | function | description |
1086
- |----------|-------------|
1087
- | `arrayContaining` | Matches the items in an array against a number of variants. Matching is successful if each variant occurs once in the array. Variants may be objects containing matching rules. |
1088
-
1089
- ```js
1090
- {
1091
- "actions": arrayContaining(
1092
- {
1093
- "name": "update",
1094
- "method": "PUT",
1095
- "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
1096
- },
1097
- {
1098
- "name": "delete",
1099
- "method": "DELETE",
1100
- "href": url("http://localhost:9000", ["orders", regex("\\d+", "1234")])
1101
- }
1102
- )
1103
- }
1104
- ```
1105
-
1106
- #### Provider State Injected Values
1107
-
1108
- The `fromProviderState` matching function allows values to be generated based on values returned from the provider state callbacks. This should be used for the cases were database entries have auto-generated values and these values need to be used in the URLs or query parameters.
1109
-
1110
- For an example, see [examples/v3/provider-state-injected](https://github.com/pact-foundation/pact-js/tree/feat/v3.0.0/examples/v3/provider-state-injected).
1111
-
1112
- For this to work, in the consumer test we use the `fromProviderState` matching function which takes an expression and an example value. The example value will be used in the consumer test.
1113
-
1114
- For example:
1115
-
1116
- ```js
1117
- query: { accountNumber: fromProviderState("\${accountNumber}", "100") },
1118
- ```
1119
-
1120
- Then when the provider is verified, the provider state callback can return a map of values. These values will be used to generate the value using the expression supplied from the consumer test.
1121
-
1122
- For example:
1123
-
1124
- ```js
1125
- stateHandlers: {
1126
- "Account Test001 exists": (params) => {
1127
- const account = new Account(0, 0, "Test001", params.accountRef, new AccountNumber(0), Date.now(), Date.now())
1128
- const persistedAccount = accountRepository.save(account)
1129
- return { accountNumber: persistedAccount.accountNumber.id }
1130
- }
1131
- },
1132
- ```
1133
-
1134
- ### Using Pact with XML
1135
-
1136
- You can write both consumer and provider verification tests with XML requests or responses. For an example, see [examples/v3/todo-consumer/test/consumer.spec.js](https://github.com/pact-foundation/pact-js/blob/feat/v3.0.0/examples/v3/todo-consumer/test/consumer.spec.js).
1137
- There is an `XmlBuilder` class that provides a DSL to help construct XML bodies with matching rules and generators (NOTE that generators are not supported for XML at this time).
1138
-
1139
- for example:
1140
-
1141
- ```javascript
1142
- body: new XmlBuilder("1.0", "UTF-8", "ns1:projects").build((el) => {
1143
- el.setAttributes({
1144
- id: "1234",
1145
- "xmlns:ns1": "http://some.namespace/and/more/stuff",
1146
- })
1147
- el.eachLike(
1148
- "ns1:project",
1149
- {
1150
- id: integer(1),
1151
- type: "activity",
1152
- name: string("Project 1"),
1153
- due: timestamp("yyyy-MM-dd'T'HH:mm:ss.SZ", "2016-02-11T09:46:56.023Z"),
1154
- },
1155
- (project) => {
1156
- project.appendElement("ns1:tasks", {}, (task) => {
1157
- task.eachLike(
1158
- "ns1:task",
1159
- {
1160
- id: integer(1),
1161
- name: string("Task 1"),
1162
- done: boolean(true),
1163
- },
1164
- null,
1165
- { examples: 5 }
1166
- )
1167
- })
1168
- },
1169
- { examples: 2 }
1170
- )
1171
- })
1172
- ```
1173
-
1174
- ### Verifying providers with VerifierV3
1175
-
1176
- The `VerifierV3` class is no longer necessary. The `Verifier` class now supports
1177
- all the same options. `VerifierV3` still exists, but delegates to the `Verifier` class.
1178
-
1179
- #### Provider state callbacks
1180
-
1181
- Provider state callbacks have been updated to support parameters and return values.
1182
-
1183
- Simple callbacks run before the verification and receive optional parameters containing any key-value parameters defined in the pact file.
1184
-
1185
- The second form of callback accepts a `setup` and `teardown` function that execute on the lifecycle of the state setup. `setup` runs prior to the test, and `teardown` runs after the actual request has been sent to the provider.
1186
-
1187
- Provider state callbacks can also return a map of key-value values. These are used with provider-state injected values (see the section on that above).
1188
-
1189
- ```javascript
1190
- stateHandlers: {
1191
- // Simple state handler, runs before the verification
1192
- "Has no animals": () => {
1193
- return animalRepository.clear()
1194
- },
1195
- // Runs only on setup phase (no teardown)
1196
- "Has some animals": {
1197
- setup: () => {
1198
- return importData()
1199
- }
1200
- },
1201
- // Runs only on teardown phase (no setup)
1202
- "Has a broken dependency": {
1203
- setup: () => {
1204
- // make some dependency fail...
1205
- return Promise.resolve()
1206
- },
1207
- teardown: () => {
1208
- // fix the broken dependency!
1209
- return Promise.resolve()
1210
- }
1211
- },
1212
- // Return provider specific IDs
1213
- "Has an animal with ID": async (parameters) => {
1214
- await importData()
1215
- animalRepository.first().id = parameters.id
1216
- return {
1217
- description: `Animal with ID ${parameters.id} added to the db`,
1218
- id: parameters.id,
1219
- }
1220
- },
1221
- ```
1222
-
1223
- ### Debugging issues with Pact-JS V3
1224
-
1225
- You can change the log levels using the `LOG_LEVEL` environment variable.
1226
-
1227
- ### Debugging
1228
-
1229
- If your standard tricks don't get you anywhere, setting the logLevel to `debug` and increasing the timeout doesn't help and you don't know where else to look, it could be that the binaries we use to do much of the Pact magic aren't starting as expected.
1230
-
1231
- Try starting the mock service manually and seeing if it comes up. When submitting a bug report, it would be worth running these commands before hand as it will greatly help us:
1232
-
1233
- ```
1234
- ./node_modules/.bin/pact-mock-service
1235
- ```
1236
-
1237
- ...and also the verifier (it will whinge about missing params, but that means it works):
1238
-
1239
- ```
1240
- ./node_modules/.bin/pact-provider-verifier
159
+ .willRespondWith({
160
+ status: 200,
161
+ headers: { 'Content-Type': 'application/json' },
162
+ body: EXPECTED_BODY,
163
+ });
164
+
165
+ return provider.executeTest((mockserver) => {
166
+ // Act: test our API client behaves correctly
167
+ //
168
+ // Note we configure the DogService API client dynamically to
169
+ // point to the mock service Pact created for us, instead of
170
+ // the real one
171
+ dogService = new DogService(mockserver.url);
172
+ const response = await dogService.getMeDogs('today')
173
+
174
+ // Assert: check the result
175
+ expect(response.data[0]).to.deep.eq(dogExample);
176
+ });
177
+ });
178
+ });
1241
179
  ```
1242
180
 
1243
- ## Troubleshooting / FAQs
1244
-
1245
- If you are having issues, a good place to start is setting `logLevel: 'debug'` when configuring the `new Pact({...})` object. This will give you detailed in/out requests as far as Pact sees them during verification.
1246
-
1247
- ### Corporate Proxies / Firewalls
1248
-
1249
- If you're on a corporate machine, it's common for all network calls to route through a proxy - even requests that go to your own machine!
1250
-
1251
- The symptom presents as follows:
1252
-
1253
- 1. The mock server starts up correctly, as shown by a debug level log message such as this:
1254
-
1255
- ```
1256
- [2021-11-22 11:16:01.214 +0000] DEBUG (3863 on Matts-iMac): pact-core@11.0.1: INFO WEBrick::HTTPServer#start: pid=3864 port=50337
1257
- ```
1258
-
1259
- 2. You receive a conflicting message such as "The pact mock service doesn't appear to be running" and the tests never run or any before all blocks fail to complete.
1260
-
1261
- The problem is that the Pact framework attempts to ensure the mock service can be communicated with before the tests run. It does so via an HTTP call, which will be sent via any intermediate proxies if configured. The proxy is unlikely to know how send the request back to your machine, which results in a timeout or error.
1262
-
1263
- This may be resolved by ensuring the `http_proxy` and `no_proxy` directives are correctly set (usually, by excluding the address of the mock server such as `localhost` or `127.0.0.1`).
1264
-
1265
- ### Alpine + Docker
1266
-
1267
- See https://docs.pact.io/docker/.
1268
-
1269
- ### Parallel tests
1270
-
1271
- Pact tests are inherently stateful, as we need to keep track of the interactions on a per-test basis, to ensure each contract is validated in isolation from others. However, in larger test suites, this can result in slower test execution.
1272
-
1273
- Modern testing frameworks like Ava and Jest support parallel execution out-of-the-box, which
181
+ You can see (and run) the full version of this in `./examples/v3/typescript`, as well as other exmaples in the parent folder.
1274
182
 
1275
- The good news is, parallel test execution is possible, you need to ensure that:
183
+ ![----------](https://raw.githubusercontent.com/pactumjs/pactum/master/assets/rainbow.png)
1276
184
 
1277
- 1. Before any test run invocation, you remove any existing pact files, to prevent invalid / stale interactions being left over from previous test runs
1278
- 1. Each test is fully self-contained, with its **own mock server** on its **own port**
1279
- 1. You set the option `pactfileWriteMode` to `"merge"`, instructing Pact to merge any pact documents with the same consumer and provider pairing at the end of all test runs.
185
+ ### Provider package
1280
186
 
1281
- When all of your tests have completed, the result is the union of the all of the interactions from each test case in the generated pact file.
187
+ The main provider interface is the `Verifier` class of the `@pact-foundation/pact` package.
1282
188
 
1283
- See the following examples for working parallel tests:
189
+ #### Verifying a Provider
1284
190
 
1285
- - [Pact with AVA (Node env)](https://github.com/pact-foundation/pact-js/tree/master/examples/ava)
1286
- - [Pact with Mocha](https://github.com/pact-foundation/pact-js/tree/master/examples/mocha)
1287
-
1288
- ### Splitting tests across multiple files
1289
-
1290
- Pact tests tend to be quite long, due to the need to be specific about request/response payloads. Often times it is nicer to be able to split your tests across multiple files for manageability.
1291
-
1292
- You have a number of options to achieve this feat:
1293
-
1294
- 1. Consider implementing the [Parallel tests](#parallel-tests) guidelines.
1295
-
1296
- 1. Create a Pact test helper to orchestrate the setup and teardown of the mock service for multiple tests.
1297
-
1298
- In larger test bases, this can significantly reduce test suite time and the amount of code you have to manage.
1299
-
1300
- See this [example](https://github.com/tarciosaraiva/pact-melbjs/blob/master/helper.js) and this [issue](https://github.com/pact-foundation/pact-js/issues/11) for more.
1301
-
1302
- 1. Set `pactfileWriteMode` to `merge` in the `Pact()` constructor
1303
-
1304
- This will allow you to have multiple independent tests for a given Consumer-Provider pair, without it clobbering previous interactions, thereby allowing you to incrementally build up or modify your pact files.
1305
-
1306
- This feature addresses the use case of "my pact suite takes bloody ages to run, so I just want to replace the interactions that have been run in this test execution" and requires careful management
1307
-
1308
- _NOTE_: If using this approach, you _must_ be careful to clear out existing pact files (e.g. `rm ./pacts/*.json`) before you run tests to ensure you don't have left over requests that are no longer relevant.
1309
-
1310
- See this [PR](https://github.com/pact-foundation/pact-js/pull/48) for background.
1311
-
1312
- ### Test fails when it should pass
1313
-
1314
- TL;DR - you almost certainly have not properly handled (returned) a Promise.
1315
-
1316
- We see this sort of thing all of the time:
191
+ A provider test takes one or more pact files (contracts) as input, and Pact verifies that your provider adheres to the contract. In the simplest case, you can verify a provider as per below using a local pact file, although in practice you would usually use a Pact Broker to manage your contracts and CI/CD workflow.
1317
192
 
1318
193
  ```js
1319
- it("returns a successful thing", () => {
1320
- executeApiCallThatIsAPromise()
1321
- .then((response) => {
1322
- expect(response.data).to.eq({...})
1323
- })
1324
- .then(() => {
1325
- provider.verify()
1326
- })
1327
- })
1328
- ```
1329
-
1330
- There are several problems with this:
1331
-
1332
- 1. in the "returns a successful thing", the call to `executeApiCallThatIsAPromise()` is a function that returns a Promise, but is not returned by the function (`it` block) - this leaves a dangling, unhandled Promise. In your case it fails, but by the time it does the `it` block has already completed without problems - and returns a green result ✅.
1333
- 1. In the `then` block, the call to `provider.verify()` is also not returned, and will suffer the same fate as (1)
194
+ const { Verifier } = require('@pact-foundation/pact');
1334
195
 
1335
- _Side note_: Jasmine and other test frameworks may detect an unhandled promise rejection and report on it.
196
+ // (1) Start provider locally. Be sure to stub out any external dependencies
197
+ server.listen(8081, () => {
198
+ importData();
199
+ console.log('Animal Profile Service listening on http://localhost:8081');
200
+ });
1336
201
 
1337
- The correct code for the above is:
202
+ // (2) Verify that the provider meets all consumer expectations
203
+ describe('Pact Verification', () => {
204
+ it('validates the expectations of Matching Service', () => {
205
+ let token = 'INVALID TOKEN';
1338
206
 
1339
- ```js
1340
- it("returns a successful thing", () => {
1341
- return executeApiCallThatIsAPromise() // <- explicit return here, you could also use the "async/await" syntax here
1342
- .then((response) => {
1343
- expect(response.data).to.eq({...})
207
+ return new Verifier({
208
+ providerBaseUrl: 'http://localhost:8081', // <- location of your running provider
209
+ pactUrls: [ path.resolve(process.cwd(), "./pacts/SomeConsumer-SomeProvider.json") ],
1344
210
  })
1345
- .then(() => provider.verify()) // provider.verify() also returned
1346
- })
1347
- ```
1348
-
1349
- ### Test intermittent failures
1350
-
1351
- See above - you probably have not returned a Promise when you should have.
1352
-
1353
- ### Re-run specific verification failures
1354
-
1355
- If you prefix your test command (e.g. `npm t`) with the following environment variables, you can selectively run a specific interaction during provider verification.
1356
-
1357
- | variable name | description | comments |
1358
- | ---------------------- | ------------------------------------------------------------------------------------------------------ | ----------- |
1359
- | PACT_DESCRIPTION | select all tests that contain this string in its `description`(from the test output, or the pact file) | |
1360
- | PACT_PROVIDER_STATE | select all tests that contain this string in on of its `providerState` | |
1361
- | PACT_PROVIDER_NO_STATE | set to `TRUE` to select all tests what don't have any `providerState` | only for V3 |
1362
-
1363
- For the e2e example, let's assume we have the following failure:
1364
-
1365
- ```sh
1366
- 3 interactions, 2 failures
1367
-
1368
- Failed interactions:
1369
-
1370
- * A request for all animals given Has some animals
1371
-
1372
- * A request for an animal with id 1 given Has an animal with ID 1
1373
- ```
1374
-
1375
- If we wanted to target the second failure, we can extract the description and state as the bits before and after the word "given":
1376
-
1377
- ```sh
1378
- PACT_DESCRIPTION="A request for an animal with ID 1" PACT_PROVIDER_STATE="Has an animal with ID 1" npm t
1379
- ```
1380
-
1381
- ### Timeout
1382
-
1383
- Under the hood, Pact JS spins up a [Ruby Mock Service](https://github.com/pact-foundation/pact-mock-service-npm).
1384
- On some systems, this may take more than a few seconds to start. It is recommended
1385
- to review your unit testing timeout to ensure it has sufficient time to start the server.
1386
-
1387
- See [here](http://stackoverflow.com/questions/42496401/all-pact-js-tests-are-failing-with-same-errors/42518752) for more details.
1388
-
1389
- ### Usage with Jest
1390
-
1391
- Jest uses JSDOM under the hood which may cause issues with libraries making HTTP request.
1392
-
1393
- You'll need to add the following snippet to your `package.json` to ensure it uses
1394
- the proper Node environment:
211
+ .verifyProvider()
212
+ .then(() => {
213
+ console.log('Pact Verification Complete!');
214
+ });
215
+ });
216
+ });
1395
217
 
1396
- ```js
1397
- "jest": {
1398
- "testEnvironment": "node"
1399
- }
1400
218
  ```
1401
219
 
1402
- Also, [from Jest 20](https://facebook.github.io/jest/blog/2017/05/06/jest-20-delightful-testing-multi-project-runner.html), you can add the environment to the top of the test file as a comment. This will allow your pact test to run along side the rest of your JSDOM env tests.
1403
-
1404
- ```js
1405
- /**
1406
- * @jest-environment node
1407
- */
1408
- ```
220
+ It's best to run Pact verification tests as part of your unit testing suite, so you can readily access stubbing, IaC and other helpful tools.
1409
221
 
1410
- Jest also runs tests in parallel by default, which can be problematic with Pact which is stateful. See [parallel tests](#parallel-tests) to see how to make it run in parallel, or run Jest with the `--runInBand` [option](https://facebook.github.io/jest/docs/en/cli.html#runinband) to run them sequentially.
222
+ ![----------](https://raw.githubusercontent.com/pactumjs/pactum/master/assets/rainbow.png)
1411
223
 
1412
- See [this issue](https://github.com/pact-foundation/pact-js/issues/10) for background,
1413
- and the Jest [example](https://github.com/pact-foundation/pact-js/blob/master/examples/jest/package.json#L10-L12) for a working example.
224
+ ## Compatibility
1414
225
 
1415
- ### Usage with Angular
226
+ <details><summary>Specification Compatibility</summary>
1416
227
 
1417
- You way want to consider using this starter schematic: https://github.com/niklas-wortmann/ngx-pact
228
+ | Version | Stable | [Spec] Compatibility | Install |
229
+ | ------- | ------ | -------------------- | ------------------ |
230
+ | 10.x.x | Release Candidate | 2, 3 | See [installation] |
231
+ | 9.x.x | Yes | 2, 3\* | [9xx] |
1418
232
 
1419
- Angular's HttpClient filters out many headers from the response object, this may cause issues when validating a response in tests.
233
+ _\*_ v3 support is limited to the subset of functionality required to enable language inter-operable [Message support].
1420
234
 
1421
- You'll need to add the additional header `Access-Control-Expose-Headers`, this will allow specified headers to be passed to the response object. This can be done by declaring the header in the `willRespondWith` section of your interaction:
1422
-
1423
- ```js
1424
- "willRespondWith": {
1425
- "headers": {
1426
- "Access-Control-Expose-Headers": like("My-Header"),
1427
- "My-Header": "..."
1428
- },
1429
- ...
1430
- }
1431
- ```
235
+ </details>
1432
236
 
1433
- See [this issue](https://github.com/angular/angular/issues/13554) for background.
237
+ ## Roadmap
1434
238
 
239
+ The [roadmap](https://docs.pact.io/roadmap/) for Pact and Pact JS is outlined on our main website.
1435
240
  ## Contributing
1436
241
 
1437
- 1. Fork it
1438
- 2. Create your feature branch from the relevant tree (e.g. [v5] or [v6]) (`git checkout -b my-new-feature`)
1439
- 3. Commit your changes (`git commit -am 'Add some feature'`)
1440
- 4. Push to the branch (`git push origin my-new-feature`)
1441
- 5. Create new Pull Request
1442
-
1443
- ## Contact
242
+ See [CONTRIBUTING](CONTRIBUTING.md).
1444
243
 
1445
- Join us on [Slack](https://slack.pact.io)
1446
-
1447
- <a href="https://slack.pact.io"><img src="https://slack.pact.io/badge.svg"></img></a>
1448
-
1449
- or chat to us at
1450
-
1451
- - Twitter: [@pact_up](https://twitter.com/pact_up)
1452
- - Stack Overflow: https://stackoverflow.com/questions/tagged/pact
244
+ <a href="https://github.com/pact-foundation/pact-js/graphs/contributors">
245
+ <img src="https://contrib.rocks/image?repo=pact-foundation/pact-js" />
246
+ </a>
247
+ <br />
1453
248
 
1454
- [getting started with pact]: https://docs.pact.io/getting_started
1455
249
  [spec]: https://github.com/pact-foundation/pact-specification
250
+ [9xx]: https://github.com/pact-foundation/pact-js/
251
+ [pact website]: http://docs.pact.io/
252
+ [@pact_up]: https://twitter.com/pact_up
253
+ [pact specification v2]: https://github.com/pact-foundation/pact-specification/tree/version-2
254
+ [pact specification v3]: https://github.com/pact-foundation/pact-specification/tree/version-3
255
+ [library]: https://github.com/pact-foundation/pact-reference/releases
256
+ [installation]: #installation
257
+ [message support]: https://github.com/pact-foundation/pact-specification/tree/version-3#introduces-messages-for-services-that-communicate-via-event-streams-and-message-queues
1456
258
  [changelog]: https://github.com/pact-foundation/pact-js/blob/master/CHANGELOG.md
1457
- [pactflow]: https://pactflow.io
259
+ [pact broker]: https://github.com/pact-foundation/pact_broker
260
+ [pactflow]: https://pactflow.io