Skip to content

Commit a1d2628

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

File tree

4 files changed

+429
-46
lines changed

4 files changed

+429
-46
lines changed

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

Lines changed: 177 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,92 @@
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+
> afterEach value: <yellow>1</color>
27+
hook_success: afterEach
28+
test_done: test
29+
run_describe_finish: describe
30+
run_describe_start: describe with beforeAll
31+
hook_start: beforeAll
32+
> beforeAll value: <yellow>3</color>
33+
hook_success: beforeAll
34+
test_start: test
35+
test_started: test
36+
test_fn_start: test
37+
> test value: <yellow>3</color>
38+
test_fn_success: test
39+
test_done: test
40+
hook_start: afterAll
41+
> afterAll value: <yellow>3</color>
42+
hook_success: afterAll
43+
run_describe_finish: describe with beforeAll
44+
run_describe_finish: ROOT_DESCRIBE_BLOCK
45+
run_finish
46+
47+
unhandledErrors: 0"
48+
`;
49+
50+
exports[`beforeAll can return a cleanup function, run in the correct order relative to afterAll hooks 1`] = `
51+
"start_describe_definition: describe
52+
add_hook: afterAll
53+
add_hook: beforeAll
54+
add_hook: afterAll
55+
add_hook: beforeAll
56+
add_hook: afterAll
57+
add_test: test
58+
finish_describe_definition: describe
59+
run_start
60+
run_describe_start: ROOT_DESCRIBE_BLOCK
61+
run_describe_start: describe
62+
hook_start: beforeAll
63+
> beforeAll 1
64+
hook_success: beforeAll
65+
hook_start: beforeAll
66+
> beforeAll 2
67+
hook_success: beforeAll
68+
test_start: test
69+
test_started: test
70+
test_fn_start: test
71+
> test
72+
test_fn_success: test
73+
test_done: test
74+
hook_start: afterAll
75+
> afterAll 1 (defined first)
76+
hook_success: afterAll
77+
hook_start: afterAll
78+
> afterAll 2
79+
hook_success: afterAll
80+
hook_start: afterAll
81+
> afterAll 3 (defined last)
82+
hook_success: afterAll
83+
run_describe_finish: describe
84+
run_describe_finish: ROOT_DESCRIBE_BLOCK
85+
run_finish
86+
87+
unhandledErrors: 0"
88+
`;
89+
390
exports[`beforeAll is exectued correctly 1`] = `
491
"start_describe_definition: describe 1
592
add_hook: beforeAll
@@ -46,6 +133,46 @@ run_finish
46133
unhandledErrors: 0"
47134
`;
48135

136+
exports[`beforeEach can return a cleanup function, run in the correct order relative to afterEach hooks 1`] = `
137+
"start_describe_definition: describe
138+
add_hook: afterEach
139+
add_hook: beforeEach
140+
add_hook: afterEach
141+
add_hook: beforeEach
142+
add_hook: afterEach
143+
add_test: test
144+
finish_describe_definition: describe
145+
run_start
146+
run_describe_start: ROOT_DESCRIBE_BLOCK
147+
run_describe_start: describe
148+
test_start: test
149+
test_started: test
150+
hook_start: beforeEach
151+
> beforeEach 2
152+
hook_success: beforeEach
153+
hook_start: beforeEach
154+
> beforeEach 1
155+
hook_success: beforeEach
156+
test_fn_start: test
157+
> test
158+
test_fn_success: test
159+
hook_start: afterEach
160+
> afterEach 3 (defined last)
161+
hook_success: afterEach
162+
hook_start: afterEach
163+
> afterEach 2
164+
hook_success: afterEach
165+
hook_start: afterEach
166+
> afterEach 1 (defined first)
167+
hook_success: afterEach
168+
test_done: test
169+
run_describe_finish: describe
170+
run_describe_finish: ROOT_DESCRIBE_BLOCK
171+
run_finish
172+
173+
unhandledErrors: 0"
174+
`;
175+
49176
exports[`beforeEach is executed before each test in current/child describe blocks 1`] = `
50177
"start_describe_definition: describe
51178
add_hook: beforeEach
@@ -136,6 +263,54 @@ run_finish
136263
unhandledErrors: 0"
137264
`;
138265

266+
exports[`cleanup functions run in the correct order with nested describes 1`] = `
267+
"start_describe_definition: outer describe
268+
add_hook: afterAll
269+
add_hook: beforeAll
270+
add_hook: afterAll
271+
start_describe_definition: inner describe
272+
add_hook: afterAll
273+
add_hook: beforeAll
274+
add_hook: afterAll
275+
add_test: test
276+
finish_describe_definition: inner describe
277+
finish_describe_definition: outer describe
278+
run_start
279+
run_describe_start: ROOT_DESCRIBE_BLOCK
280+
run_describe_start: outer describe
281+
hook_start: beforeAll
282+
> outer beforeAll
283+
hook_success: beforeAll
284+
run_describe_start: inner describe
285+
hook_start: beforeAll
286+
> inner beforeAll
287+
hook_success: beforeAll
288+
test_start: test
289+
test_started: test
290+
test_fn_start: test
291+
> test
292+
test_fn_success: test
293+
test_done: test
294+
hook_start: afterAll
295+
> inner afterAll 1 (defined first)
296+
hook_success: afterAll
297+
hook_start: afterAll
298+
> inner afterAll 2 (defined last)
299+
hook_success: afterAll
300+
run_describe_finish: inner describe
301+
hook_start: afterAll
302+
> outer afterAll 1 (defined first)
303+
hook_success: afterAll
304+
hook_start: afterAll
305+
> outer afterAll 2 (defined last)
306+
hook_success: afterAll
307+
run_describe_finish: outer describe
308+
run_describe_finish: ROOT_DESCRIBE_BLOCK
309+
run_finish
310+
311+
unhandledErrors: 0"
312+
`;
313+
139314
exports[`multiple before each hooks in one describe are executed in the right order 1`] = `
140315
"start_describe_definition: describe 1
141316
add_hook: beforeEach
@@ -151,10 +326,10 @@ run_describe_start: 2nd level describe
151326
test_start: test
152327
test_started: test
153328
hook_start: beforeEach
154-
before each 1
329+
before each 2
155330
hook_success: beforeEach
156331
hook_start: beforeEach
157-
before each 2
332+
before each 1
158333
hook_success: beforeEach
159334
test_fn_start: test
160335
test_fn_success: test

packages/jest-circus/src/__tests__/hooks.test.ts

Lines changed: 168 additions & 0 deletions
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)