Hardhat Chain Fork Testing – How to Execute on Test Rather Than Config File

hardhatsoliditytesting

I saw that it is possible to test with Hardhat on a forked chain, via the config file.

Is it possible to do it on the test file instead? In a beforeEach, maybe.

I only want some of my tests to run on a fork, while the majority should just run on the normal test network.

Best Answer

yes

just change your network (add this with your api key to the before each):

await hre.network.provider.request({
      method: "hardhat_reset",
      params: [{forking: {
            jsonRpcUrl: "https://eth-mainnet.alchemyapi.io/v2/xxxxxx",
            blockNumber:14768690
          },},],
      });
Related Topic