Hardhat – How to Reset the Local Hardhat Node Inside a Test Suite?

hardhat

I am testing some deployment features of my contracts, and I would like to reset the local hardhat network to initial state between some of the tests, in particular, to clear out all previously deployed contracts from the test net. (I am not using an independent note — ust the one that gets created when you type hh test.)

Is there a way to do this inside a test suite, rather than having to rerun it all together?

Best Answer

Use hardhat_reset in a beforeEach hook:

describe("suite", function () {
  beforeEach(async function () {
    await hre.network.provider.send("hardhat_reset")
  })

  it("first test", function () {
    // Fresh instance of the hardhat network

    // Send txs
  })

  it("second test", function () {
    // Fresh instance of the network again

    // The txs sent in the previous test won't 
    // be reflected here.
  })
})