From f92dce51ed24a5b4e0009c1522ee12b30f3409ac Mon Sep 17 00:00:00 2001 From: Francisco Date: Wed, 4 Oct 2023 20:00:02 -0300 Subject: [PATCH] Reset Hardhat Network before each test suite (#4652) --- hardhat/env-contract.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/hardhat/env-contract.js b/hardhat/env-contract.js index 74d54cfbb04..c615249a3a3 100644 --- a/hardhat/env-contract.js +++ b/hardhat/env-contract.js @@ -2,9 +2,24 @@ extendEnvironment(env => { const { contract } = env; env.contract = function (name, body) { - // remove the default account from the accounts list used in tests, in order - // to protect tests against accidentally passing due to the contract - // deployer being used subsequently as function caller - contract(name, accounts => body(accounts.slice(1))); + const { takeSnapshot } = require('@nomicfoundation/hardhat-network-helpers'); + + contract(name, accounts => { + // reset the state of the chain in between contract test suites + let snapshot; + + before(async function () { + snapshot = await takeSnapshot(); + }); + + after(async function () { + await snapshot.restore(); + }); + + // remove the default account from the accounts list used in tests, in order + // to protect tests against accidentally passing due to the contract + // deployer being used subsequently as function caller + body(accounts.slice(1)); + }); }; });