This document summarizes the implementation of the unit testing infrastructure for MentorsMind Backend, addressing issue #151.
✅ Install and configure Jest with ts-jest for TypeScript support
- Jest and ts-jest are already installed in package.json
- Configuration is set up in jest.unit.config.ts
✅ Configure path aliases in Jest to match tsconfig.json
- Path alias
@/maps tosrc/directory - UUID mock is configured for consistent testing
✅ Set up jest.setup.ts for global test utilities and mocks
- Created
src/tests/jest.setup.tswith global test utilities - Includes random generators for strings, emails, and UUIDs
- Date utilities for future/past dates
- Automatic mock clearing and restoration
✅ Create mock factories for common dependencies
- Database mock factory (
src/tests/mocks/database.mock.ts) - Redis mock factory (
src/tests/mocks/redis.mock.ts) - Stellar SDK mock factory (
src/tests/mocks/stellar.mock.ts) - Email service mock factory (
src/tests/mocks/email.mock.ts)
✅ Configure coverage reporting
- Coverage thresholds set to 80% for lines, statements, and functions
- Coverage reporters: text, lcov, html, json-summary
- Coverage directory:
coverage/
-
src/tests/jest.setup.ts- Global test utilities (randomString, randomEmail, randomUUID, etc.)
- Date utilities (futureDate, pastDate, mockDate)
- Automatic mock management (clearMocks, restoreMocks)
- Console suppression for cleaner test output
-
src/tests/mocks/database.mock.ts- Mock PostgreSQL pool and query functions
- Helper functions for creating mock query results
- Support for INSERT, UPDATE, DELETE operations
- Database module mocking utilities
-
src/tests/mocks/redis.mock.ts- Mock Redis client with all common operations
- Support for strings, hashes, lists, sets, sorted sets
- Redis module mocking utilities
- Cache service mock factory
-
src/tests/mocks/stellar.mock.ts- Mock Stellar SDK server and operations
- Account, transaction, and operation factories
- Support for payments, account creation, trustlines
- Stellar module mocking utilities
-
src/tests/mocks/email.mock.ts- Mock email service with all email types
- Support for welcome, verification, password reset emails
- Booking, payment, and notification email mocks
- Nodemailer module mocking utilities
-
src/tests/mocks/index.ts- Central export for all mock factories
- Easy importing for test files
-
UNIT_TESTING_GUIDE.md- Comprehensive guide for using the testing infrastructure
- Examples for all mock factories
- Best practices and troubleshooting
- Quick start instructions
jest.unit.config.ts- Added
setupFilesAfterEnvpointing tojest.setup.ts - Configured path aliases for
@/anduuid - Maintained existing test patterns and coverage settings
- Added
import { mockDatabaseModule, setupDatabaseMocks } from '../tests/mocks';
const mockPool = mockDatabaseModule();
setupDatabaseMocks(mockPool);
// In your test
mockPool.query.mockResolvedValue({
rows: [{ id: '1', name: 'Test' }],
rowCount: 1,
});import { mockRedisModule, setupRedisMocks } from '../tests/mocks';
const mockRedis = mockRedisModule();
setupRedisMocks(mockRedis);
// In your test
mockRedis.get.mockResolvedValue('cached_value');import { mockStellarModule, setupStellarMocks, createMockStellarAccount } from '../tests/mocks';
const mockServer = mockStellarModule();
setupStellarMocks(mockServer);
// In your test
mockServer.loadAccount.mockResolvedValue(
createMockStellarAccount({ balance: '1000.0000000' })
);import { mockEmailServiceModule, setupEmailServiceMocks } from '../tests/mocks';
const mockEmail = mockEmailServiceModule();
setupEmailServiceMocks(mockEmail);
// In your test
mockEmail.sendWelcomeEmail.mockResolvedValue({
messageId: 'msg_123',
accepted: ['[email protected]'],
});# Run all unit tests
npm test
# Run with coverage
npm run test:coverage
# Run in watch mode
npm run test:watch
# Run specific test file
npm test -- path/to/test.unit.test.tsThe unit test configuration includes:
- Thresholds: 80% for lines, statements, functions; 70% for branches
- Reporters: text (console), lcov (for CI/CD), html (for browsing), json-summary
- Directory:
coverage/ - Included: All TypeScript files in
src/ - Excluded: Type definitions, index files, test files, documentation
- Test Isolation: Each test is independent with automatic mock clearing
- Clear Structure: Arrange-Act-Assert pattern encouraged
- Descriptive Names: Test names clearly describe expected behavior
- Edge Case Coverage: Utilities for testing error conditions and boundaries
- Type Safety: Full TypeScript support with proper type definitions
- Documentation: Comprehensive guide with examples and troubleshooting
The unit testing infrastructure integrates seamlessly with:
- Existing Jest Configuration: Works alongside
jest.config.ts,jest.integration.config.ts, etc. - Existing Factories: Complements
src/tests/factories/for database test data - Existing Setup: Separate from
src/tests/setup.ts(integration tests) - Existing Tests: All existing unit tests continue to work
To use the new infrastructure:
- Install dependencies:
npm install - Run existing tests to verify:
npm test - Write new unit tests using the mock factories
- Refer to
UNIT_TESTING_GUIDE.mdfor detailed usage
- TypeScript errors in mock files are expected and will resolve at runtime when processed by ts-jest
- The infrastructure is designed for unit tests that don't require database connections
- Integration tests should continue using
src/tests/setup.tswith real database connections - All mock factories follow consistent patterns for easy adoption
UNIT_TESTING_GUIDE.md- Comprehensive usage guideTESTING_SETUP.md- Existing testing setup documentationjest.unit.config.ts- Unit test configurationjest.config.ts- Main Jest configuration