Skip to content

Commit

Permalink
feat: support beforeEach and beforeAll returning cleanup functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SevenOutman committed Feb 15, 2025
1 parent 93ca9d3 commit a1d2628
Show file tree
Hide file tree
Showing 4 changed files with 429 additions and 46 deletions.
179 changes: 177 additions & 2 deletions packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,92 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`async cleanup functions are properly awaited 1`] = `
"start_describe_definition: describe
add_hook: beforeEach
add_hook: afterEach
add_test: test
finish_describe_definition: describe
start_describe_definition: describe with beforeAll
add_hook: beforeAll
add_hook: afterAll
add_test: test
finish_describe_definition: describe with beforeAll
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: test
test_started: test
hook_start: beforeEach
> beforeEach value: <yellow>1</color>
hook_success: beforeEach
test_fn_start: test
> test value: <yellow>1</color>
test_fn_success: test
hook_start: afterEach
> afterEach value: <yellow>1</color>
hook_success: afterEach
test_done: test
run_describe_finish: describe
run_describe_start: describe with beforeAll
hook_start: beforeAll
> beforeAll value: <yellow>3</color>
hook_success: beforeAll
test_start: test
test_started: test
test_fn_start: test
> test value: <yellow>3</color>
test_fn_success: test
test_done: test
hook_start: afterAll
> afterAll value: <yellow>3</color>
hook_success: afterAll
run_describe_finish: describe with beforeAll
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0"
`;

exports[`beforeAll can return a cleanup function, run in the correct order relative to afterAll hooks 1`] = `
"start_describe_definition: describe
add_hook: afterAll
add_hook: beforeAll
add_hook: afterAll
add_hook: beforeAll
add_hook: afterAll
add_test: test
finish_describe_definition: describe
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
hook_start: beforeAll
> beforeAll 1
hook_success: beforeAll
hook_start: beforeAll
> beforeAll 2
hook_success: beforeAll
test_start: test
test_started: test
test_fn_start: test
> test
test_fn_success: test
test_done: test
hook_start: afterAll
> afterAll 1 (defined first)
hook_success: afterAll
hook_start: afterAll
> afterAll 2
hook_success: afterAll
hook_start: afterAll
> afterAll 3 (defined last)
hook_success: afterAll
run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0"
`;

exports[`beforeAll is exectued correctly 1`] = `
"start_describe_definition: describe 1
add_hook: beforeAll
Expand Down Expand Up @@ -46,6 +133,46 @@ run_finish
unhandledErrors: 0"
`;

exports[`beforeEach can return a cleanup function, run in the correct order relative to afterEach hooks 1`] = `
"start_describe_definition: describe
add_hook: afterEach
add_hook: beforeEach
add_hook: afterEach
add_hook: beforeEach
add_hook: afterEach
add_test: test
finish_describe_definition: describe
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: describe
test_start: test
test_started: test
hook_start: beforeEach
> beforeEach 2
hook_success: beforeEach
hook_start: beforeEach
> beforeEach 1
hook_success: beforeEach
test_fn_start: test
> test
test_fn_success: test
hook_start: afterEach
> afterEach 3 (defined last)
hook_success: afterEach
hook_start: afterEach
> afterEach 2
hook_success: afterEach
hook_start: afterEach
> afterEach 1 (defined first)
hook_success: afterEach
test_done: test
run_describe_finish: describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0"
`;

exports[`beforeEach is executed before each test in current/child describe blocks 1`] = `
"start_describe_definition: describe
add_hook: beforeEach
Expand Down Expand Up @@ -136,6 +263,54 @@ run_finish
unhandledErrors: 0"
`;

exports[`cleanup functions run in the correct order with nested describes 1`] = `
"start_describe_definition: outer describe
add_hook: afterAll
add_hook: beforeAll
add_hook: afterAll
start_describe_definition: inner describe
add_hook: afterAll
add_hook: beforeAll
add_hook: afterAll
add_test: test
finish_describe_definition: inner describe
finish_describe_definition: outer describe
run_start
run_describe_start: ROOT_DESCRIBE_BLOCK
run_describe_start: outer describe
hook_start: beforeAll
> outer beforeAll
hook_success: beforeAll
run_describe_start: inner describe
hook_start: beforeAll
> inner beforeAll
hook_success: beforeAll
test_start: test
test_started: test
test_fn_start: test
> test
test_fn_success: test
test_done: test
hook_start: afterAll
> inner afterAll 1 (defined first)
hook_success: afterAll
hook_start: afterAll
> inner afterAll 2 (defined last)
hook_success: afterAll
run_describe_finish: inner describe
hook_start: afterAll
> outer afterAll 1 (defined first)
hook_success: afterAll
hook_start: afterAll
> outer afterAll 2 (defined last)
hook_success: afterAll
run_describe_finish: outer describe
run_describe_finish: ROOT_DESCRIBE_BLOCK
run_finish
unhandledErrors: 0"
`;

exports[`multiple before each hooks in one describe are executed in the right order 1`] = `
"start_describe_definition: describe 1
add_hook: beforeEach
Expand All @@ -151,10 +326,10 @@ run_describe_start: 2nd level describe
test_start: test
test_started: test
hook_start: beforeEach
before each 1
before each 2
hook_success: beforeEach
hook_start: beforeEach
before each 2
before each 1
hook_success: beforeEach
test_fn_start: test
test_fn_success: test
Expand Down
168 changes: 168 additions & 0 deletions packages/jest-circus/src/__tests__/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,45 @@ test('multiple before each hooks in one describe are executed in the right order
expect(stdout).toMatchSnapshot();
});

test('beforeEach can return a cleanup function, run in the correct order relative to afterEach hooks', () => {
const {stdout} = runTest(`
describe('describe', () => {
afterEach(() => {
console.log('> afterEach 1 (defined first)');
});
beforeEach(() => {
console.log('> beforeEach 1');
return () => {
console.log('> cleanup 1');
};
});
afterEach(() => {
console.log('> afterEach 2');
});
beforeEach(() => {
console.log('> beforeEach 2');
return () => {
console.log('> cleanup 2');
};
});
afterEach(() => {
console.log('> afterEach 3 (defined last)');
});
test('test', () => {
console.log('> test');
});
});
`);

expect(stdout).toMatchSnapshot();
});


Check failure on line 96 in packages/jest-circus/src/__tests__/hooks.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Delete `⏎`
test('beforeAll is exectued correctly', () => {
const {stdout} = runTest(`
describe('describe 1', () => {
Expand All @@ -71,3 +110,132 @@ test('beforeAll is exectued correctly', () => {

expect(stdout).toMatchSnapshot();
});

test('beforeAll can return a cleanup function, run in the correct order relative to afterAll hooks', () => {
const {stdout} = runTest(`
describe('describe', () => {
afterAll(() => {
console.log('> afterAll 1 (defined first)');
});
beforeAll(() => {
console.log('> beforeAll 1');
return () => {
console.log('> cleanup 1');
};
});
afterAll(() => {
console.log('> afterAll 2');
});
beforeAll(() => {
console.log('> beforeAll 2');
return () => {
console.log('> cleanup 2');
};
});
afterAll(() => {
console.log('> afterAll 3 (defined last)');
});
test('test', () => {
console.log('> test');
});
});
`);

expect(stdout).toMatchSnapshot();
});

test('cleanup functions run in the correct order with nested describes', () => {
const {stdout} = runTest(`
describe('outer describe', () => {
afterAll(() => {
console.log('> outer afterAll 1 (defined first)');
});
beforeAll(() => {
console.log('> outer beforeAll');
return () => {
console.log('> outer cleanup');
};
});
afterAll(() => {
console.log('> outer afterAll 2 (defined last)');
});
describe('inner describe', () => {
afterAll(() => {
console.log('> inner afterAll 1 (defined first)');
});
beforeAll(() => {
console.log('> inner beforeAll');
return () => {
console.log('> inner cleanup');
};
});
afterAll(() => {
console.log('> inner afterAll 2 (defined last)');
});
test('test', () => {
console.log('> test');
});
});
});
`);

expect(stdout).toMatchSnapshot();
});

test('async cleanup functions are properly awaited', () => {
const {stdout} = runTest(`
let value = 0;
describe('describe', () => {
beforeEach(() => {
value += 1;
console.log('> beforeEach value:', value);
return async () => {
await new Promise(resolve => setTimeout(resolve, 0));
value -= 1;
console.log('> cleanup value:', value);
};
});
afterEach(() => {
console.log('> afterEach value:', value);
});
test('test', () => {
console.log('> test value:', value);
});
});
describe('describe with beforeAll', () => {
beforeAll(() => {
value += 2;
console.log('> beforeAll value:', value);
return async () => {
await new Promise(resolve => setTimeout(resolve, 0));
value -= 2;
console.log('> cleanup value:', value);
};
});
afterAll(() => {
console.log('> afterAll value:', value);
});
test('test', () => {
console.log('> test value:', value);
});
});
`);

expect(stdout).toMatchSnapshot();
});

Check failure on line 241 in packages/jest-circus/src/__tests__/hooks.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Insert `⏎`
Loading

0 comments on commit a1d2628

Please sign in to comment.