Reset block time

As i am testing i’m realizing that my tests use the current block time from the previous statements.

Is there a way to reset the block time?

To get more specific, i am using “describe … it”, and what seems to happen is as i increase the time using

await ethers.provider.send('evm_increaseTime', [seconds]);
await ethers.provider.send('evm_mine', []);

and what seems to happen is, in the next “describe”, uses the previous blocktime.

Hi, I think when you test your contract, the block time should increase all the time, if you want to reset the block time, that means you should reset your contract, so it can go back to the original status. The block time and the contract status should be consistent.

So just re-deploy the contracts to reset the block time.

Hi Skyge i found a better solution

  static async resetBlockTimestamp() {
    const blockNumber = ethers.provider.getBlockNumber();
    const block = await ethers.provider.getBlock(blockNumber);
    const currentTimestamp = Math.floor(new Date().getTime() / 1000);
    const secondsDiff = currentTimestamp - block.timestamp;
    await ethers.provider.send('evm_increaseTime', [secondsDiff]);
    await ethers.provider.send('evm_mine', []);
  }