Skip to content

Commit 839857b

Browse files
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

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

integrations/.nango/schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62636,5 +62636,5 @@
6263662636
"additionalProperties": false
6263762637
}
6263862638
},
62639-
"$comment": "This file was generated by Nango (v0.69.18)"
62639+
"$comment": "This file was generated by Nango (v0.69.20)"
6264062640
}

integrations/.nango/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ---------------------------
2-
// This file was generated by Nango (v0.69.18)
2+
// This file was generated by Nango (v0.69.20)
33
// You can version this file
44
// ---------------------------
55

integrations/hubspot/fixtures/change-user-role.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

integrations/hubspot/fixtures/create-company.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

integrations/hubspot/fixtures/create-contact.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

integrations/hubspot/fixtures/create-deal.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

integrations/hubspot/fixtures/create-note.json

Lines changed: 0 additions & 35 deletions
This file was deleted.

integrations/hubspot/fixtures/create-property.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

integrations/hubspot/fixtures/create-task.json

Lines changed: 0 additions & 37 deletions
This file was deleted.

integrations/hubspot/fixtures/create-user.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)