Skip to content

Commit d4721f0

Browse files
talissoncostaclaude
andcommitted
feat(frontend): add /unit-test command for generating tests
- Add Claude command to analyse files and generate unit tests - Include example test for isFreeEmailDomain utility - Follow existing test patterns (Jest, it.each, edge cases) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 4213b50 commit d4721f0

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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`
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import isFreeEmailDomain from 'common/utils/isFreeEmailDomain'
2+
3+
describe('isFreeEmailDomain', () => {
4+
it.each`
5+
input | expected
6+
${'user@gmail.com'} | ${true}
7+
${'user@yahoo.com'} | ${true}
8+
${'user@hotmail.com'} | ${true}
9+
${'user@outlook.com'} | ${true}
10+
${'user@company.com'} | ${false}
11+
${'user@flagsmith.com'} | ${false}
12+
${'user@enterprise.co.uk'} | ${false}
13+
${null} | ${false}
14+
${undefined} | ${false}
15+
${''} | ${false}
16+
${'invalid-email'} | ${false}
17+
${'@gmail.com'} | ${true}
18+
`('isFreeEmailDomain($input) returns $expected', ({ expected, input }) => {
19+
expect(isFreeEmailDomain(input)).toBe(expected)
20+
})
21+
})

0 commit comments

Comments
 (0)