Fast-forward time
Use anvil.cheats.increaseTime() and mine a block; time helpers take durations and Dates.
Jump ahead
The samples use a vault that locks alice's deposit for 30 days:
/** Simulates alice's withdrawal from the vault (throws while locked). */
const withdraw = () =>
anvil.testClientFor(alice).simulateContract({
address: vault,
abi: timeLockVault.abi,
functionName: "withdraw",
});
Tested in examples/test/e2e/time-travel.e2e.test.ts:59
await anvil.cheats.increaseTime({ days: 30 }).mine();
const { request } = await withdraw();
await anvil.confirm(anvil.testClientFor(alice).writeContract(request));
expect(await balanceOf(anvil.testClient, token, alice)).toBe(
parseEther("10"),
);
Tested in examples/test/e2e/time-travel.e2e.test.ts:75
Time moves with the next mined block.
Hit an exact timestamp
const { unlockTime } = await vaultLock(anvil.testClient, vault, alice);
const asAlice = anvil.testClientFor(alice);
const sendWithdraw = () =>
asAlice.writeContract({
address: vault,
abi: timeLockVault.abi,
functionName: "withdraw",
}); // signed by anvil: mined even when it reverts
// One second early: mined, but reverted.
await anvil.cheats.setNextBlockTimestamp(unlockTime - 1n);
await expect(anvil.confirm(sendWithdraw())).rejects.toBeInstanceOf(
AnvilTransactionRevertedError,
);
// Exactly at the unlock time: succeeds.
await anvil.cheats.setNextBlockTimestamp(unlockTime);
const receipt = await anvil.confirm(sendWithdraw());
expect(receipt.status).toBe("success");
Tested in examples/test/e2e/time-travel.e2e.test.ts:87
Set the clock
const launch = new Date("2030-01-01T00:00:00Z");
await anvil.cheats.setTime(launch).mine();
const { timestamp } = await anvil.testClient.getBlock();
expect(timestamp).toBeGreaterThanOrEqual(BigInt(launch.getTime() / 1000));
// 2030 is long past the unlock time: the withdrawal goes through.
const { request } = await withdraw();
expect(
(await anvil.confirm(anvil.testClientFor(alice).writeContract(request)))
.status,
).toBe("success");
Tested in examples/test/e2e/time-travel.e2e.test.ts:141
Regular block times
// Node settings are not part of snapshots: restore them yourself.
await anvil.cheats.setBlockTimestampInterval(12); // 12 s "slots"
try {
const start = await anvil.latestTimestamp();
await anvil.mine(5);
expect(await anvil.latestTimestamp()).toBe(start + 60n);
} finally {
await anvil.cheats.removeBlockTimestampInterval();
}
Tested in examples/test/e2e/time-travel.e2e.test.ts:111
Blocks spaced in time
const before = await anvil.testClient.getBlockNumber();
await anvil.mine(3, { hours: 1 }); // three blocks, one hour apart
const times = await Promise.all(
[1n, 2n, 3n].map(
async (offset) =>
(await anvil.testClient.getBlock({ blockNumber: before + offset }))
.timestamp,
),
);
expect([times[1] - times[0], times[2] - times[1]]).toEqual([3600n, 3600n]);
Tested in examples/test/e2e/time-travel.e2e.test.ts:125
Durations and timestamps
Seconds, unit objects and Dates:
expect(durationToSeconds({ days: 1, hours: 2 })).toBe(93_600n);
expect(durationToSeconds(90)).toBe(90n);
expect(timestampToSeconds(new Date("2030-01-01T00:00:00Z"))).toBe(
1_893_456_000n,
);
expect(() => durationToSeconds({ days: -1 })).toThrow(RangeError);
Tested in examples/test/unit/options.test.ts:283
A fixed start time: Boot from a custom genesis.
Concepts
- How per-test isolation works (the block timestamp interval is a node setting)
- Reference: cheats