Skip to content

Commit e948b89

Browse files
committed
Genric message
1 parent 5218946 commit e948b89

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

README.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Lessons learned while working with 50 companies
2727
- [`Strategy & Workflow`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#section-1-strategy-and-workflow) - Which tests should you write in 2025? (5 best practices)
2828
- [`Database And Infrastructure Setup`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#section-2-infrastructure-and-database-setup) - Optimizing your DB, MQ and other infra for testing (6 best practices)
2929
- [`Web Server Setup`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#section-3-web-server-setup) - Good practices for starting and stopping the backend API (3 best practices)
30-
- [`The Test Anatomy`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#section-4-test-test-anatomy-basics) - The bread and butter of a component test (6 best practices)
30+
- [`The Test Anatomy`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#section-4-the-test-anatomy) - The bread and butter of a component test (6 best practices)
3131
- [`Integration`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#Section-5-integrations) - Techniques for testing collaborations with 3rd party components (8 best practices)
3232
- [`Dealing With Data`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#section-6-dealing-with-data) - Patterns and practices for testing the application data and database (8 best practices)
3333
- [`Message Queue`](https://github.com/testjavascript/nodejs-integration-tests-best-practices#section-7-message-queues) - Correctly testing flows that start or end at a queue (8 best practices)
@@ -459,7 +459,7 @@ beforeAll(async () => {
459459

460460
<br/><br/>
461461

462-
## **Section 4: The test anatomy (basics)**
462+
## **Section 4: The test anatomy**
463463

464464
<br/>
465465

@@ -684,7 +684,7 @@ Find here the [same content as a course](https://testjavascript.com/), online [w
684684

685685
<br/><br/>
686686

687-
## **Section 5: Integrations**
687+
## **Section 5: Integrations With Other Services and APIs**
688688

689689
<br/>
690690

@@ -933,9 +933,45 @@ test('When order failed, send mail to admin', async () => {
933933

934934
<br/><br/>
935935

936-
### ⚪️ 8. Fake the time to minimize network call duration
936+
### ⚪️ 8. Code against a strict API provider contract
937937

938-
🏷&nbsp; **Tags:** `#basic, #draft`
938+
🏷&nbsp; **Tags:** `#advanced`
939+
940+
:white_check_mark: **Do:** Make the API provider produce artifacts like an OpenAPI document, which, with additional tooling, can make consumer tests fail early if their understanding deviates from the real API supported schemas
941+
942+
A common trap in API integrations is assuming requests and responses follow a certain structure, only to discover in production that something doesn’t match. Without a contract, consumers might send an unexpected query parameter, miss a required field, or misinterpret a response. When relying on mocking-only - these issues pass unnoticed in testing and might fail in production once they meet the real API. The risk grows when providers update their API, breaking assumptions silently
943+
944+
One simple solution is to share types or schemas between the provider and consumer, such as a TypeScript type definition or JSON Schema in a shared package. This ensures tests fail when a type-related breaking change occurs. But this only catches structural issues—it won’t help with incorrect query parameters, outdated routes, or missing headers
945+
946+
A stronger approach is generating an API client from an OpenAPI document using tools like [openapi-fetch](https://www.npmjs.com/package/openapi-fetch). This enforces API correctness at compile time and instantly fails when something is off—like an outdated endpoint or missing a required field. Various vendors offer a mock server that among other features can validate a request against a pre-configured OpenAPI
947+
948+
For even greater confidence with additional cost, a test-kit approach should be considered. Here, the provider supplies a lightweight mock of their API that consumers integrate into their tests. This catches not only request mismatches but also state-related issues, such as expecting an item to persist when the real API behaves differently. For the usual integration, an OpenAPI-based solution is the way to go
949+
950+
This guide wouldn’t be complete without mentioning [PACT Consumer-Driven Contracts](https://docs.pact.io/) — an innovative but opinionated framework for syncing providers and consumers. While feature-rich, its proprietary workflow raises doubts about broad developer adoption. The same vendor now offers a new, OpenAPI-based model that aligns better with industry standards
951+
952+
![Contract options](/graphics/contract-options.png)
953+
954+
<br/>
955+
956+
👀 &nbsp; **Alternatives:** E2E; Mocks; PACT;
957+
958+
<br/>
959+
960+
<details><summary>✏ <b>Code Examples</b></summary>
961+
962+
```javascript
963+
// TODO
964+
```
965+
966+
➡️ [Full code here]()
967+
968+
</details>
969+
970+
<br/><br/>
971+
972+
### ⚪️ 9. Fake the time to minimize network call duration
973+
974+
🏷&nbsp; **Tags:** `#basic`
939975

940976
:white_check_mark: **Do:** Interception tools include record mode which ...; use this to become aware of the integration it self, but also to its various patterns. Ensure all variations are covered with testing. You may use the recorded file as default; Do this in staging environment; Valuable when there are many integrations.
941977

graphics/contract-options.png

60.3 KB
Loading

0 commit comments

Comments
 (0)