This document provides comprehensive testing guidelines for the AI Trading Platform.
We use Vitest as our primary testing framework, chosen for its:
- Native TypeScript support
- Fast execution with ES modules
- Built-in coverage reporting
- Excellent developer experience
├── client/src/test/
│ ├── setup.ts # Test configuration
│ └── components/ # React component tests
│ └── AIInsights.test.tsx
├── server/test/ # Backend service tests
│ └── storage.test.ts
└── vitest.config.ts # Test configuration
# Run tests in watch mode
npm test
# Run all tests once
npm run test:run
# Run tests with coverage
npm run test:coverage
# Run tests with UI dashboard
npm run test:uiCoverage reports are generated in the coverage/ directory:
coverage/index.html- Visual coverage reportcoverage/coverage-final.json- JSON coverage data
import { render, screen } from '@testing-library/react'
import { describe, it, expect } from 'vitest'
import MyComponent from '@/components/MyComponent'
describe('MyComponent', () => {
it('renders correctly', () => {
render(<MyComponent />)
expect(screen.getByText('Expected Text')).toBeInTheDocument()
})
})import { describe, it, expect, beforeEach } from 'vitest';
import { MyService } from '../services/MyService';
describe('MyService', () => {
let service: MyService;
beforeEach(() => {
service = new MyService();
});
it('performs expected operation', async () => {
const result = await service.doSomething();
expect(result).toBeDefined();
});
});- Mock external dependencies (APIs, databases)
- Use
vi.fn()for function mocking - Mock WebSocket connections in component tests
- Avoid mocking internal business logic
- Minimum Coverage: 70% overall
- Critical Components: 90%+ coverage
- Exclusions: Test files, config files, type definitions
| Area | Target | Priority |
|---|---|---|
| Storage Layer | 90% | High |
| AI Services | 85% | High |
| React Components | 80% | Medium |
| API Routes | 85% | High |
| WebSocket Handlers | 75% | Medium |
Our CI pipeline runs on every push and PR:
- YAML Lint: Workflow configuration validation
- Lint Check: ESLint code quality validation
- Type Check: TypeScript compilation and type safety
- Unit Tests: Full test suite execution on Node.js matrix
- Integration Tests: SQLite and PostgreSQL database testing
- Coverage Report: Multi-source coverage analysis and upload
- Security Audit: npm dependency vulnerability scanning
For detailed information about CI/CD workflows, troubleshooting, and YAML configuration guidelines, see CI/CD Workflows Documentation.
Husky runs these checks before each commit:
- Code formatting (Prettier)
- Linting (ESLint)
- Type checking (TypeScript)
- Individual function/component testing
- Isolated business logic validation
- Fast execution (<100ms per test)
- API endpoint testing
- Database interaction testing
- Service integration validation
- React component rendering
- User interaction simulation
- Props and state validation
describe('ComponentName', () => {
describe('when condition', () => {
it('should do expected behavior', () => {
// Test implementation
});
});
});- Test files:
*.test.tsor*.test.tsx - Descriptive test names explaining behavior
- Group related tests with
describeblocks
- Use realistic test data
- Create reusable mock data factories
- Avoid hardcoded values when possible
// Good - specific assertions
expect(result.status).toBe('success');
expect(result.data).toHaveLength(3);
expect(result.timestamp).toBeInstanceOf(Date);
// Avoid - vague assertions
expect(result).toBeTruthy();
expect(result).toEqual(expect.anything());- Module Resolution: Ensure import paths match project structure
- Async Operations: Use
awaitfor async operations in tests - Mock Cleanup: Reset mocks between tests with
beforeEach - DOM Cleanup: React Testing Library auto-cleans DOM
# Run specific test file
npx vitest run path/to/test.ts
# Debug mode with console output
npx vitest run --reporter=verbose
# Run tests matching pattern
npx vitest run --grep "pattern"Before submitting a PR, ensure:
- All tests pass locally
- New features include tests
- Coverage meets minimum requirements
- No console errors in test output
- Tests follow naming conventions
- Mock data is realistic and relevant