Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation: Not possible to restore initial implementation when mocking? #7068

Closed
mikemorris opened this issue Sep 28, 2018 · 4 comments
Closed

Comments

@mikemorris
Copy link

mikemorris commented Sep 28, 2018

馃挜 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 after jest.resetAllMocks in an afterEach block).

afterEach(() => {
  jest.resetAllMocks();
  jest.restoreAllMocks();
});

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 nor clearMocks seem to have this functionality either.

To Reproduce

jest.mock("fs");

const fs = require("fs");

it("mocks fs.readFileSync", () => {
  fs.readFileSync.mockReturnValue("foo");
  const file = fs.readFileSync("foo.txt", "utf8");
  expect(file).toEqual("foo");
});

it("restores mock and reads file", () => {
  const file = fs.readFileSync("foo.txt", "utf8");
  expect(file).toEqual("bar");
  // fails with `undefined` when run with --resetMocks
  // fails with `foo` when run with --restoreMocks
});

foo.txt

bar

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

  System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
  Binaries:
    Node: 8.9.3 - ~/.asdf/shims/node
    npm: 6.4.1 - ~/.asdf/shims/npm
  npmPackages:
    jest: ^23.6.0 => 23.6.0
@angeloocana
Copy link

angeloocana commented Oct 3, 2018

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);
	});
});

@github-actions
Copy link

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.

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

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.

@github-actions
Copy link

github-actions bot commented May 1, 2022

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants