This document describes the comprehensive testing suite for the DevXP CLI application, covering unit tests, integration tests, CLI command tests with snapshots, performance tests, and end-to-end tests.
├── src/
│ ├── modules/
│ │ ├── xp-system.test.ts # Unit tests for XP calculations
│ │ ├── database.integration.test.ts # Integration tests for database
│ │ └── git-hooks.test.ts # Unit tests for git hooks
│ └── commands/
│ └── __tests__/
│ └── *.command.test.ts # CLI command tests with snapshots
├── e2e/
│ └── full-workflow.spec.ts # End-to-end workflow tests
├── jest.config.js # Jest configuration
├── jest.setup.js # Jest setup and helpers
└── playwright.config.ts # Playwright configuration
Tests for individual modules and functions in isolation.
Coverage:
- XP calculation logic and level progression
- Streak and multiplier calculations
- Challenge and achievement systems
- Git commit analysis and quality scoring
- Configuration management
Run:
npm run test:unitTests for database operations and module interactions.
Coverage:
- Database CRUD operations
- User management
- Activity tracking
- XP history
- Leaderboard queries
- Backup and restore functionality
- Data import/export
Run:
npm run test:integrationTests for CLI commands with snapshot testing.
Coverage:
- Command help output
- Shell integration commands
- Configuration commands
- Error handling
- JSON output formats
Run:
npm run test:unit -- shell-integration.command.testTests for shell integration and performance monitoring.
Coverage:
- Command debouncing
- Concurrent operations
- Heavy activity handling
- Resource usage
Run:
npm run test:integration -- --testNamePattern="Performance"Complete workflow tests using Playwright.
Coverage:
- Complete XP earning workflow
- Shell integration workflow
- Achievement and challenge workflow
- Data export/import workflow
- Multi-user scenarios
- Error recovery
Run:
npm run test:e2enpm run test:all# Unit tests only
npm run test:unit
# Integration tests only
npm run test:integration
# E2E tests only
npm run test:e2e
# With coverage report
npm run test:coverage
# Watch mode for development
npm run test:watch
# CI mode with coverage
npm run test:ci# Run specific test file
npx jest src/modules/xp-system.test.ts
# Run tests matching pattern
npx jest --testNamePattern="XP calculation"
# Run tests in specific directory
npx jest src/modulesThe test suite aims for the following coverage targets:
- Lines: 80%
- Functions: 70%
- Branches: 70%
- Statements: 80%
View coverage report:
npm run test:coverage
open coverage/lcov-report/index.htmlFile System:
jest.mock('fs/promises');Child Process:
jest.mock('child_process');Database:
jest.mock('./database');The jest.setup.js file provides global test helpers:
testHelpers.generateUserId()- Generate random user IDstestHelpers.generateTimestamp(daysAgo)- Generate timestampstestHelpers.sleep(ms)- Async sleep helper
Snapshots are used for CLI command output testing. Update snapshots when output changes are intentional:
# Update all snapshots
npm run test:unit -- -u
# Update specific snapshot
npm run test:unit -- shell-integration.command.test -uThe test suite includes custom Jest matchers:
expect(value).toBeWithinRange(min, max);Performance tests verify:
- Operations complete within time limits
- No memory leaks
- Proper resource cleanup
- Concurrent operation handling
End-to-end tests use Playwright to test complete workflows:
- Tests run in isolated temporary directories
- Each test creates its own git repository
- Automatic cleanup after tests
# Run with headed browser (if applicable)
npx playwright test --headed
# Debug specific test
npx playwright test --debug full-workflow.spec.ts
# View trace
npx playwright show-trace trace.zipname: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm run build
- run: npm run test:ci
- uses: actions/upload-artifact@v3
if: always()
with:
name: coverage
path: coverage/Integration tests use temporary SQLite databases in system temp directory.
E2E tests create temporary git repositories for testing hooks and commands.
All test data is automatically cleaned up after test completion.
"Database not initialized" errors:
- Ensure
beforeEachhooks properly initialize the database - Check that async operations are properly awaited
Snapshot mismatches:
- Review the diff carefully
- Update snapshots if changes are intentional
- Check for environment-specific output
E2E test timeouts:
- Increase timeout in
playwright.config.ts - Check for hanging async operations
- Verify CLI build is complete
Mock not working:
- Ensure mock is defined before module import
- Check mock path matches actual module path
- Verify mock implementation returns expected types
- Test Isolation: Each test should be independent
- Clear Names: Use descriptive test names
- Arrange-Act-Assert: Follow AAA pattern
- Mock External Dependencies: Don't make real API calls or file system changes
- Test Edge Cases: Include boundary conditions and error cases
- Keep Tests Fast: Mock heavy operations
- Use Fixtures: Share common test data setup
- Clean Up: Always clean up test data and restore mocks
When adding new features:
- Write unit tests for new functions
- Add integration tests for database changes
- Update E2E tests for new workflows
- Ensure coverage targets are met
- Update snapshots if CLI output changes
- Document new test helpers or patterns