|
| 1 | +# Dates-LE Testing Guidelines |
| 2 | + |
| 3 | +This document outlines testing practices and policies for Dates-LE development. |
| 4 | + |
| 5 | +## Core Principle |
| 6 | + |
| 7 | +**No broken or failed tests are allowed in commits.** |
| 8 | + |
| 9 | +All tests must pass before code can be committed or merged. This ensures code quality and prevents regressions. |
| 10 | + |
| 11 | +## Running Tests |
| 12 | + |
| 13 | +### Run All Tests |
| 14 | + |
| 15 | +```bash |
| 16 | +bun run test |
| 17 | +``` |
| 18 | + |
| 19 | +### Run Tests with Coverage |
| 20 | + |
| 21 | +```bash |
| 22 | +bun run test:coverage |
| 23 | +``` |
| 24 | + |
| 25 | +### Run Tests in Watch Mode |
| 26 | + |
| 27 | +```bash |
| 28 | +bun run test:watch |
| 29 | +``` |
| 30 | + |
| 31 | +### Run Tests for Specific File |
| 32 | + |
| 33 | +```bash |
| 34 | +bun x vitest run src/extraction/formats/json.test.ts |
| 35 | +``` |
| 36 | + |
| 37 | +## Test Structure |
| 38 | + |
| 39 | +### Unit Tests |
| 40 | + |
| 41 | +Located in `src/**/*.test.ts` and `src/**/*.spec.ts`: |
| 42 | + |
| 43 | +- **Pure function tests** - Test extraction logic in isolation |
| 44 | +- **Format tests** - Test JSON, YAML, CSV, log file extraction |
| 45 | +- **Utility tests** - Test helper functions |
| 46 | +- **Configuration tests** - Test config validation |
| 47 | + |
| 48 | +### Integration Tests |
| 49 | + |
| 50 | +Located in `src/**/*.spec.ts`: |
| 51 | + |
| 52 | +- **Sample file tests** - Test extraction against real file formats |
| 53 | +- **Cross-platform tests** - Ensure case-sensitivity compatibility |
| 54 | +- **End-to-end workflows** - Test complete command flows |
| 55 | + |
| 56 | +## Test Coverage Requirements |
| 57 | + |
| 58 | +- **Minimum Coverage**: Maintain reasonable coverage across core functionality |
| 59 | +- **Critical Paths**: All extraction logic must be tested |
| 60 | +- **Error Handling**: All error paths must be covered |
| 61 | +- **Edge Cases**: Boundary conditions must be tested |
| 62 | + |
| 63 | +## Before Committing |
| 64 | + |
| 65 | +### Checklist |
| 66 | + |
| 67 | +- [ ] All tests pass (`bun run test`) |
| 68 | +- [ ] No broken tests |
| 69 | +- [ ] No skipped tests (unless intentionally) |
| 70 | +- [ ] Type checking passes (`bun x tsc -p ./`) |
| 71 | +- [ ] Linting passes (`bun run lint`) |
| 72 | + |
| 73 | +### CI/CD Validation |
| 74 | + |
| 75 | +The CI pipeline automatically: |
| 76 | + |
| 77 | +1. Runs all tests on Ubuntu, macOS, and Windows |
| 78 | +2. Generates coverage reports |
| 79 | +3. Verifies all tests pass |
| 80 | +4. Fails the build if any tests fail |
| 81 | + |
| 82 | +## Fixing Failed Tests |
| 83 | + |
| 84 | +### When a Test Fails |
| 85 | + |
| 86 | +1. **Don't commit the failure** - Fix the test or the code |
| 87 | +2. **Run locally first** - Verify fix works before pushing |
| 88 | +3. **Check all platforms** - Ensure fix works on Linux/Windows (case sensitivity, etc.) |
| 89 | +4. **Update test if needed** - If behavior changed intentionally, update test |
| 90 | + |
| 91 | +### Common Issues |
| 92 | + |
| 93 | +- **Case sensitivity** - Use exact case for file references (`README.md` not `readme.md`) |
| 94 | +- **Mock issues** - Ensure mocks are properly reset in `beforeEach` |
| 95 | +- **Timing issues** - Avoid `async/await` in tests when possible, use static imports |
| 96 | + |
| 97 | +## Test Best Practices |
| 98 | + |
| 99 | +### 1. Use Descriptive Test Names |
| 100 | + |
| 101 | +```typescript |
| 102 | +// ✅ Good |
| 103 | +it('should extract ISO 8601 dates from JSON files', () => { |
| 104 | + // ... |
| 105 | +}); |
| 106 | + |
| 107 | +// ❌ Bad |
| 108 | +it('works', () => { |
| 109 | + // ... |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +### 2. Test One Thing Per Test |
| 114 | + |
| 115 | +```typescript |
| 116 | +// ✅ Good - separate tests |
| 117 | +it('should extract ISO dates', () => { /* ... */ }); |
| 118 | +it('should extract Unix timestamps', () => { /* ... */ }); |
| 119 | + |
| 120 | +// ❌ Bad - multiple concerns |
| 121 | +it('should extract ISO dates and Unix timestamps', () => { /* ... */ }); |
| 122 | +``` |
| 123 | + |
| 124 | +### 3. Use Arrange-Act-Assert Pattern |
| 125 | + |
| 126 | +```typescript |
| 127 | +it('should parse date from JSON', () => { |
| 128 | + // Arrange |
| 129 | + const json = '{"timestamp": "2025-01-15T10:30:00Z"}'; |
| 130 | + |
| 131 | + // Act |
| 132 | + const result = extractDates(json); |
| 133 | + |
| 134 | + // Assert |
| 135 | + expect(result.dates).toContainEqual({ |
| 136 | + value: '2025-01-15T10:30:00Z', |
| 137 | + format: 'ISO8601', |
| 138 | + }); |
| 139 | +}); |
| 140 | +``` |
| 141 | + |
| 142 | +### 4. Clean Up Mocks |
| 143 | + |
| 144 | +```typescript |
| 145 | +beforeEach(() => { |
| 146 | + vi.clearAllMocks(); |
| 147 | + // Reset mocks to default state |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +## Cross-Platform Testing |
| 152 | + |
| 153 | +### Case Sensitivity |
| 154 | + |
| 155 | +Always use exact case for file references: |
| 156 | + |
| 157 | +```typescript |
| 158 | +// ✅ Good - works on all platforms |
| 159 | +const content = readSampleFile('README.md'); |
| 160 | + |
| 161 | +// ❌ Bad - fails on Linux |
| 162 | +const content = readSampleFile('readme.md'); |
| 163 | +``` |
| 164 | + |
| 165 | +### Path Separators |
| 166 | + |
| 167 | +Use platform-agnostic path handling: |
| 168 | + |
| 169 | +```typescript |
| 170 | +import { join } from 'path'; |
| 171 | +const filePath = join(SAMPLE_DIR, filename); |
| 172 | +``` |
| 173 | + |
| 174 | +## Coverage Reports |
| 175 | + |
| 176 | +Coverage reports are generated automatically: |
| 177 | + |
| 178 | +- **Location**: `coverage/index.html` |
| 179 | +- **Format**: HTML, LCOV, JSON |
| 180 | +- **CI/CD**: Coverage uploaded as artifact |
| 181 | + |
| 182 | +## Continuous Integration |
| 183 | + |
| 184 | +### GitHub Actions |
| 185 | + |
| 186 | +Tests run automatically on: |
| 187 | + |
| 188 | +- **Ubuntu** (latest) |
| 189 | +- **macOS** (latest) |
| 190 | +- **Windows** (latest) |
| 191 | + |
| 192 | +All platforms must pass for the build to succeed. |
| 193 | + |
| 194 | +### Pre-commit Hooks |
| 195 | + |
| 196 | +Consider setting up pre-commit hooks to run tests before commits: |
| 197 | + |
| 198 | +```bash |
| 199 | +# Install husky (if needed) |
| 200 | +bun add -d husky |
| 201 | + |
| 202 | +# Add pre-commit hook |
| 203 | +echo "bun run test" > .husky/pre-commit |
| 204 | +``` |
| 205 | + |
| 206 | +## Reporting Test Issues |
| 207 | + |
| 208 | +If you encounter test failures: |
| 209 | + |
| 210 | +1. **Run locally** - Verify it fails consistently |
| 211 | +2. **Check CI logs** - See platform-specific errors |
| 212 | +3. **Reproduce** - Document steps to reproduce |
| 213 | +4. **Fix or report** - Either fix or create an issue |
| 214 | + |
| 215 | +## Related Documentation |
| 216 | + |
| 217 | +- [Performance Monitoring](PERFORMANCE.md) - Performance testing and benchmarks |
| 218 | +- [Architecture](ARCHITECTURE.md) - Code structure |
| 219 | +- [Commands](COMMANDS.md) - Command testing guidelines |
| 220 | + |
0 commit comments