Forge tests are located in files ending with .t.sol:
// SPDX-License-Identifier: MITpragma solidity ^0.8.25;import {Test} from "forge-std/Test.sol";import {PermissionedRegistry} from "src/registry/PermissionedRegistry.sol";import {RegistryRolesLib} from "src/registry/libraries/RegistryRolesLib.sol";contract PermissionedRegistryTest is Test { PermissionedRegistry public registry; address public owner = address(1); address public user = address(2); function setUp() public { // Setup runs before each test registry = new PermissionedRegistry( hcaFactory, metadata, owner, RegistryRolesLib.ALL_ROLES ); } function testRegisterName() public { vm.prank(owner); uint256 tokenId = registry.register( "test", user, address(0), address(0), 0, type(uint64).max ); assertEq(registry.ownerOf(tokenId), user); }}
Never use vm.expectEmit() for event testing. Always use vm.recordLogs() and verify logs manually.
function testEventEmission() public { vm.recordLogs(); // Perform action that emits events registry.register("test", user, address(0), address(0), 0, type(uint64).max); // Get and verify logs Vm.Log[] memory logs = vm.getRecordedLogs(); assertEq(logs.length, 2); // Transfer + NewSubname // Verify specific event data assertEq(logs[1].topics[0], keccak256("NewSubname(uint256,string)"));}
When using vm.prank and vm.expectRevert together, always place vm.expectRevertbeforevm.prank.
// Correct orderfunction testUnauthorizedAccess() public { vm.expectRevert(abi.encodeWithSignature("Unauthorized()")); vm.prank(user); registry.adminFunction();}// Wrong order - will failfunction testUnauthorizedAccessWrong() public { vm.prank(user); // Don't do this vm.expectRevert(abi.encodeWithSignature("Unauthorized()")); registry.adminFunction();}
Always use constants defined in source contracts instead of hardcoding values in tests.
import {RegistryRolesLib} from "src/registry/libraries/RegistryRolesLib.sol";// Good - uses defined constantfunction testGrantRole() public { registry.grantRoles(tokenId, RegistryRolesLib.ROLE_SET_RESOLVER, user);}// Bad - hardcoded valuefunction testGrantRoleBad() public { registry.grantRoles(tokenId, 1 << 12, user); // Don't do this}