Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.
This repository was archived by the owner on May 20, 2025. It is now read-only.

Contracts can be unintenionally mocked when snapshots are used #178

@fvictorio

Description

@fvictorio

Describe the bug
If you use snapshots and smock, a non-mocked contract in one test can accidentally get the mocked behavior from a previous test.

Reproduction steps

Consider this contract:

contract Foo {
  function f() public view returns (uint) {
    return 42;
  }
}

and these tests:

it("test 1", async function () {
  const snapshotId = await network.provider.send("evm_snapshot")
  const Foo = await smock.mock("Foo");
  const foo = await Foo.deploy();

  console.log("Address:", foo.address);

  foo.f.returns(1);

  console.log(await foo.f());

  await network.provider.send("evm_revert", [snapshotId])
});

it("test 2", async function () {
  const Foo = await smock.mock("Foo");
  const foo = await Foo.deploy();

  console.log("Address:", foo.address);

  console.log(await foo.f());
});

If you run it, the second test will log 1 instead of the expected 42.

Expected behavior
Newly deployed contracts don't have their functions mocked, even if their address match the address of a previously deployed and mocked contract.

Additional context
See this issue in Hardhat's repo.

Possible solutions
I'm not sure what's the right solution here, but some ideas:

  • Just drop all the fakes/mocks when hardhat_revert is called. We emit an event in the provider when this happens, so you should be able to listen for it.
  • If you think the user should handle this, then add something like Allow mock/fake.resetAll() #129 would help, but it should be a global function instead that just clears everything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions