You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Use `it` to specify the desired behavior for the code under test
109
109
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:
111
111
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
115
119
116
120
### Examples
117
121
118
-
🚫
119
-
122
+
🚫 Don't
120
123
```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', () => {
122
125
// ...
123
126
});
124
-
```
125
-
126
-
✅
127
127
128
-
```typescript
129
-
it('does not stop the block tracker', () => {
128
+
it('should fail and show error message when invalid address is provided', () => {
130
129
// ...
131
130
});
132
-
```
133
131
134
-
🚫
132
+
it('works correctly when processing the transaction', () => {
133
+
// ...
134
+
});
135
135
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
+
// ...
141
138
});
142
139
```
143
140
144
-
🚫
145
-
141
+
✅ Do
146
142
```typescript
147
-
describe('TokensController', () => {
148
-
it('adds a token', () => {
149
-
// ...
150
-
});
143
+
it('stores valid token in state', () => {
144
+
// ...
151
145
});
152
-
```
153
146
154
-
✅
147
+
it('displays invalid address error', () => {
148
+
// ...
149
+
});
155
150
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
+
// ...
163
157
});
164
158
```
165
159
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
+
166
168
### Read more
167
169
168
170
-["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