Write test definitions inside the /test directory, for example, test/HelloWorld.t.sol.
// SPDX-License-Identifier: MITpragma solidity ^0.8.0;import { Test } from "forge-std/Test.sol";contract HelloWorldTest is Test { function setUp() public { owner = makeAddr("owner"); vm.prank(owner); helloWorld = new HelloWorld(); } function test_HelloWorld() public { helloWorld.setMessage("Hello, World!"); assertEq(helloWorld.getMessage(), "Hello, World!"); }}
When testing on Abstract, cheatcodes have important limitations due to how the underlying ZKsync VM executes transactions. Cheatcodes can only be used at the root level of your test contract - they cannot be accessed from within any contract being tested.
// This works ✅function testCheatcodeAtRootLevel() public { vm.roll(10); // Valid: called directly from test vm.prank(someAddress); // Valid: called directly from test MyContract testContract = new MyContract(); testContract.someFunction(); // Cheatcodes not available inside this call}// This won't work as expected ❌contract MyContract { function someFunction() public { vm.warp(1000); // Invalid: called from within a contract }}
Running your tests against a fork of Abstract testnet or mainnet allows you to test your contracts in a real environment before deploying to Abstract.This is especially useful for testing contracts that interact with other contracts on Abstract such as those listed on the Deployed Contracts page.To run your tests against a fork of Abstract testnet or mainnet, you can use the following command:
forge test --zksync --fork-url https://api.testnet.abs.xyz
Anvil-zksync comes installed with Foundry, and is a tool that allows you
to instantiate a local node for testing purposes.Run the following command to start the local node:
anvil-zksync
Then run your tests on the local node by running the following command:
forge test --zksync --fork-url http://localhost:8011