-
-
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
Documentation: Not possible to restore initial implementation when mocking? #7068
Comments
This works: class Bla {
foo() {
return true;
}
}
const bla = new Bla();
describe("Bla", () => {
it("should return mock value", () => {
jest.spyOn(bla, "foo").mockImplementationOnce(() => false);
const result = bla.foo();
expect(result).toBe(false);
});
it("should return real value", () => {
const result = bla.foo();
expect(result).toBe(true);
});
}); or class Bla {
foo() {
return true;
}
}
const bla = new Bla();
describe("Bla", () => {
afterEach(() => {
jest.restoreAllMocks();
});
it("should return mock value", () => {
jest.spyOn(bla, "foo").mockImplementation(() => false);
const result = bla.foo();
expect(result).toBe(false);
});
it("should return real value", () => {
const result = bla.foo();
expect(result).toBe(true);
});
}); |
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 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
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. |
💥 Regression Report
Jest documentation for
restoreMocks
claims to remove fake implementations and restore initial implementations, but the underlying functionality here appears to have changed to no longer remove fake implementations (and doesn't seem to restore pre-mocked functionality even when called manually afterjest.resetAllMocks
in an afterEach block).resetMocks
does remove fake implementations, but does not restore initial implementations.The differences between these methods are not clearly explained (#5969, #5143, #4828), and it's unclear if it's even possible to restore pre-mocked functionality.
Neither
resetModules
norclearMocks
seem to have this functionality either.To Reproduce
foo.txt
Expected behavior
The Jest documentation accurately describes the behavior of mock reset/restore functionality and what is or is not possible when mocking.
Link to repl or repo (highly encouraged)
https://github.com/mikemorris/jest-restore-mocks
Run
npx envinfo --preset jest
The text was updated successfully, but these errors were encountered: