|
| 1 | +--- |
| 2 | +description: Analyse a frontend file and generate unit tests (Jest) |
| 3 | +--- |
| 4 | + |
| 5 | +Generate unit tests for the frontend file at `$ARGUMENTS`. |
| 6 | + |
| 7 | +**Note:** This command is for frontend code only. For backend (Python) tests, see `../api/README.md`. |
| 8 | + |
| 9 | +## Steps |
| 10 | + |
| 11 | +1. **Check for existing tests** |
| 12 | + - Look for `__tests__/{filename}.test.ts` in the same directory |
| 13 | + - If tests exist, analyse them for coverage gaps |
| 14 | + |
| 15 | +2. **Read and analyse the source file** |
| 16 | + - Identify all exported functions, classes, and constants |
| 17 | + - Note dependencies and imports |
| 18 | + - Determine testability: |
| 19 | + - Pure functions (no side effects) → highly testable |
| 20 | + - React components → may need mocking |
| 21 | + - Functions with external dependencies → note what needs mocking |
| 22 | + |
| 23 | +3. **Generate test file** |
| 24 | + - Follow the pattern in `common/utils/__tests__/format.test.ts` |
| 25 | + - Location: `{sourceDir}/__tests__/{filename}.test.ts` |
| 26 | + - Use path aliases for imports (`common/...`, `components/...`) |
| 27 | + |
| 28 | +4. **Test structure requirements** |
| 29 | + - Use `describe` block for each exported function |
| 30 | + - Use `it.each` for table-driven tests when function has multiple input/output cases |
| 31 | + - Include edge cases: `null`, `undefined`, empty strings, empty arrays |
| 32 | + - Include boundary cases where applicable |
| 33 | + |
| 34 | +5. **After generating, run the tests** |
| 35 | + ```bash |
| 36 | + npm run test:unit -- --testPathPatterns={filename} |
| 37 | + ``` |
| 38 | + |
| 39 | +## Test file pattern |
| 40 | + |
| 41 | +```typescript |
| 42 | +import { functionName } from 'common/path/to/file' |
| 43 | + |
| 44 | +describe('functionName', () => { |
| 45 | + it.each` |
| 46 | + input | expected |
| 47 | + ${value1} | ${result1} |
| 48 | + ${value2} | ${result2} |
| 49 | + ${null} | ${expectedForNull} |
| 50 | + ${undefined} | ${expectedForUndefined} |
| 51 | + `('functionName($input) returns $expected', ({ input, expected }) => { |
| 52 | + expect(functionName(input)).toBe(expected) |
| 53 | + }) |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +## Reference |
| 58 | + |
| 59 | +See existing tests at: |
| 60 | +- `common/utils/__tests__/format.test.ts` |
| 61 | +- `common/utils/__tests__/featureFilterParams.test.ts` |
0 commit comments