Skip to content

Commit aa44b04

Browse files
Improve unit testing guidelines with detailed examples and clarifications
- Break up test naming examples into pairs with explanations - Add example for missing/unclear test descriptions - Clarify error-related test naming (be specific about error types) - Update snapshot test naming to 'matches rendered snapshot' - Remove vague terms like 'gracefully' and 'handles' from examples - Enhance guidance to avoid weasel words and subjective language
1 parent be67124 commit aa44b04

File tree

1 file changed

+94
-12
lines changed

1 file changed

+94
-12
lines changed

docs/testing/unit-testing.md

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,54 +119,136 @@ As each test [has to focus on a single aspect of that behavior](#keep-tests-focu
119119

120120
### Examples
121121

122-
🚫 Don't
122+
🚫 **Using "should" unnecessarily**
123123

124124
```typescript
125125
it('should successfully add token when address is valid and decimals are set and symbol exists', () => {
126126
// ...
127127
});
128+
```
129+
130+
**Describe the behavior directly**
131+
132+
```typescript
133+
it('stores valid token in state', () => {
134+
// ...
135+
});
136+
```
137+
138+
---
128139

140+
🚫 **Listing implementation details and parameters**
141+
142+
```typescript
129143
it('should fail and show error message when invalid address is provided', () => {
130144
// ...
131145
});
146+
```
147+
148+
**Focus on what is being tested**
132149

150+
```typescript
151+
it('displays invalid address error', () => {
152+
// ...
153+
});
154+
```
155+
156+
---
157+
158+
🚫 **Stating obvious successful outcomes**
159+
160+
```typescript
133161
it('works correctly when processing the transaction', () => {
134162
// ...
135163
});
164+
```
136165

137-
it('should throw error when balance is insufficient and user tries to send tokens', () => {
166+
**Be specific about the behavior**
167+
168+
```typescript
169+
it('processes transaction', () => {
138170
// ...
139171
});
140172
```
141173

142-
✅ Do
174+
---
175+
176+
🚫 **Describing implementation instead of behavior**
143177

144178
```typescript
145-
it('stores valid token in state', () => {
179+
it('calls redirectTo("/login") when session expires', () => {
146180
// ...
147181
});
182+
```
148183

149-
it('displays invalid address error', () => {
184+
**Describe the expected outcome**
185+
186+
```typescript
187+
it('redirects to login when session expires', () => {
150188
// ...
151189
});
190+
```
152191

153-
it('processes transaction', () => {
192+
---
193+
194+
🚫 **Using vague error language**
195+
196+
```typescript
197+
it('throws an error when balance is insufficient', () => {
154198
// ...
155199
});
200+
```
201+
202+
**Be precise about the expected behavior**
156203

204+
```typescript
157205
it('prevents sending with insufficient balance', () => {
158206
// ...
159207
});
160208
```
161209

210+
Or, when the specific error type is the key behavior:
211+
212+
```typescript
213+
it('throws InvalidPayloadError on malformed request', () => {
214+
// ...
215+
});
216+
```
217+
218+
---
219+
220+
🚫 **Missing or unclear description**
221+
222+
```typescript
223+
it('test', () => {
224+
// ...
225+
});
226+
227+
it('edge case', () => {
228+
// ...
229+
});
230+
```
231+
232+
**Clear, descriptive names**
233+
234+
```typescript
235+
it('returns empty array when input is empty', () => {
236+
// ...
237+
});
238+
239+
it('accepts transaction up to maximum amount limit', () => {
240+
// ...
241+
});
242+
```
243+
162244
The test description should communicate the expected behavior clearly and directly. Avoid:
163245

164-
- Repeating the name of the function or method being tested
246+
- Repeating the name of the function or method being tested that is already in the "describe" section.
165247
- Using "should" at the beginning of the test name
166-
- Including implementation details in the name
167-
- Stating obvious successful outcomes
248+
- Including implementation details (like function calls or internal methods)
249+
- Stating obvious successful outcomes ("works correctly", "successfully" or "gracefully")
168250
- Listing test parameters instead of what they represent
169-
- Using words like "fail", "error", or "throw" when the error is the expected behavior
251+
- Using vague terms like "fail", "error", or "throw" without being specific about the exact behavior (error type)
170252

171253
### Read more
172254

@@ -1102,12 +1184,12 @@ describe('MyComponent', () => {
11021184
11031185
```ts
11041186
describe('MyComponent', () => {
1105-
it('Matches rendered snapshot')
1187+
it('matches rendered snapshot')
11061188
```
11071189
11081190
Of course variants of this naming can be used to add some context, for instance:
11091191
11101192
```ts
11111193
describe('MyComponent', () => {
1112-
it('render matches snapshot when not enabled'
1194+
it('matches rendered snapshot when not enabled')
11131195
```

0 commit comments

Comments
 (0)