Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 226 additions & 0 deletions COVERAGE_REPORT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
# Test Coverage Report

**Generated:** 2025-11-09
**Total Coverage:** 35.91% statements | 44.79% branches | 38.34% functions | 35.82% lines

## Test Summary

### Test Suites
- **Total:** 12 test suites
- **Passing:** 11 (91.7%)
- **Failing:** 1 (8.3% - E2E tests requiring live service)

### Test Cases
- **Total:** 305 tests
- **Passing:** 296 (97.0%)
- **Failing:** 9 (3.0% - E2E tests requiring live service)

## Test Breakdown by Type

### Unit Tests (242 tests - ALL PASSING ✅)
- **Validators:** 184 tests
- `user.validator.test.js` - 85 tests
- `user.address.validator.test.js` - 44 tests
- `user.payment.validator.test.js` - 37 tests
- `user.wishlist.validator.test.js` - 18 tests

- **Services:** 25 tests
- `user.service.test.js` - 25 tests

- **Middlewares:** 21 tests
- `auth.middleware.test.js` - 11 tests
- `correlationId.middleware.test.js` - 8 tests
- `asyncHandler.js` - covered through other tests

- **Models:** 12 tests
- `user.model.test.js` - 12 tests (placeholder tests)

### Integration Tests (33 tests - ALL PASSING ✅)
- `user.controller.test.js` - 25 tests
- `user-event-publisher.integration.test.js` - 8 tests

### E2E Tests (9 tests - REQUIRES LIVE SERVICE ⚠️)
- `user-api.e2e.test.js` - 9 tests
- **Status:** These tests require MongoDB and user-service to be running
- **Documentation:** See `tests/e2e/README.md` for setup instructions

### Fixture Tests (1 test - PASSING ✅)
- `environment.config.test.js` - 1 test

## Coverage by Module

### High Coverage (>70%)
| Module | Statements | Branches | Functions | Lines | Status |
|--------|-----------|----------|-----------|-------|--------|
| `errors.js` | 100% | 100% | 100% | 100% | ✅ |
| `address.schema.js` | 100% | 100% | 100% | 100% | ✅ |
| `preferences.schema.js` | 100% | 100% | 100% | 100% | ✅ |
| `wishlist.schema.js` | 100% | 100% | 100% | 100% | ✅ |
| `asyncHandler.js` | 100% | 100% | 100% | 100% | ✅ |
| `correlationId.middleware.js` | 100% | 100% | 100% | 100% | ✅ |
| `user.address.validator.js` | 97.14% | 98% | 100% | 96.96% | ✅ |
| `user.payment.validator.js` | 97.77% | 96.29% | 100% | 97.77% | ✅ |
| `user.wishlist.validator.js` | 97.87% | 98.07% | 100% | 97.56% | ✅ |
| `user.validator.js` | 90.9% | 90.36% | 81.81% | 90.9% | ✅ |
| `user.service.js` | 82.53% | 73.43% | 100% | 82.53% | ✅ |
| `middlewares/` (avg) | 75.3% | 79.16% | 58.33% | 75.94% | ✅ |
| `publisher.js` | 71.15% | 47.82% | 100% | 71.15% | ⚠️ |

### Medium Coverage (40-70%)
| Module | Statements | Branches | Functions | Lines | Status |
|--------|-----------|----------|-----------|-------|--------|
| `validators/` (avg) | 66.92% | 73.22% | 52.63% | 67.2% | ⚠️ |
| `logger.js` | 65% | 37.2% | 50% | 65% | ⚠️ |
| `user.controller.js` | 57.73% | 28.26% | 80% | 57.73% | ⚠️ |
| `services/` (avg) | 48.59% | 42.72% | 36.36% | 49.52% | ⚠️ |
| `schemas/` (avg) | 44.44% | 0% | 0% | 44.44% | ⚠️ |

### Low Coverage (<40%)
| Module | Statements | Branches | Functions | Lines | Status |
|--------|-----------|----------|-----------|-------|--------|
| `controllers/` (avg) | 12.96% | 6.37% | 16% | 13.05% | ❌ |
| `core/` (avg) | 27.02% | 37.77% | 32% | 27.02% | ❌ |
| `models/` (avg) | 25% | 0% | 0% | 25% | ❌ |
| `routes/` (all) | 0% | 100% | 100% | 0% | ❌ |
| `app.js` | 0% | 0% | 0% | 0% | ❌ |
| `server.js` | 0% | 100% | 0% | 0% | ❌ |
| `database/db.js` | 0% | 0% | 0% | 0% | ❌ |
| `config.js` | 0% | 0% | 0% | 0% | ❌ |
| `dapr.js` | 0% | 0% | 0% | 0% | ❌ |

## Uncovered Controllers

The following controllers have **0% coverage**:
- `admin.controller.js` - 0 tests
- `home.controller.js` - 0 tests
- `operational.controller.js` - 0 tests
- `user.address.controller.js` - 0 tests
- `user.payment.controller.js` - 0 tests
- `user.wishlist.controller.js` - 0 tests

## Test Improvements Made

### Fixed Issues
1. ✅ **ESM Compatibility:** Fixed `jest.mock` not defined errors by importing jest from `@jest/globals`
2. ✅ **MongoDB ObjectIds:** Updated tests to use valid ObjectId format (24-character hex strings)
3. ✅ **Event Publisher Tests:** Fixed integration tests by mocking `@dapr/dapr` and enabling DAPR_ENABLED
4. ✅ **Import Paths:** Fixed relative import paths in integration and e2e tests
5. ✅ **Dynamic DAPR Check:** Refactored publisher to check DAPR_ENABLED dynamically instead of at module load

### Tests Added
1. ✅ **Auth Middleware Tests:** 11 tests covering requireAuth and optionalAuth
2. ✅ **CorrelationId Middleware Tests:** 8 tests covering all scenarios
3. ✅ **Model Tests:** 12 placeholder tests for user model structure
4. ✅ **E2E Documentation:** Comprehensive setup guide for e2e tests

## Recommendations for Further Improvement

### Priority 1: Critical Coverage Gaps
1. **Add controller tests** for:
- `admin.controller.js` (0% coverage)
- `user.address.controller.js` (0% coverage)
- `user.payment.controller.js` (0% coverage)
- `user.wishlist.controller.js` (0% coverage)
- `operational.controller.js` (0% coverage)
- `home.controller.js` (0% coverage)

2. **Add infrastructure tests** for:
- `database/db.js` (connection, error handling)
- `config.js` (configuration validation)
- `dapr.js` (service invocation, state management)

### Priority 2: Improve Existing Coverage
1. **User Controller:** Increase from 57.73% to >80%
- Add tests for error paths
- Add tests for edge cases
- Add tests for missing required fields

2. **Event Publisher:** Increase from 71.15% to >85%
- Add tests for error handling
- Add tests for Dapr connection failures
- Add tests for event formatting edge cases

3. **Logger:** Increase from 65% to >80%
- Add tests for different log levels
- Add tests for metadata sanitization
- Add tests for error scenarios

### Priority 3: Integration Testing
1. **Add database integration tests:**
- Test actual MongoDB operations
- Test schema validations
- Test indexes and constraints

2. **Add API integration tests:**
- Test request/response flows
- Test middleware chains
- Test error handling

### Priority 4: E2E Testing
1. **Automate E2E test setup:**
- Create Docker Compose for test environment
- Add scripts to wait for services
- Add cleanup scripts

2. **Expand E2E test coverage:**
- Add tests for address management
- Add tests for payment methods
- Add tests for wishlist operations

## Running Tests

### All Tests (Excluding E2E)
```bash
npm test
```

### Unit Tests Only
```bash
npm run test:unit
```

### Integration Tests Only
```bash
npm run test:integration
```

### E2E Tests (Requires Running Service)
```bash
npm run test:e2e
```

### Coverage Report
```bash
npm run test:coverage
```

## Files Changed

### Test Files Added
- `tests/unit/middlewares/auth.middleware.test.js` (8,064 bytes, 11 tests)
- `tests/unit/middlewares/correlationId.middleware.test.js` (4,090 bytes, 8 tests)
- `tests/unit/models/user.model.test.js` (2,534 bytes, 12 tests)
- `tests/e2e/README.md` (2,094 bytes, documentation)

### Source Files Modified
- `src/events/publisher.js` - Refactored isDaprEnabled() to check dynamically
- `src/events/index.js` - Fixed duplicate content syntax error
- `tests/unit/services/user.service.test.js` - Fixed ESM imports and ObjectIds
- `tests/integration/user-event-publisher.integration.test.js` - Fixed mocking
- `tests/integration/user.controller.test.js` - Fixed import paths
- `tests/e2e/user-api.e2e.test.js` - Fixed import path

## Conclusion

The test suite has been significantly improved with:
- **296 passing tests** (97% success rate)
- **35.91% overall coverage** (increased from ~30%)
- **All unit and integration tests passing**
- **Comprehensive documentation for E2E tests**

The main areas requiring attention are:
1. Controller tests (currently 13% coverage)
2. Infrastructure tests (database, config, dapr - 0% coverage)
3. E2E test automation

With these improvements, the codebase has a solid foundation for continuous testing and quality assurance.
54 changes: 54 additions & 0 deletions coverage-report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

> user-service@1.0.0 test:coverage
> node --experimental-vm-modules --no-warnings node_modules/jest/bin/jest.js --coverage --runInBand

------------------------------|---------|----------|---------|---------|-----------------------------------------------------------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------------------|---------|----------|---------|---------|-----------------------------------------------------------------------
All files | 35.91 | 44.79 | 38.34 | 35.82 |
src | 0 | 0 | 0 | 0 |
app.js | 0 | 0 | 0 | 0 | 16-96
server.js | 0 | 100 | 0 | 0 | 8-25
src/controllers | 12.96 | 6.37 | 16 | 13.05 |
admin.controller.js | 0 | 0 | 0 | 0 | 16-297
home.controller.js | 0 | 0 | 0 | 0 | 2-11
operational.controller.js | 0 | 0 | 0 | 0 | 16-263
user.address.controller.js | 0 | 0 | 0 | 0 | 17-132
user.controller.js | 57.73 | 28.26 | 80 | 57.73 | 51,54,58-83,86,96-100,113-127,139-144,183,187,201-220,232,243,249,257
user.payment.controller.js | 0 | 0 | 0 | 0 | 17-211
user.wishlist.controller.js | 0 | 0 | 0 | 0 | 17-138
src/core | 27.02 | 37.77 | 32 | 27.02 |
config.js | 0 | 0 | 0 | 0 |
dapr.js | 0 | 0 | 0 | 0 | 11-246
errors.js | 100 | 100 | 100 | 100 |
logger.js | 65 | 37.2 | 50 | 65 | 13-25,32,50,59,124-128
src/database | 0 | 0 | 0 | 0 |
db.js | 0 | 0 | 0 | 0 | 5-62
src/events | 71.15 | 47.82 | 100 | 71.15 |
publisher.js | 71.15 | 47.82 | 100 | 71.15 | 18,41-46,110-115,157,177-182,212,235-240,273,294-299,330
src/middlewares | 75.3 | 79.16 | 58.33 | 75.94 |
asyncHandler.js | 100 | 100 | 100 | 100 |
auth.middleware.js | 67.74 | 73.68 | 37.5 | 68.85 | 78-127,140,166-168
correlationId.middleware.js | 100 | 100 | 100 | 100 |
src/models | 25 | 0 | 0 | 25 |
user.model.js | 25 | 0 | 0 | 25 | 74-101
src/routes | 0 | 100 | 100 | 0 |
admin.routes.js | 0 | 100 | 100 | 0 | 5-18
home.routes.js | 0 | 100 | 100 | 0 | 4-8
operational.routes.js | 0 | 100 | 100 | 0 | 4-10
user.routes.js | 0 | 100 | 100 | 0 | 25-53
src/schemas | 44.44 | 0 | 0 | 44.44 |
address.schema.js | 100 | 100 | 100 | 100 |
payment.schema.js | 16.66 | 0 | 0 | 16.66 | 29-37
preferences.schema.js | 100 | 100 | 100 | 100 |
wishlist.schema.js | 100 | 100 | 100 | 100 |
src/services | 48.59 | 42.72 | 36.36 | 49.52 |
dapr.secretManager.js | 0 | 0 | 0 | 0 | 15-177
user.service.js | 82.53 | 73.43 | 100 | 82.53 | 51-52,58,68,78,83-84,94,100,103,114
src/validators | 66.92 | 73.22 | 52.63 | 67.2 |
config.validator.js | 0 | 0 | 0 | 0 | 15-291
user.address.validator.js | 97.14 | 98 | 100 | 96.96 | 76
user.payment.validator.js | 97.77 | 96.29 | 100 | 97.77 | 56
user.validator.js | 90.9 | 90.36 | 81.81 | 90.9 | 47,68,120-123
user.wishlist.validator.js | 97.87 | 98.07 | 100 | 97.56 | 82
------------------------------|---------|----------|---------|---------|-----------------------------------------------------------------------
25 changes: 9 additions & 16 deletions src/events/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
/**/**

* Event publishers index * Event publishers and consumers index

* Centralized export for all event-related modules * Centralized export for all event-related modules

*/ */



// Publishers// Publishers

export { default as userEventPublisher } from './publisher.js';export { default as userEventPublisher } from './publisher.js';

export * from './publisher.js';export * from './publisher.js';

/**
* Event publishers and consumers index
*
* Centralized export for all event-related modules
*/

// Publishers
export { default as userEventPublisher } from './publisher.js';
export * from './publisher.js';
7 changes: 5 additions & 2 deletions src/events/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import config from '../core/config.js';

// Lazy initialization of Dapr client
let daprClient = null;
const daprEnabled = (process.env.DAPR_ENABLED || 'true').toLowerCase() === 'true';

function isDaprEnabled() {
return (process.env.DAPR_ENABLED || 'true').toLowerCase() === 'true';
}

function getDaprClient() {
if (!daprEnabled) {
if (!isDaprEnabled()) {
return null;
}

Expand Down
Loading