0

I have this code for a Voting smart contract test:

it("Should return true if end time is less than the current timestamp", async function () {
  const { votingSmartContract } = await loadFixture(deployVotingSmartContract);
  const now = Math.floor(new Date().getTime()/1000.0);
  await votingSmartContract.initialise(
    [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000], // candidates
    now + 1 // endTime
  );
  expect(await votingSmartContract.checkVotingIsOver()).to.be.true;

});

What this does is basically initialise the voting so it finishes in 1 sec, so I can then test that checkVotingIsOver returns true.

Problem is test is failing because I am getting false every time. I have console.log the block.timestamp that I am checking and it looks like I am always getting it to be 1 LESS than the now variable.

How can I wait until block.timestamp increases so I can pass this test?

1
  • Consider switching to Foundry and warping in time would become 100x easier. Commented Dec 7, 2023 at 21:22

1 Answer 1

1

There are hardhat helpers for manipulating time and block.number.

Time-dependent tests with Hardhat?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.