-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Jest 27: incorrect afterEach
order with jest-circus
#11456
Comments
I'm not sure what makes the most sense. @jeysal? |
@SimenB In case this issue gets dismissed, is there any other way how to have a global |
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly.
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly.
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
Tbh I would say the previous behavior is rather unexpected (and I certainly didn't know it used to behave that way), because in any other case |
This is a followup after jestjs/jest#11456. Basically, our previous solution stopped working reliably and after a discussion with Jest team I realized that it's an ugly hack and we are lucky it worked for so long. This change introduces a new `@adeira/jest-disallow-console` package which helps with testing console warnings. The code is heavily inspired by `warnings.js` from `facebook/relay` (see: https://github.com/facebook/relay/blob/57dde664137a221a4633d1c02592b2a4e17d3feb/packages/relay-test-utils-internal/warnings.js) but it's modified to work with `global.console` instead of `warning` module.
This is a followup after jestjs/jest#11456. Basically, our previous solution stopped working reliably and after a discussion with Jest team I realized that it's an ugly hack and we are lucky it worked for so long. This change introduces a new `@adeira/jest-disallow-console` package which helps with testing console warnings. The code is heavily inspired by `warnings.js` from `facebook/relay` (see: https://github.com/facebook/relay/blob/57dde664137a221a4633d1c02592b2a4e17d3feb/packages/relay-test-utils-internal/warnings.js) but it's modified to work with `global.console` instead of `warning` module.
This is a followup after jestjs/jest#11456. Basically, our previous solution stopped working reliably and after a discussion with Jest team I realized that it's an ugly hack and we are lucky it worked for so long. This change introduces a new `@adeira/jest-disallow-console` package which helps with testing console warnings. The code is heavily inspired by `warnings.js` from `facebook/relay` (see: https://github.com/facebook/relay/blob/57dde664137a221a4633d1c02592b2a4e17d3feb/packages/relay-test-utils-internal/warnings.js) but it's modified to work with `global.console` instead of `warning` module.
This is a followup after jestjs/jest#11456. Basically, our previous solution stopped working reliably and after a discussion with Jest team I realized that it's an ugly hack and we are lucky it worked for so long. This change introduces a new `@adeira/jest-disallow-console` package which helps with testing console warnings. The code is heavily inspired by `warnings.js` from `facebook/relay` (see: https://github.com/facebook/relay/blob/57dde664137a221a4633d1c02592b2a4e17d3feb/packages/relay-test-utils-internal/warnings.js) but it's modified to work with `global.console` instead of `warning` module.
This is a followup after jestjs/jest#11456. Basically, our previous solution stopped working reliably and after a discussion with Jest team I realized that it's an ugly hack and we are lucky it worked for so long. This change introduces a new `@adeira/jest-disallow-console` package which helps with testing console warnings. The code is heavily inspired by `warnings.js` from `facebook/relay` (see: https://github.com/facebook/relay/blob/57dde664137a221a4633d1c02592b2a4e17d3feb/packages/relay-test-utils-internal/warnings.js) but it's modified to work with `global.console` instead of `warning` module.
This is a followup after jestjs/jest#11456. Basically, our previous solution stopped working reliably and after a discussion with Jest team I realized that it's an ugly hack and we are lucky it worked for so long. This change introduces a new `@adeira/jest-disallow-console` package which helps with testing console warnings. The code is heavily inspired by `warnings.js` from `facebook/relay` (see: https://github.com/facebook/relay/blob/57dde664137a221a4633d1c02592b2a4e17d3feb/packages/relay-test-utils-internal/warnings.js) but it's modified to work with `global.console` instead of `warning` module.
We ran into this issue as well. In several cases we have Putting the global |
I'm not sure if this bug I just found is related to this, but I was always under the impression that beforeEach/afterEach are always run in order of their declaration if within the same block. In my code, we have 2 global afterEach, one to do an expect, and another to do a cleanUp. Since the expect can throw, I didn't want to put it in the same block since I wanted the cleanup to run even if the expect fails:
Here I'm expecting the |
In that specific case, you can probably do afterEach(() => {
try {
expect(nock).toBeDone();
} finally {
nock.cleanAll();
}
}); |
I understand that, but that doesn't "fix" the original issue which is against what the documentation mentions around order of execution. |
Where in the docs is this specified? |
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` (see: jestjs/jest#11456). I will wait for the resolution of that issue and update our code accordingly. adeira-source-id: 154daeb2c02992787370c74c897cfb6a6164b0ab
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
do not close, please |
Still experiencing this issue with |
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 Let's take a look at a simple example: we want to backup a global variable for a test, and restore it afterwards. const backupTest = (newValue: typeof globalVar): void => {
let backup: typeof globalVar;
beforeEach(() => {
backup = globalVar;
globalVar = newValue;
});
afterEach(() => {
globalVar = backup;
});
}; The idea is simple, our test should look like that: describe('globalVar', () => {
backupTest(1);
it('equals 1', () => {
expect(globalVar).toEqual(1);
});
}); so instead of having 10 lines of code how to backup Now, let's imagine that In all testing libraries it will look like that: describe('globalVar', () => {
backupTest(1); // one group of stub members.
backupTest(2); // another group of stub members.
// tests
}); and it works well, after the test, However, with the new changes in let globalVar = 0;
const backupTest = (newValue: typeof globalVar): void => {
let backup: typeof globalVar;
beforeEach(() => {
backup = globalVar;
globalVar = newValue;
});
afterEach(() => {
globalVar = backup;
});
};
describe('backup', () => {
it('equals 0 before all', () => {
expect(globalVar).toEqual(0);
});
describe('globalVar', () => {
backupTest(1); // setting globalVar to 1 and restoring it to 0 afterwards
backupTest(2); // setting globalVar to 2 and restoring it to 1 afterwards
it('equals 2 before each', () => {
expect(globalVar).toEqual(2);
});
describe('each', () => {
backupTest(3); // setting globalVar to 3 and restoring it to 2 afterwards
backupTest(4); // setting globalVar to 4 and restoring it to 3 afterwards
it('equals 4 after each', () => {
expect(globalVar).toEqual(4);
});
});
it('resets to 2 after each', () => {
expect(globalVar).toEqual(2);
});
});
it('resets to 0 after all', () => {
expect(globalVar).toEqual(0);
});
}); which fails on
proposed fix long time ago: #12861 |
I was recently affected by this unexpected behavior too. In my case, it was caused by different testing utilities using beforeEach/afterEach to patch
I wonder what's the recommended approach to address scenarios like this. The best approach I could come up with is to not unpatch console.error at all (not tested): let shouldHideReactUpdateOnUnmountedWarning = false
beforeAll(() => {
const originalConsoleError: typeof console.error
console.error = (...args) => {
const reactUpdateOnUnmountedWarning = `Can't perform a React state update on an unmounted component`
if (
shouldHideReactUpdateOnUnmountedWarning &&
String(args[0]).includes(reactUpdateOnUnmountedWarning)
) {
return
}
originalConsoleError.call(console, ...args)
}
})
beforeEach(() => {
shouldHideReactUpdateOnUnmountedWarning = true
})
afterEach(() => {
shouldHideReactUpdateOnUnmountedWarning = false
}) |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days. |
This is still relevant. 😎 |
not stale |
💥 Regression Report
In Jest 26, it was possible to set up
afterEach
callback in asetupFilesAfterEnv
script which was correctly called after the tests specificafterEach
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 specificafterEach
callbacks).It seems that Jest 27 calls the "global"
afterEach
before the test-specificafterEach
which breaks our codebase. It's because some of the tests are callingjest.restoreAllMocks
inafterEach
and later we are checking (in the globalafterEach
) whether allconsole
mocks 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:
afterEach
callback insetupFilesAfterEnv
scriptafterEach
callback in some testsetupFilesAfterEnv
)Expected behavior
The order of
afterEach
callbacks is not changed from version 26 so it's still possible to register custom globalafterEach
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:
The text was updated successfully, but these errors were encountered: