Skip to main content

Isolation and snapshots

Per-test isolation​

With useAnvil() (default isolation: "test"):

  1. beforeAll: start, setup, baseline checkpoint();
  2. afterEach: drop pending transactions, restore the baseline;
  3. afterAll: stop.

A restore takes milliseconds; a new node, about a second.

What a snapshot restores​

Restored (chain state)Not restored (node settings)
balances, code, storage, noncesautomine / interval mining
blocks and block numbersimpersonation, auto-impersonation
the clock (block timestamps)block gas limit, coinbase
logging, block timestamp interval, chain id

Restore changed settings in finally. Reverting also keeps pending transactions: useAnvil() empties the pool; around withSnapshot() / checkpoint(), call cheats.dropAllTransactions() yourself.

Strict snapshots​

A snapshot can be reverted to once. anvil answers false for unknown or used ids, which viem ignores; the library throws SNAPSHOT_NOT_FOUND instead. checkpoint() re-snapshots after each restore.

reset() and loadState() discard every snapshot: use isolation: "none" in files that call them.

NeedAPI
isolate every testuseAnvil() default, or checkpoint() + afterEach(() => base.restore())
isolate a block of codewithSnapshot(async (client) => …)
back to genesis / the fork blockreset() (use isolation: "none")
fixturessetup, loadStateFromFile(), state.withLoadState()

Related: Share a node, Isolate state inside a test.