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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support hardhat_reset #667

Merged
merged 1 commit into from Jan 14, 2022
Merged

Conversation

adjisb
Copy link
Contributor

@adjisb adjisb commented Sep 29, 2021

No description provided.

@nchamo
Copy link

nchamo commented Oct 30, 2021

Hey @adjisb , just wanted to say that this change also fixed the problem for me 😄

@jummy123
Copy link

Hi @adjisb this patch also works in my use case where I reset hardhat and fork to a specific block number before each test. Are you still trying to get this merged or is there another accepted way to get solidity-coverage working with hardhat_reset.

@adjisb
Copy link
Contributor Author

adjisb commented Dec 29, 2021

I don't know if there is a better way and what is needed to get this PR merged.

@jummy123
Copy link

@cgewecke sorry for calling you out on this PR. This is blocking us from using coverage ATM, is there any feedback on this PR. I'm happy to help out fixing this if you had an alternate fix in mind.

@cgewecke
Copy link
Member

Apologies, will take a look this weekend.

@Shelvak
Copy link

Shelvak commented Jan 13, 2022

I can confirm that the patch is working as expected too =) Thanks for the easy patch @adjisb =)

@cgewecke cgewecke changed the base branch from master to hardhat-reset-tests January 14, 2022 21:19
@cgewecke cgewecke self-requested a review January 14, 2022 21:21
Copy link
Member

@cgewecke cgewecke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank you.

@cgewecke cgewecke merged commit b437cbf into sc-forks:hardhat-reset-tests Jan 14, 2022
cgewecke pushed a commit that referenced this pull request Jan 15, 2022
Co-authored-by: Andres Adjimann <aadjiman@gmail.com>
@cgewecke
Copy link
Member

cgewecke commented Jan 15, 2022

@adjisb @Shelvak

I have written a test for this in #681 but it fails. My expectation is that given a contract with 2 simple functions I can:

  • call function 1
  • hardhat_reset
  • call function 2
    ... and have 100% line coverage.

Is this what you are seeing when you use the solution in this PR?

In the hardhat code it looks like the hardhat node is destroyed and recreated on each reset event. For solidity-coverage to re-attach a step listener to its vm it's important that we get a reference to the new node object. Have found this to be impossible so far.

Event listeners attached to the new (or is it old for some reason?) vm never fire.

Could either of you (or anyone in this thread) comment on whether this is correct? It could be that I've made a wrong assumption somewhere here or something about the test environment in this repo is resulting in a false failure.

Note: I've slightly modified the logic in the new PR to use an hre scoped reference to the network provider but the incorrect behavior exists for this PR's approach as well. Have also tried:

  • using setTimeout in various places to make sure I'm not running into an event related race condition.
  • re-running nomiclabsUtils.setupHardhatNetwork(env, api, ui) in the reset event call back

@Shelvak
Copy link

Shelvak commented Jan 16, 2022

Ho @cgewecke I'll pull your branch and test locally. In my case the coverage fails just with one file and one test with the reset in the before callback. Will back to you after I found a way to fix or to make the test pass =)

@Shelvak
Copy link

Shelvak commented Jan 16, 2022

@cgewecke it's maybe because of the "multiple describe"... This is the way it's failing me:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("contractA", function() {
  let instance;
  let startBlockNumber;

  before(async () => {
    await hre.network.provider.request({
      method: "hardhat_reset",
      params: [
        {
          forking: {
            jsonRpcUrl: hre.network.config.forking.url,
            blockNumber: hre.network.config.forking.blockNumber
          },
        },
      ],
    });

    const factory = await ethers.getContractFactory("ContractA");
    instance = await factory.deploy();
  });

  it('sends', async function(){
    await instance.sendFn();
    await instance.sendFn2(); // call 2nd fn to pass the coverage part =)
  });
});

And it's confirmed with this test that if I comment the patch the test fails with 0% coverage
imagen

Let me know if you need anything else 👍 thanks for the lib, it's amazing =D

Cheers

@cgewecke
Copy link
Member

Thanks so much @Shelvak! I think I misunderstood what range of block numbers are ok to pass to hardhat_reset. Your suggestion to restructure the test fixes things.

cgewecke added a commit that referenced this pull request Jan 17, 2022
Co-authored-by: adjisb <85941014+adjisb@users.noreply.github.com>
Co-authored-by: Andres Adjimann <aadjiman@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants