Skip to content

Add integration tests for API routes#40

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/create-tests-for-api-layer
Draft

Add integration tests for API routes#40
Copilot wants to merge 2 commits intomainfrom
copilot/create-tests-for-api-layer

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 11, 2026

Adds comprehensive integration test coverage for all API route handlers using Vitest and supertest against in-memory SQLite databases.

Tests Added

  • 7 new route test files: supplier, headquarters, product, order, orderDetail, delivery, orderDetailDelivery
  • 46 new tests covering CRUD operations, error cases, and foreign key relationships
  • 64 total tests passing (18 existing + 46 new)

Repository Bug Fix

Fixed singleton caching issue in 4 repository getter functions that prevented proper test isolation:

  • getHeadquartersRepository
  • getProductsRepository
  • getOrderDetailsRepository
  • getOrderDetailDeliveriesRepository

These repositories cached database connections across tests. Added test environment detection to return fresh instances:

export async function getHeadquartersRepository(isTest: boolean = false): Promise<HeadquartersRepository> {
  const isTestEnv = isTest || process.env.NODE_ENV === 'test' || process.env.VITEST === 'true';
  if (isTestEnv) {
    return createHeadquartersRepository(true);  // Fresh instance per test
  }
  // Singleton for production
  if (!headquartersRepo) {
    headquartersRepo = await createHeadquartersRepository(false);
  }
  return headquartersRepo;
}

Pattern already existed in branchesRepo, suppliersRepo, ordersRepo, and deliveriesRepo but was missing in the above four.

Coverage

  • Routes: 70.94% statement coverage, 100% function coverage
  • All tests follow established pattern: fresh DB per test, proper foreign key seeding, supertest for HTTP assertions
Original prompt

create tests for api layer

The user has attached the following file paths as relevant context:

  • .github\copilot-instructions.md

Created from VS Code.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

- Created integration tests for supplier, headquarters, product, order, orderDetail, delivery, and orderDetailDelivery routes
- Fixed repository singletons to properly handle test environment (headquarters, products, orderDetails, orderDetailDeliveries)
- All 64 tests passing including existing branch and suppliersRepo tests

Co-authored-by: webmaxru <1560278+webmaxru@users.noreply.github.com>
Copilot AI changed the title [WIP] Add tests for API layer functionality Add integration tests for API routes Feb 11, 2026
Copilot AI requested a review from webmaxru February 11, 2026 15:23
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