Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support beforeEach and beforeAll returning cleanup functions #15510

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions packages/jest-circus/src/__tests__/__snapshots__/hooks.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,104 @@
// 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
> cleanup value: <yellow>0</color>
hook_success: afterEach
hook_start: afterEach
> afterEach value: <yellow>0</color>
hook_success: afterEach
test_done: test
run_describe_finish: describe
run_describe_start: describe with beforeAll
hook_start: beforeAll
> beforeAll value: <yellow>2</color>
hook_success: beforeAll
test_start: test
test_started: test
test_fn_start: test
> test value: <yellow>2</color>
test_fn_success: test
test_done: test
hook_start: afterAll
> cleanup value: <yellow>0</color>
hook_success: afterAll
hook_start: afterAll
> afterAll value: <yellow>0</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
> cleanup 1
hook_success: afterAll
hook_start: afterAll
> afterAll 2
hook_success: afterAll
hook_start: afterAll
> cleanup 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 +145,52 @@ 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 1
hook_success: beforeEach
hook_start: beforeEach
> beforeEach 2
hook_success: beforeEach
test_fn_start: test
> test
test_fn_success: test
hook_start: afterEach
> afterEach 1 (defined first)
hook_success: afterEach
hook_start: afterEach
> cleanup 1
hook_success: afterEach
hook_start: afterEach
> afterEach 2
hook_success: afterEach
hook_start: afterEach
> cleanup 2
hook_success: afterEach
hook_start: afterEach
> afterEach 3 (defined last)
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 +281,60 @@ 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 cleanup
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 cleanup
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 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 @@
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 @@

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
Loading