Skip to content

Commit 36073aa

Browse files
committed
Add before each and after each hooks documentation
1 parent f70eb44 commit 36073aa

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,86 @@ it('counts multiple assertions too', async () => {
140140
});
141141
```
142142

143+
**beforeEach and afterEach blocks**
144+
145+
If you have expectations inside either of `beforeEach` or `afterEach` blocks for your test then these expects will be
146+
included in the count - even if you have nested describe blocks each with their own `beforeEach`/`afterEach` the count
147+
will accumulate.
148+
149+
```js
150+
beforeEach(() => {
151+
expect(true).toBe(true);
152+
});
153+
154+
afterEach(() => {
155+
expect(true).toBe(true);
156+
});
157+
158+
describe('.add', () => {
159+
beforeEach(() => {
160+
expect(true).toBe(true);
161+
});
162+
afterEach(() => {
163+
expect(true).toBe(true);
164+
});
165+
it('returns 1 when given 0 and 1', () => {
166+
expect(add(1, 0)).toEqual(1);
167+
});
168+
169+
describe('.add2', () => {
170+
beforeEach(() => {
171+
expect(true).toBe(true);
172+
});
173+
afterEach(() => {
174+
expect(true).toBe(true);
175+
});
176+
it('returns 1 when given 0 and 1', () => {
177+
expect(add2(1, 0)).toEqual(1);
178+
});
179+
});
180+
});
181+
182+
↓ ↓ ↓ ↓ ↓ ↓
183+
184+
beforeEach(() => {
185+
expect(true).toBe(true);
186+
});
187+
188+
afterEach(() => {
189+
expect(true).toBe(true);
190+
});
191+
192+
describe('.add', () => {
193+
beforeEach(() => {
194+
expect(true).toBe(true);
195+
});
196+
afterEach(() => {
197+
expect(true).toBe(true);
198+
});
199+
it('returns 1 when given 0 and 1', () => {
200+
expect.assertions(5);
201+
expect.hasAssertions();
202+
203+
expect(add2(1, 0)).toEqual(1);
204+
});
205+
206+
describe('.add2', () => {
207+
beforeEach(() => {
208+
expect(true).toBe(true);
209+
});
210+
afterEach(() => {
211+
expect(true).toBe(true);
212+
});
213+
it('returns 1 when given 0 and 1', () => {
214+
expect.assertions(7);
215+
expect.hasAssertions();
216+
217+
expect(add2(1, 0)).toEqual(1);
218+
});
219+
});
220+
});
221+
```
222+
143223
**Comments are ignored**
144224
```js
145225
it('ignores commented-out assertions', async () => {

0 commit comments

Comments
 (0)