Skip to content

Testing with Vitest

Running TEVM inside Vitest eliminates external node processes, port collisions across parallel test workers, and slow RPC round trips. Each test file or test case can own an isolated engine, or share a single engine rewound with snapshot() / revert().

Create a fresh client in beforeEach and release it in afterEach. This guarantees zero shared state between tests:

counter.spec.ts
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { createMemoryClient, type MemoryClient, parseAbi, PREFUNDED_ACCOUNTS } from 'tevm'
const ANSWER_CREATION_BYTECODE = '0x600a600c600039600a6000f3602a60005260206000f3'
const ANSWER_ABI = parseAbi(['function answer() view returns (uint256)'])
describe('Answer contract', () => {
let client: MemoryClient
beforeEach(async () => {
client = createMemoryClient({
account: PREFUNDED_ACCOUNTS[0],
})
await client.tevmReady()
})
afterEach(async () => {
await client.tevmClose()
})
it('deploys and returns 42', async () => {
const deployed = await client.tevmDeploy({
bytecode: ANSWER_CREATION_BYTECODE,
})
expect(deployed.createdAddress).toBeDefined()
const result = await client.tevmContract({
address: deployed.createdAddress!,
abi: ANSWER_ABI,
functionName: 'answer',
})
expect(result.data).toBe(42n)
})
})

Pattern 2: Shared deployment with snapshot rollback

Section titled “Pattern 2: Shared deployment with snapshot rollback”

When deploying a multi-contract system or forking mainnet, set up the fixture once in beforeAll, take a snapshot before each test, and revert afterward:

vault-fixture.spec.ts
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest'
import { createMemoryClient, type Hex, type MemoryClient, parseEther, PREFUNDED_ACCOUNTS } from 'tevm'
describe('Vault suite (snapshot isolated)', () => {
let client: MemoryClient
let snapshotId: Hex
const [deployer, user] = PREFUNDED_ACCOUNTS
beforeAll(async () => {
client = createMemoryClient({ account: deployer })
await client.tevmReady()
// Expensive fixture setup runs once
await client.tevmDeal({ address: user.address, amount: parseEther('500') })
})
beforeEach(async () => {
snapshotId = await client.snapshot()
})
afterEach(async () => {
await client.revert({ id: snapshotId })
})
afterAll(async () => {
await client.tevmClose()
})
it('spends ETH inside test 1', async () => {
await client.tevmDeal({ address: user.address, amount: parseEther('1') })
expect(await client.getBalance({ address: user.address })).toBe(parseEther('1'))
})
it('sees the clean 500 ETH fixture in test 2', async () => {
expect(await client.getBalance({ address: user.address })).toBe(parseEther('500'))
})
})

Capture Hardhat console.log calls directly in test assertions using the consoleLog option, and verify revert payloads with NativeRpcError:

revert-and-logs.spec.ts
import { describe, expect, it } from 'vitest'
import { createMemoryClient, NativeRpcError } from 'tevm'
describe('Reverts and console.log', () => {
it('captures revert errors and Solidity console logs', async () => {
const logs: string[] = []
const client = createMemoryClient({
consoleLog: (message) => {
logs.push(message)
},
})
try {
await expect(
client.tevmCall({ code: '0x60006000fd' }), // REVERT
).rejects.toSatisfy(
(error: unknown) => error instanceof NativeRpcError && error.code === 3,
)
} finally {
await client.tevmClose()
}
})
})
vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
environment: 'node',
testTimeout: 20_000, // extra headroom for forked tests
},
})

If you also import .s.sol files directly in Vitest, add vitePluginTevm() to the plugins array in vitest.config.ts (see Tutorial: Solidity bundler).