-
-
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
Should restoring mocks and other things happen in afterEach? #9896
Comments
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 issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
My understanding is that this is still current. @SimenB |
const someMock = jest.fn().mockImplementation(() => true);
beforeEach(() => {
jest.restoreAllMocks();
});
test("one", () => {
expect(someMock()).toBe(undefined); // mock was restored
});
test("two", () => {
expect(someMock()).toBe(undefined); // mock was restored
}); Same with const someMock = jest.fn().mockImplementation(() => true);
afterEach(() => {
jest.restoreAllMocks();
});
test("three", () => {
expect(someMock()).toBe(true); // mock will be restored after this test
});
test("four", () => {
expect(someMock()).toBe(undefined); // mock was restored
}); Regarding restoring mocks after all tests. It seem like |
@mrazauskas I don't mind doing it in |
This example makes no sense: why are you specifying a mock at first if you don't want to use it afterwards? |
In the real life, we'll usually define a mock in both ways:
First option without using jest.fn() works fine. |
That was just a demo: what is the difference if |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This is a follow up to #7654.
Currently restoring mocks happen in
beforeEach
, possibly becauseafterEach
isn't run in case a test fails. I don't know if that's what users expect, at least the documentation doesn't say anything about that.In #7654 we saw that it can impact tests when working with shared prototypes where some methods were mocked. Of course the main issue is the shared prototype thing, but by restoring mocks in
beforeEach
we wouldn't restore it for the final test in a test file, so this exacerbates the problem.Relevant code (thanks @SimenB!):
https://github.com/facebook/jest/blob/faa52673f4d02ee1d273103ada9382a0805b54a4/packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapter.ts#L56-L77
https://github.com/facebook/jest/blob/faa52673f4d02ee1d273103ada9382a0805b54a4/packages/jest-jasmine2/src/index.ts#L100-L120
My proposal here is:
afterEach
instead of, or possibly in addition to,beforeEach
.afterEach
even if a test fails (working like afinally
).What do you all think?
The text was updated successfully, but these errors were encountered: