Description
💥 Regression Report
In Jest 26, it was possible to set up afterEach
callback in a setupFilesAfterEnv
script which was correctly called after the tests specific afterEach
callbacks. It makes sense to behave like this because it allows us to set up callbacks that are executed after any test was completed (including their specific afterEach
callbacks).
It seems that Jest 27 calls the "global" afterEach
before the test-specific afterEach
which breaks our codebase. It's because some of the tests are calling jest.restoreAllMocks
in afterEach
and later we are checking (in the global afterEach
) whether all console
mocks were actually restored or not.
Use case: we are capturing unexpected console logs in a similar way like React, see:
- https://github.com/facebook/react/blob/bb1c8215569f59b70276e8939bd8a59aa7f28f53/scripts/jest/setupTests.js#L159-L160
- our code: https://github.com/adeira/universe/blob/adc8bdf25d253b8bb883a040b6bf0d4bd1b66404/scripts/jest/setupTests.js#L63-L92
Test file:
afterEach(() => {
jest.restoreAllMocks();
});
Setup file:
afterEach(() => {
// here we check whether `restoreAllMocks` (or any equivalent which restores console mocks) was called
// HOWEVER, if this global callback is called first then the test-specific mocks are not restored yet breaking the logic
});
Last working version
Worked up to version: 26.6.3
Stopped working in version: 27.0.1
To Reproduce
Steps to reproduce the behavior:
- setup
afterEach
callback insetupFilesAfterEnv
script - setup
afterEach
callback in some test - check the order of execution of these callbacks (the test specific one should be run before the one from
setupFilesAfterEnv
)
Expected behavior
The order of afterEach
callbacks is not changed from version 26 so it's still possible to register custom global afterEach
callback.
Link to repl or repo (highly encouraged)
https://replit.com/@mrtnzlml/jest-afterEach-bug (try to change the Jest versions in package.json
and check the difference in the order)
Run npx envinfo --preset jest
Paste the results here:
System:
OS: macOS 11.2.3
CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Binaries:
Node: 16.2.0 - /usr/local/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 7.14.0 - /usr/local/bin/npm
npmPackages:
jest: ^27.0.1 => 27.0.1