💥 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:
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 in setupFilesAfterEnv 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
💥 Regression Report
In Jest 26, it was possible to set up
afterEachcallback in asetupFilesAfterEnvscript which was correctly called after the tests specificafterEachcallbacks. 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 specificafterEachcallbacks).It seems that Jest 27 calls the "global"
afterEachbefore the test-specificafterEachwhich breaks our codebase. It's because some of the tests are callingjest.restoreAllMocksinafterEachand later we are checking (in the globalafterEach) whether allconsolemocks were actually restored or not.Use case: we are capturing unexpected console logs in a similar way like React, see:
Test file:
Setup file:
Last working version
Worked up to version: 26.6.3
Stopped working in version: 27.0.1
To Reproduce
Steps to reproduce the behavior:
afterEachcallback insetupFilesAfterEnvscriptafterEachcallback in some testsetupFilesAfterEnv)Expected behavior
The order of
afterEachcallbacks is not changed from version 26 so it's still possible to register custom globalafterEachcallback.Link to repl or repo (highly encouraged)
https://replit.com/@mrtnzlml/jest-afterEach-bug (try to change the Jest versions in
package.jsonand check the difference in the order)Run
npx envinfo --preset jestPaste the results here: