Commit 839857b
authored
refactor(test): centralize integration test mocks (#421)
## Describe your changes
Refactoring of how integration test mocks are managed. We are moving
from a scattered, directory-based mock structure to a centralized,
single-file approach using `.test.json` files co-located with their
corresponding tests.
**Key Changes:**
1. **Centralized Mocks:** A new `*.test.json` file now holds all mock
data (input, output, API calls, etc.) for a single test, making tests
easier to read and maintain.
2. **New Fixture Providers:** We've refactored `NangoActionMock` to use
a strategy pattern for loading mocks (now called "Fixture Providers"),
ensuring full backward compatibility while enabling the new system.
* `LegacyFixtureProvider`: For old tests.
* `UnifiedFixtureProvider`: For new, centralized `.test.json` files.
* `RecordingFixtureProvider`: A new utility to automatically migrate old
tests to the new format.
3. **Async Loading:** The mock loading process is now asynchronous to
avoid synchronous I/O operations, with `getFixtureProvider` returning a
promise.
4. **Strict Error Handling:** The new provider enforces strict error
handling for missing critical mock data (like `batchSave` or `output`),
matching the behavior of the legacy system.
5. **Automated Migration:** A new workflow allows developers to
automatically generate the new `.test.json` files by running existing
tests with a `MIGRATE_MOCKS=true` environment variable.
This new structure improves the developer experience by making test data
easier to manage and reason about, and it provides a simple, automated
path for migrating existing tests.
## Issue ticket number and link
[NAN-4463](https://linear.app/nango/issue/NAN-4463/simplify-mock-file-structure-in-the-nango-integrations-folder)
## Checklist before requesting a review (skip if just adding/editing
APIs & templates)
- [x] I added tests, otherwise the reason is: This is a refactoring of
the testing framework itself.
- [ ] External API requests have `retries`
- [ ] Pagination is used where appropriate
- [ ] The built in `nango.paginate` call is used instead of a `while
(true)` loop
- [ ] Third party requests are NOT parallelized (this can cause issues
with rate limits)
- [ ] If the sync requires metadata the `nango.yaml` has `auto_start:
false`
- [ ] If the sync is a `full` sync then `track_deletes: true` is set
- [ ] I followed the best practices and guidelines from the [Writing
Integration
Scripts](/NangoHQ/integration-templates/blob/main/guides/WRITING_SCRIPTS.md)
doc
---
## Migration Guide
We have made migration as automated as possible. Follow these steps to
migrate an integration to the new structure.
### Step 1: Ensure Tests Pass
Before starting, make sure the existing tests for the integration are
passing using the legacy mocks.
```bash
npx vitest run integration-templates/integrations/<integration-name>/tests/
```
### Step 2: Run with Migration Mode
Run the tests again with the `MIGRATE_MOCKS` environment variable set to
`true`. This will trigger the `RecordingFixtureProvider`, which will
execute the tests against the old mocks and generate the new
`.test.json` files.
```bash
MIGRATE_MOCKS=true npx vitest run integration-templates/integrations/<integration-name>/tests/
```
*Note: You will see new `*.test.json` files appear next to your
`*.test.ts` files.*
### Step 3: Verify and Clean Up
1. **Run Tests Again:** Run the tests *without* the environment
variable. The system should now automatically pick up the new
`.test.json` files and use the `UnifiedFixtureProvider`.
```bash
npx vitest run
integration-templates/integrations/<integration-name>/tests/
```
2. **Verify:** Ensure all tests pass.
3. **Delete Old Mocks:** Once confirmed, you can safely delete the old
mock folders for the migrated tests from the `mocks/` directory.
### Step 4: Troubleshooting
If a test fails after migration:
- **Check the JSON:** Open the generated `.test.json` and verify the
data looks correct.
- **API Matching:** If an API call isn't matching, check the `api`
section. The loader uses subset matching, so ensure the critical
parameters in the `request` object match what the code is sending.
- **Re-record:** You can delete the generated `.test.json` and re-run
Step 2 to re-record the interaction.
### Step 5: Commit
Commit the new `.test.json` files and the removal of the old `mocks/`
directories.1 parent f0b7ef3 commit 839857b
File tree
150 files changed
+16826
-17775
lines changed- integrations
- .nango
- hubspot
- fixtures
- mocks
- change-user-role
- companies/Company
- contacts/Contact
- create-company
- create-contact
- create-deal
- create-note
- create-property
- create-task
- create-user
- currency-codes/CurrencyCode
- deals/Deal
- delete-company
- delete-contact
- delete-deal
- delete-task
- delete-user
- fetch-account-information
- fetch-pipelines
- fetch-properties
- nango
- delete/proxy
- crm/v3/objects
- companies/23735747388
- contacts/65283032841
- deals/22943530616
- tasks/61688519345
- settings/v3/users/71690856
- get/proxy
- account-info/v3/details
- crm/v3
- objects
- companies
- 23516293919
- contacts
- 64283084318
- 64283084319
- 64283084320
- deals
- 22983893905
- products
- tasks
- owners
- pipelines/deal
- properties
- deals
- line_items
- settings/v3
- currencies/codes
- users
- patch/proxy/crm/v3/objects
- companies
- 18796435435
- 23599699034
- 23618222883
- contacts
- 44882112721
- 65273051187
- 65276536194
- 65318450564
- deals/22983893905
- tasks/61710019858
- post/proxy
- crm/v3
- objects
- companies
- contacts
- deals
- notes
- tasks
- properties/companies
- settings/v3/users
- put/proxy/settings/v3/users/010101
- owners/HubspotOwner
- products/Product
- tasks/Task
- update-company
- update-contact
- update-deal
- update-task
- users/User
- tests
- internal
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
150 files changed
+16826
-17775
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62636 | 62636 | | |
62637 | 62637 | | |
62638 | 62638 | | |
62639 | | - | |
| 62639 | + | |
62640 | 62640 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
0 commit comments