Skip to content

Conversation

@cteyton
Copy link
Contributor

@cteyton cteyton commented Dec 12, 2025

Explanation

Optimized integration test performance by refactoring the test setup pattern. The integration tests were slow (~90-120s) and memory-consuming because every test was recreating the database schema and initializing all hexas in beforeEach.

Key Changes:

  • Refactored all 10 test files to use beforeAll for shared datasource and TestApp initialization, eliminating per-test schema recreation overhead
  • Created cleanTestDatabase() helper function using TRUNCATE CASCADE for efficient database cleanup between tests
  • Implemented table name caching with WeakMap to avoid rebuilding the table list on every cleanup call
  • Reduced test timeout from 30s to 10s to catch slow tests earlier
  • Set maxWorkers: 1 for serial execution to reduce memory pressure
  • Removed DEBUG_TEST_PERF profiling code

Performance Results:

  • Before: ~90-120 seconds for full test suite (on my local laptop)
  • After: ~47.6 seconds
  • Improvement: ~52-60% faster execution

All 102 tests passing with proper test isolation maintained.

Type of Change

  • Bug fix
  • New feature
  • Improvement/Enhancement
  • Refactoring
  • Documentation
  • Breaking change

Affected Components

  • Domain packages affected: @packmind/integration-tests, @packmind/test-utils
  • Frontend / Backend / Both: Backend (test infrastructure)
  • Breaking changes (if any): None - all tests maintain the same behavior and isolation

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing completed
  • Test coverage maintained or improved

Test Details:

  • All 102 integration tests passing
  • Verified test isolation is maintained with shared datasource pattern
  • Confirmed database cleanup works correctly between tests
  • Performance verified: test suite execution time reduced by ~50%

TODO List

  • CHANGELOG Updated
  • Documentation Updated

Reviewer Notes

  • The beforeAll/afterAll pattern is intentional for performance optimization. Each test file now shares a single datasource instance, with data cleaned between tests using cleanTestDatabase()
  • The cleanTestDatabase() function uses TRUNCATE CASCADE for efficiency, with a fallback to DELETE if TRUNCATE isn't supported
  • Table name caching uses WeakMap for automatic garbage collection when datasources are destroyed
  • All test files follow the same pattern for consistency and maintainability

@sonarqubecloud
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants