From a18fbb889979ad0a348efa1a99c135663e62f568 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 27 Jan 2025 16:06:00 +0100 Subject: [PATCH] vault: allow automine to be disabled in time sensitive tests --- test/Vault.tests.js | 13 ++++++++++++- test/evm.js | 11 +++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/test/Vault.tests.js b/test/Vault.tests.js index f1a8419..0529b6f 100644 --- a/test/Vault.tests.js +++ b/test/Vault.tests.js @@ -1,7 +1,13 @@ const { expect } = require("chai") const { ethers } = require("hardhat") const { randomBytes } = ethers.utils -const { currentTime, advanceTimeTo, mine } = require("./evm") +const { + currentTime, + advanceTimeTo, + mine, + snapshot, + revert, +} = require("./evm") describe("Vault", function () { let token @@ -10,6 +16,7 @@ describe("Vault", function () { let account, account2, account3 beforeEach(async function () { + await snapshot() const TestToken = await ethers.getContractFactory("TestToken") token = await TestToken.deploy() const Vault = await ethers.getContractFactory("Vault") @@ -20,6 +27,10 @@ describe("Vault", function () { await token.mint(account3.address, 1_000_000) }) + afterEach(async function () { + await revert() + }) + describe("depositing", function () { const context = randomBytes(32) const amount = 42 diff --git a/test/evm.js b/test/evm.js index 85aa141..589ac73 100644 --- a/test/evm.js +++ b/test/evm.js @@ -5,13 +5,19 @@ let snapshots = [] async function snapshot() { const id = await ethers.provider.send("evm_snapshot") const time = await currentTime() - snapshots.push({ id, time }) + const automine = await ethers.provider.send("hardhat_getAutomine") + snapshots.push({ id, time, automine }) } async function revert() { - const { id, time } = snapshots.pop() + const { id, time, automine } = snapshots.pop() await ethers.provider.send("evm_revert", [id]) await ethers.provider.send("evm_setNextBlockTimestamp", [time]) + await ethers.provider.send("evm_setAutomine", [automine]) +} + +async function setAutomine(enabled) { + await ethers.provider.send("evm_setAutomine", [enabled]) } async function mine() { @@ -46,6 +52,7 @@ async function setNextBlockTimestamp(timestamp) { module.exports = { snapshot, revert, + setAutomine, mine, ensureMinimumBlockHeight, currentTime,