Skip to content

Commit 64ba557

Browse files
authored
Merge pull request #508 from extolkom/docs/utils-testing-guide
docs: add testing guide for utility functions and coverage requirements
2 parents b758d28 + df97076 commit 64ba557

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

docs/utils-testing-guide.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Utils Testing Guide
2+
3+
## Naming Convention & Co-location
4+
5+
- Test files should be named **`<helperName>.test.ts`** (or `.test.tsx` for React‑related helpers).
6+
- Place the test file **side‑by‑side** with the helper it exercises, inside the same directory.
7+
8+
Example directory layout:
9+
```
10+
src/utils/
11+
├─ formatNumber.utils.ts
12+
└─ formatNumber.utils.test.ts ← test file
13+
```
14+
15+
## Running Only Util Tests
16+
17+
The project uses **Vitest** as the test runner (configured in `vitest.config.ts`).
18+
19+
- To run **all** tests: `pnpm test`
20+
- To run **only utils** tests:
21+
```bash
22+
pnpm test "src/utils/**/*.test.ts"
23+
```
24+
This pattern matches every test file under `src/utils`.
25+
26+
## Worked Example Test
27+
28+
Below is a simple example for a pure helper `formatNumber` that formats a number with commas and two decimal places.
29+
30+
```ts
31+
// src/utils/formatNumber.utils.test.ts
32+
import { describe, expect, it } from 'vitest';
33+
import { formatNumber } from './formatNumber.utils';
34+
35+
describe('formatNumber utils', () => {
36+
it('formats an integer with commas', () => {
37+
expect(formatNumber(1234567)).toBe('1,234,567.00');
38+
});
39+
40+
it('formats a floating‑point number with two decimals', () => {
41+
expect(formatNumber(1234.5)).toBe('1,234.50');
42+
});
43+
44+
it('handles negative numbers', () => {
45+
expect(formatNumber(-9876.543)).toBe('-9,876.54');
46+
});
47+
});
48+
```
49+
50+
### Explanation
51+
- **`describe`** groups related tests under a readable heading.
52+
- **`it`** defines individual test cases.
53+
- **`expect(...).toBe(...)`** performs the assertion.
54+
- Because `formatNumber` is a **pure function** (no side‑effects), we can achieve **100 % branch coverage** with the three cases above (positive, decimal, negative).
55+
56+
## Branch Coverage Expectation
57+
58+
- **Pure helpers** (functions that depend only on their inputs) must have **100 % branch coverage**.
59+
- Run the coverage report with:
60+
```bash
61+
pnpm test --coverage
62+
```
63+
- Ensure the generated `coverage` report shows `100%` for each pure helper file.
64+
65+
---
66+
*This guide lives in the repository under `docs/utils-testing-guide.md` and should be referenced by contributors when adding new utility helpers.*

0 commit comments

Comments
 (0)