@hellaweb3/foundryanvil-testcontainers-nodejs
v0.7.2
Published
Fluent, type-safe Testcontainers module for Foundry Anvil: every anvil CLI flag with conflict/dependency validation, reliable startup, host/container state transfer and a chain-aware viem test client with helpers.
Maintainers
Readme
📖 Documentation · Quick start · Tasks · API reference · Examples
A Testcontainers module that runs a Foundry Anvil node in Docker for tests.
- Every anvil flag through a fluent, type-safe API, validated as you set it.
- Reliable startup that reports anvil's own errors, and host files copied in and out.
- A pre-configured viem test client bound to the node, with anvil extras and fluent
cheats. - Per-test isolation with
useAnvil(): one node per file, restored after every test. - Failure diagnostics: every failing test leaves decoded reverts, traces, the pool and the node's log.
- Gas and JSON-RPC profiling of every test, with budgets and reports.
Default image: ghcr.io/foundry-rs/foundry:v1.6.0-rc1 ·
Anvil reference
Installation
pnpm add -D @hellaweb3/foundryanvil-testcontainers-nodejsRequires Node.js ≥ 22 and a Docker-compatible runtime (or none).
Quick start
A complete test file:
import {
AnvilContainer,
AnvilOptions,
Hardfork,
useAnvil,
} from "@hellaweb3/foundryanvil-testcontainers-nodejs";
import { parseEther } from "viem";
const options = new AnvilOptions().account
.withAccounts(5)
.withBalance(1_000) // ETH per dev account
.evm.withChainId(1337)
.withHardfork(Hardfork.Cancun)
.logs.quiet();
// Starts in beforeAll, stops in afterAll, restores the state after every test.
const anvil = useAnvil(
new AnvilContainer(options).withTestClient((client) => client.withAccount(0)),
);
it("sends Ether", async () => {
const [, bob] = await anvil.addresses();
// No `account` needed: dev account 0 is the typed default signer.
const receipt = await anvil.confirm(
anvil.testClient.sendTransaction({ to: bob, value: parseEther("1") }),
);
expect(receipt.status).toBe("success");
expect(await anvil.testClient.getChainId()).toBe(1337);
});
it("manipulates the chain fluently", async () => {
const whale = "0x1111111111111111111111111111111111111111";
await anvil.cheats
.setBalanceEther(whale, 1_000)
.impersonate(whale)
.increaseTime({ days: 7 })
.mine();
expect(await anvil.getBalance(whale)).toBe(parseEther("1000"));
});Without useAnvil: const anvil = await new AnvilContainer(options).start()
in beforeAll and await anvil.stop() in afterAll
(lifecycle).
Documentation
The documentation site is organised by task, with a complete API reference; every code sample on it (and in this README) is a tested example.
| I want to… | Guide | |---|---| | Share a node per test file, isolate tests | Share a node · Isolate tests | | Use, fund or impersonate accounts | Accounts · Impersonate | | Send transactions, assert reverts, simulate | Send · Reverts · Simulate | | Move time, control mining | Time · Mining | | Mock contracts, save and load state | Mocks · State files | | Fork mainnet, watch events | Fork a chain · Events | | Configure the node and the client | Configure | | Find out why a test failed | Diagnose failing tests · Trace | | Profile gas and JSON-RPC calls | Gas · JSON-RPC · Budgets |
API reference: AnvilOptions · AnvilContainer · StartedAnvilContainer · test client · useAnvil · diagnostics · errors · everything.
Examples
examples/ is a separate npm project that uses this library the way your project would, with a tested workflow per topic; its workflows are the source of every code sample in the documentation.
Contributing
Scripts, the e2e suite (pnpm test:e2e, or --no-docker for local testing),
the docs site (pnpm docs:start) and release steps: see
CONTRIBUTING.md.
For support options, see SUPPORT.md. To report a security vulnerability privately, follow SECURITY.md. Community behavior is governed by CODE_OF_CONDUCT.md.
Maintained by Hella Labs · MIT
