jest: Jest 27: incorrect `afterEach` order with `jest-circus`
💥 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
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 10
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/universe by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/universe by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/eslint-fixtures-tester by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/relay-example by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/fetch by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/js by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/relay by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/sx-design by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/sx-jest-snapshot-serializer by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/sx by mrtnzlml 3 years ago
- Publish new package `@adeira/jest-disallow-console` This is a followup after https://github.com/facebook/jest/issues/11456. Basically, our previous solution stopped working reliably and after a discu... — committed to adeira/universe by mrtnzlml 3 years ago
- Publish new package `@adeira/jest-disallow-console` This is a followup after https://github.com/facebook/jest/issues/11456. Basically, our previous solution stopped working reliably and after a discu... — committed to adeira/universe by mrtnzlml 3 years ago
- Publish new package `@adeira/jest-disallow-console` This is a followup after https://github.com/facebook/jest/issues/11456. Basically, our previous solution stopped working reliably and after a discu... — committed to adeira/universe by mrtnzlml 3 years ago
- Publish new package `@adeira/jest-disallow-console` This is a followup after https://github.com/facebook/jest/issues/11456. Basically, our previous solution stopped working reliably and after a discu... — committed to adeira/universe by mrtnzlml 3 years ago
- Publish new package `@adeira/jest-disallow-console` This is a followup after https://github.com/facebook/jest/issues/11456. Basically, our previous solution stopped working reliably and after a discu... — committed to adeira/universe by mrtnzlml 3 years ago
- Publish new package `@adeira/jest-disallow-console` This is a followup after https://github.com/facebook/jest/issues/11456. Basically, our previous solution stopped working reliably and after a discu... — committed to adeira/universe by mrtnzlml 3 years ago
- Use Jest version 27.0.1 See release notes: https://jestjs.io/blog/2021/05/25/jest-27 There is currently one issue with global `afterEach` callbacks order which I workarounded by using `afterAll` (se... — committed to adeira/abacus-backoffice by mrtnzlml 3 years ago
Hi @jeysal ,
I would still vote for fixing a bug instead of documenting it.
There are many testing libraries around, which use hooks under the hood, and the expectation is that the order of their execution will be respected, as it is in all other testing frameworks. Not sure why
jest
decided to invent own way and to cause issues.Let’s take a look at a simple example: we want to backup a global variable for a test, and restore it afterwards. To avoid copy-pasting of tons of lines of code, we decided to write a helper function:
The idea is simple, our test should look like that:
so instead of having 10 lines of code how to backup
globalVar
for each test, we have just 1.Now, let’s imagine that
globalVar
isn’t primitive, but a complex object andbackupTest
does only 1 modification, whereas we need an additional modification, for example, another group of stub members.In all testing libraries it will look like that:
and it works well, after the test,
globalVar
will have its initial value before the suite, because the first executedafterEach
belongs to2
and the second executedafterEach
belongs to1
.However, with the new changes in
jest
,globalVar
is going to be1
, because of the broken order, because firstjest
executesafterEach
for1
, and thenafterEach
for2
, which restores a wrong value.which fails on
jest
asproposed fix long time ago: https://github.com/jestjs/jest/pull/12861
This is still relevant. 😎
Still experiencing this issue with
jest-circus
… For now we have unblocked ourselves from upgrading past Jest 26 by using explicitly specifyingjest-jasmine2
as our test runner.do not close, please
In that specific case, you can probably do