Skip to content

Commit de1d430

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

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

docs/testing/unit-testing.md

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -107,62 +107,64 @@ 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-
🚫
119-
122+
🚫 Don't
120123
```typescript
121-
it('should not stop the block tracker', () => {
124+
it('should successfully add token when address is valid and decimals are set and symbol exists', () => {
122125
// ...
123126
});
124-
```
125-
126-
127127

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

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

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

144-
🚫
145-
141+
✅ Do
146142
```typescript
147-
describe('TokensController', () => {
148-
it('adds a token', () => {
149-
// ...
150-
});
143+
it('stores valid token in state', () => {
144+
// ...
151145
});
152-
```
153146

154-
147+
it('displays invalid address error', () => {
148+
// ...
149+
});
155150

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

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

168170
- ["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)