Achieve 100% Test Coverage for src/lib/api.ts
Issue Summary
The src/lib/api.ts file contains all API client functions for the eHRS frontend application. Current test coverage is incomplete, leaving potential bugs undetected. This issue
tracks the work to achieve maximum coverage by ONLY modifying the test file tests/lib/api.test.ts.
Current Behavior: Test coverage for api.ts is ~70% statements, ~45% branches, ~81% functions.
Expected Behavior: Achieve 100% coverage (statements, functions, lines) and ≥97% branches without modifying source files.
---
Problem Statement
What is broken or missing?
- Multiple API functions lack complete test coverage
- Edge cases and error handling paths are not tested
- Branch coverage is incomplete (conditional logic not fully tested)
Who is affected by this issue?
- Development team (reduced confidence in code changes)
- QA team (harder to identify regression issues)
- End users (potential undetected bugs in production)
What is the impact on users or the system?
- Risk of undetected API integration bugs
- Reduced code maintainability
- Potential runtime errors in production
How can the issue be reproduced?
1 cd /home/suma/ehrs-frontend-vite
2 npm run test:coverage
3 # Check coverage report for src/lib/api.ts
---
Proposed Solution
Approach:
1. Analyze current coverage report to identify uncovered lines/branches
2. Add comprehensive unit tests for all uncovered code paths
3. Test all edge cases, error conditions, and conditional branches
4. Ensure all tests pass and coverage reaches 100%
Files to Change:
- tests/lib/api.test.ts — Modify — Add missing test cases
- src/lib/api.ts — DO NOT MODIFY — Constraint requirement
Dependencies/Blockers:
- None — this is a testing-only task
---
Test-Driven Development
Acceptance Criteria (Given-When-Then)
Scenario 1: Coverage achieved
1 Given the api.ts module exists with all API functions
2 And test file tests/lib/api.test.ts is properly configured
3 And source file cannot be modified
4 When npm run test:coverage is executed
5 Then statements coverage for api.ts should be 100%
6 And functions coverage should be 100%
7 And lines coverage should be 100%
8 And branches coverage should be ≥97%
Scenario 2: All tests pass
1 Given tests/lib/api.test.ts with all test cases
2 When npm run test:run -- tests/lib/api.test.ts is executed
3 Then all tests should pass with 0 failures
Scenario 3: Source file unchanged
1 Given the constraint to not modify source files
2 When coverage improvement is complete
3 Then src/lib/api.ts should have zero changes
4 And only tests/lib/api.test.ts should be modified
---
Test Cases
┌───────┬────────────────────┬─────────────────────┬────────────────────────────────────────────────────────────────────────────────────┬─────────────────────────────┬────────┐
│ Te... │ Test Description │ Precondition │ Test Steps │ Expected Result │ Pri... │
├───────┼────────────────────┼─────────────────────┼────────────────────────────────────────────────────────────────────────────────────┼─────────────────────────────┼────────┤
│ TC... │ Test all API fu... │ Mock axios instance │ 1. Call each API function<br>2. Verify endpoint called<br>3. Verify response re... │ All functions work corre... │ High │
│ TC... │ Test edge cases │ Mock axios instance │ 1. Test null/undefined/empty<br>2. Test invalid data formats │ All edge cases handled g... │ High │
│ TC... │ Test error hand... │ Mock axios errors │ 1. Test 404, 500 errors<br>2. Test non-Axios errors │ Errors handled correctly │ High │
│ TC... │ Test interceptors │ Mock axios inter... │ 1. Setup interceptors<br>2. Verify token injection<br>3. Verify 401 handling<br... │ Interceptors work correctly │ High │
└───────┴────────────────────┴─────────────────────┴────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────┴────────┘
---
Unit Test Requirements
Components/Functions to Test:
┌──────────────────┬────────────────────────────┬────────────────────────────────┐
│ Module/Component │ Function/Hook │ Test Cases to Cover │
├──────────────────┼────────────────────────────┼────────────────────────────────┤
│ src/lib/api.ts │ All exported API functions │ TC-001, TC-002, TC-003, TC-004 │
└──────────────────┴────────────────────────────┴────────────────────────────────┘
Test Assertions Required:
- [ ] All functions are called correctly
- [ ] API endpoints are called with correct URLs
- [ ] Request payloads are correct
- [ ] Responses are returned correctly
- [ ] Error handling works for all error types
- [ ] Edge cases (null, undefined, empty) are handled
- [ ] Conditional logic branches are tested (where testable)
- [ ] Interceptors add/remove auth tokens correctly
- [ ] Cleanup functions work properly
---
Implementation Details
Files to Change
┌───────────────────────┬───────────────┬────────────────────────────────────────────────────┐
│ File Path │ Action │ Purpose │
├───────────────────────┼───────────────┼────────────────────────────────────────────────────┤
│ tests/lib/api.test.ts │ Modify │ Add missing test cases for all uncovered functions │
│ src/lib/api.ts │ DO NOT MODIFY │ Constraint: Source file must remain unchanged │
└───────────────────────┴───────────────┴────────────────────────────────────────────────────┘
---
Technical Considerations
- Mocking Strategy: Use vi.mock('axios') to mock axios instance
- Test Isolation: Use beforeEach to clear mocks between tests
- localStorage Mocking: Use vi.stubGlobal() for localStorage tests
- Coverage Tool: Use v8 coverage provider (already configured)
- Constraint: Do NOT modify src/lib/api.ts
---
Testing Strategy
Manual Testing
Test Environment:
- Browser: N/A (unit tests only)
- Test Runner: Vitest
Manual Verification Steps:
1. Run npm run test:run -- tests/lib/api.test.ts — all tests should pass
2. Run npm run test:coverage — verify coverage for api.ts
3. Verify src/lib/api.ts has zero changes (git diff)
Automated Testing
Unit Tests:
- [ ] All API function calls tested
- [ ] All error handling paths tested
- [ ] All testable conditional branches tested
- [ ] Interceptor setup/cleanup tested
---
Test Data Requirements
1 const mockAuthToken = { access_token: 'test-token', token_type: 'bearer' };
2 const mockUser = { id: 'user-123', email: '[email protected]', role: 'admin' };
3 const mockPatient = { id: 'p1', book_no: 12345, patient_name: 'John Doe' };
4 const mockDoctor = { id: 'd1', doctor_name: 'Dr. Smith', specialization: 'Cardiology' };
---
Definition of Done
- [ ] Acceptance criteria met (all Given-When-Then scenarios pass)
- [ ] All test cases executed and passing
- [ ] Unit tests written and passing (minimum coverage: 100% statements/functions/lines)
- [ ] Branches coverage ≥97%
- [ ] Source file src/lib/api.ts has zero changes
- [ ] Only tests/lib/api.test.ts modified
- [ ] No console errors or warnings
- [ ] TypeScript types defined (no any except in test mocks)
- [ ] ESLint/Prettier checks pass
---
Additional Context
Related Issues
- Related to: Overall test coverage improvement initiative
References
- Coverage tool: Vitest with v8 provider
- Mock library: Vitest built-in mocking
Notes
- 2 branches may be untestable without source changes (environment variable fallback, question ID fallback)
Edited by Suma Pullaiahgari