Skip to content

Commit 9caa32f

Browse files
committed
feat: support beforeEach and beforeAll returning cleanup functions
1 parent 93ca9d3 commit 9caa32f

File tree

6 files changed

+472
-48
lines changed

6 files changed

+472
-48
lines changed

Diff for: packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap

+199
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,104 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`async cleanup functions are properly awaited 1`] = `
4+
"start_describe_definition: describe
5+
add_hook: beforeEach
6+
add_hook: afterEach
7+
add_test: test
8+
finish_describe_definition: describe
9+
start_describe_definition: describe with beforeAll
10+
add_hook: beforeAll
11+
add_hook: afterAll
12+
add_test: test
13+
finish_describe_definition: describe with beforeAll
14+
run_start
15+
run_describe_start: ROOT_DESCRIBE_BLOCK
16+
run_describe_start: describe
17+
test_start: test
18+
test_started: test
19+
hook_start: beforeEach
20+
> beforeEach value: <yellow>1</color>
21+
hook_success: beforeEach
22+
test_fn_start: test
23+
> test value: <yellow>1</color>
24+
test_fn_success: test
25+
hook_start: afterEach
26+
> cleanup value: <yellow>0</color>
27+
hook_success: afterEach
28+
hook_start: afterEach
29+
> afterEach value: <yellow>0</color>
30+
hook_success: afterEach
31+
test_done: test
32+
run_describe_finish: describe
33+
run_describe_start: describe with beforeAll
34+
hook_start: beforeAll
35+
> beforeAll value: <yellow>2</color>
36+
hook_success: beforeAll
37+
test_start: test
38+
test_started: test
39+
test_fn_start: test
40+
> test value: <yellow>2</color>
41+
test_fn_success: test
42+
test_done: test
43+
hook_start: afterAll
44+
> cleanup value: <yellow>0</color>
45+
hook_success: afterAll
46+
hook_start: afterAll
47+
> afterAll value: <yellow>0</color>
48+
hook_success: afterAll
49+
run_describe_finish: describe with beforeAll
50+
run_describe_finish: ROOT_DESCRIBE_BLOCK
51+
run_finish
52+
53+
unhandledErrors: 0"
54+
`;
55+
56+
exports[`beforeAll can return a cleanup function, run in the correct order relative to afterAll hooks 1`] = `
57+
"start_describe_definition: describe
58+
add_hook: afterAll
59+
add_hook: beforeAll
60+
add_hook: afterAll
61+
add_hook: beforeAll
62+
add_hook: afterAll
63+
add_test: test
64+
finish_describe_definition: describe
65+
run_start
66+
run_describe_start: ROOT_DESCRIBE_BLOCK
67+
run_describe_start: describe
68+
hook_start: beforeAll
69+
> beforeAll 1
70+
hook_success: beforeAll
71+
hook_start: beforeAll
72+
> beforeAll 2
73+
hook_success: beforeAll
74+
test_start: test
75+
test_started: test
76+
test_fn_start: test
77+
> test
78+
test_fn_success: test
79+
test_done: test
80+
hook_start: afterAll
81+
> afterAll 1 (defined first)
82+
hook_success: afterAll
83+
hook_start: afterAll
84+
> cleanup 1
85+
hook_success: afterAll
86+
hook_start: afterAll
87+
> afterAll 2
88+
hook_success: afterAll
89+
hook_start: afterAll
90+
> cleanup 2
91+
hook_success: afterAll
92+
hook_start: afterAll
93+
> afterAll 3 (defined last)
94+
hook_success: afterAll
95+
run_describe_finish: describe
96+
run_describe_finish: ROOT_DESCRIBE_BLOCK
97+
run_finish
98+
99+
unhandledErrors: 0"
100+
`;
101+
3102
exports[`beforeAll is exectued correctly 1`] = `
4103
"start_describe_definition: describe 1
5104
add_hook: beforeAll
@@ -46,6 +145,52 @@ run_finish
46145
unhandledErrors: 0"
47146
`;
48147

148+
exports[`beforeEach can return a cleanup function, run in the correct order relative to afterEach hooks 1`] = `
149+
"start_describe_definition: describe
150+
add_hook: afterEach
151+
add_hook: beforeEach
152+
add_hook: afterEach
153+
add_hook: beforeEach
154+
add_hook: afterEach
155+
add_test: test
156+
finish_describe_definition: describe
157+
run_start
158+
run_describe_start: ROOT_DESCRIBE_BLOCK
159+
run_describe_start: describe
160+
test_start: test
161+
test_started: test
162+
hook_start: beforeEach
163+
> beforeEach 1
164+
hook_success: beforeEach
165+
hook_start: beforeEach
166+
> beforeEach 2
167+
hook_success: beforeEach
168+
test_fn_start: test
169+
> test
170+
test_fn_success: test
171+
hook_start: afterEach
172+
> afterEach 1 (defined first)
173+
hook_success: afterEach
174+
hook_start: afterEach
175+
> cleanup 1
176+
hook_success: afterEach
177+
hook_start: afterEach
178+
> afterEach 2
179+
hook_success: afterEach
180+
hook_start: afterEach
181+
> cleanup 2
182+
hook_success: afterEach
183+
hook_start: afterEach
184+
> afterEach 3 (defined last)
185+
hook_success: afterEach
186+
test_done: test
187+
run_describe_finish: describe
188+
run_describe_finish: ROOT_DESCRIBE_BLOCK
189+
run_finish
190+
191+
unhandledErrors: 0"
192+
`;
193+
49194
exports[`beforeEach is executed before each test in current/child describe blocks 1`] = `
50195
"start_describe_definition: describe
51196
add_hook: beforeEach
@@ -136,6 +281,60 @@ run_finish
136281
unhandledErrors: 0"
137282
`;
138283

284+
exports[`cleanup functions run in the correct order with nested describes 1`] = `
285+
"start_describe_definition: outer describe
286+
add_hook: afterAll
287+
add_hook: beforeAll
288+
add_hook: afterAll
289+
start_describe_definition: inner describe
290+
add_hook: afterAll
291+
add_hook: beforeAll
292+
add_hook: afterAll
293+
add_test: test
294+
finish_describe_definition: inner describe
295+
finish_describe_definition: outer describe
296+
run_start
297+
run_describe_start: ROOT_DESCRIBE_BLOCK
298+
run_describe_start: outer describe
299+
hook_start: beforeAll
300+
> outer beforeAll
301+
hook_success: beforeAll
302+
run_describe_start: inner describe
303+
hook_start: beforeAll
304+
> inner beforeAll
305+
hook_success: beforeAll
306+
test_start: test
307+
test_started: test
308+
test_fn_start: test
309+
> test
310+
test_fn_success: test
311+
test_done: test
312+
hook_start: afterAll
313+
> inner afterAll 1 (defined first)
314+
hook_success: afterAll
315+
hook_start: afterAll
316+
> inner cleanup
317+
hook_success: afterAll
318+
hook_start: afterAll
319+
> inner afterAll 2 (defined last)
320+
hook_success: afterAll
321+
run_describe_finish: inner describe
322+
hook_start: afterAll
323+
> outer afterAll 1 (defined first)
324+
hook_success: afterAll
325+
hook_start: afterAll
326+
> outer cleanup
327+
hook_success: afterAll
328+
hook_start: afterAll
329+
> outer afterAll 2 (defined last)
330+
hook_success: afterAll
331+
run_describe_finish: outer describe
332+
run_describe_finish: ROOT_DESCRIBE_BLOCK
333+
run_finish
334+
335+
unhandledErrors: 0"
336+
`;
337+
139338
exports[`multiple before each hooks in one describe are executed in the right order 1`] = `
140339
"start_describe_definition: describe 1
141340
add_hook: beforeEach

Diff for: packages/jest-circus/src/__tests__/hooks.test.ts

+168
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,45 @@ test('multiple before each hooks in one describe are executed in the right order
5555
expect(stdout).toMatchSnapshot();
5656
});
5757

58+
test('beforeEach can return a cleanup function, run in the correct order relative to afterEach hooks', () => {
59+
const {stdout} = runTest(`
60+
describe('describe', () => {
61+
afterEach(() => {
62+
console.log('> afterEach 1 (defined first)');
63+
});
64+
65+
beforeEach(() => {
66+
console.log('> beforeEach 1');
67+
return () => {
68+
console.log('> cleanup 1');
69+
};
70+
});
71+
72+
afterEach(() => {
73+
console.log('> afterEach 2');
74+
});
75+
76+
beforeEach(() => {
77+
console.log('> beforeEach 2');
78+
return () => {
79+
console.log('> cleanup 2');
80+
};
81+
});
82+
83+
afterEach(() => {
84+
console.log('> afterEach 3 (defined last)');
85+
});
86+
87+
test('test', () => {
88+
console.log('> test');
89+
});
90+
});
91+
`);
92+
93+
expect(stdout).toMatchSnapshot();
94+
});
95+
96+
5897
test('beforeAll is exectued correctly', () => {
5998
const {stdout} = runTest(`
6099
describe('describe 1', () => {
@@ -71,3 +110,132 @@ test('beforeAll is exectued correctly', () => {
71110

72111
expect(stdout).toMatchSnapshot();
73112
});
113+
114+
test('beforeAll can return a cleanup function, run in the correct order relative to afterAll hooks', () => {
115+
const {stdout} = runTest(`
116+
describe('describe', () => {
117+
afterAll(() => {
118+
console.log('> afterAll 1 (defined first)');
119+
});
120+
121+
beforeAll(() => {
122+
console.log('> beforeAll 1');
123+
return () => {
124+
console.log('> cleanup 1');
125+
};
126+
});
127+
128+
afterAll(() => {
129+
console.log('> afterAll 2');
130+
});
131+
132+
beforeAll(() => {
133+
console.log('> beforeAll 2');
134+
return () => {
135+
console.log('> cleanup 2');
136+
};
137+
});
138+
139+
afterAll(() => {
140+
console.log('> afterAll 3 (defined last)');
141+
});
142+
143+
test('test', () => {
144+
console.log('> test');
145+
});
146+
});
147+
`);
148+
149+
expect(stdout).toMatchSnapshot();
150+
});
151+
152+
test('cleanup functions run in the correct order with nested describes', () => {
153+
const {stdout} = runTest(`
154+
describe('outer describe', () => {
155+
afterAll(() => {
156+
console.log('> outer afterAll 1 (defined first)');
157+
});
158+
159+
beforeAll(() => {
160+
console.log('> outer beforeAll');
161+
return () => {
162+
console.log('> outer cleanup');
163+
};
164+
});
165+
166+
afterAll(() => {
167+
console.log('> outer afterAll 2 (defined last)');
168+
});
169+
170+
describe('inner describe', () => {
171+
afterAll(() => {
172+
console.log('> inner afterAll 1 (defined first)');
173+
});
174+
175+
beforeAll(() => {
176+
console.log('> inner beforeAll');
177+
return () => {
178+
console.log('> inner cleanup');
179+
};
180+
});
181+
182+
afterAll(() => {
183+
console.log('> inner afterAll 2 (defined last)');
184+
});
185+
186+
test('test', () => {
187+
console.log('> test');
188+
});
189+
});
190+
});
191+
`);
192+
193+
expect(stdout).toMatchSnapshot();
194+
});
195+
196+
test('async cleanup functions are properly awaited', () => {
197+
const {stdout} = runTest(`
198+
let value = 0;
199+
describe('describe', () => {
200+
beforeEach(() => {
201+
value += 1;
202+
console.log('> beforeEach value:', value);
203+
return async () => {
204+
await new Promise(resolve => setTimeout(resolve, 0));
205+
value -= 1;
206+
console.log('> cleanup value:', value);
207+
};
208+
});
209+
210+
afterEach(() => {
211+
console.log('> afterEach value:', value);
212+
});
213+
214+
test('test', () => {
215+
console.log('> test value:', value);
216+
});
217+
});
218+
219+
describe('describe with beforeAll', () => {
220+
beforeAll(() => {
221+
value += 2;
222+
console.log('> beforeAll value:', value);
223+
return async () => {
224+
await new Promise(resolve => setTimeout(resolve, 0));
225+
value -= 2;
226+
console.log('> cleanup value:', value);
227+
};
228+
});
229+
230+
afterAll(() => {
231+
console.log('> afterAll value:', value);
232+
});
233+
234+
test('test', () => {
235+
console.log('> test value:', value);
236+
});
237+
});
238+
`);
239+
240+
expect(stdout).toMatchSnapshot();
241+
});

0 commit comments

Comments
 (0)