Skip to content

Commit d4bb6cb

Browse files
update unit test naming guidelines
1 parent 3c07983 commit d4bb6cb

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

docs/testing/unit-testing.md

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -107,62 +107,67 @@ describe('when adding a token', () => {
107107

108108
## Use `it` to specify the desired behavior for the code under test
109109

110-
As each test [should focus on a single aspect of that behavior](#keep-tests-focused), its description should describe that behavior. This description helps to anchor the purpose of the test, understand the intended behavior, and debug differences with the actual behavior that may occur down the road.
110+
As each test [has to focus on a single aspect of that behavior](#keep-tests-focused), its description must describe that behavior using these guidelines:
111111

112-
Do not repeat the name of the function or method in the name of the test.
113-
114-
Do not use "should" at the beginning of the test name. The official Jest documentation [omits this word from their examples](https://jestjs.io/docs/next/getting-started), and it creates noise when reviewing the list of tests printed after a run.
112+
1. Start with an active verb in present tense
113+
2. Focus on what is being tested, not how
114+
3. Be explicit about the context when needed
115+
4. Keep names concise but descriptive
116+
5. Avoid "should", "when", and other unnecessary words
117+
6. Do not state obvious successful outcomes ("works successfully", "correctly")
118+
7. Do not list test parameters; describe what they represent instead
115119

116120
### Examples
117121

118-
🚫
122+
🚫 Don't
119123

120124
```typescript
121-
it('should not stop the block tracker', () => {
125+
it('should successfully add token when address is valid and decimals are set and symbol exists', () => {
122126
// ...
123127
});
124-
```
125-
126-
127128

128-
```typescript
129-
it('does not stop the block tracker', () => {
129+
it('should fail and show error message when invalid address is provided', () => {
130130
// ...
131131
});
132-
```
133132

134-
🚫
133+
it('works correctly when processing the transaction', () => {
134+
// ...
135+
});
135136

136-
```typescript
137-
describe('TokensController', () => {
138-
it('addToken', () => {
139-
// ...
140-
});
137+
it('should throw error when balance is insufficient and user tries to send tokens', () => {
138+
// ...
141139
});
142140
```
143141

144-
🚫
142+
✅ Do
145143

146144
```typescript
147-
describe('TokensController', () => {
148-
it('adds a token', () => {
149-
// ...
150-
});
145+
it('stores valid token in state', () => {
146+
// ...
151147
});
152-
```
153148

154-
149+
it('displays invalid address error', () => {
150+
// ...
151+
});
155152

156-
```typescript
157-
describe('TokensController', () => {
158-
describe('addToken', () => {
159-
it('adds the given token to "tokens" in state', () => {
160-
// ...
161-
});
162-
});
153+
it('processes transaction', () => {
154+
// ...
155+
});
156+
157+
it('prevents sending with insufficient balance', () => {
158+
// ...
163159
});
164160
```
165161

162+
The test description should communicate the expected behavior clearly and directly. Avoid:
163+
164+
- Repeating the name of the function or method being tested
165+
- Using "should" at the beginning of the test name
166+
- Including implementation details in the name
167+
- Stating obvious successful outcomes
168+
- Listing test parameters instead of what they represent
169+
- Using words like "fail", "error", or "throw" when the error is the expected behavior
170+
166171
### Read more
167172

168173
- ["Tests as Specification"](http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Tests%20as%20Specification) and ["Tests as Documentation"](http://xunitpatterns.com/Goals%20of%20Test%20Automation.html#Tests%20as%20Documentation) in xUnit Patterns

0 commit comments

Comments
 (0)