Skip to content

chore: prefer named imports over default imports in examples #1150

@0xumarkhatab

Description

@0xumarkhatab

Chapter 8 -> section 8.1 lacks import of stdError & Vm

Issue

There are multiple places where forge-std's components are used without importing them .

  1. Lack of stdError and Vm import
A good practice is to use the pattern test_Revert[If|When]_Condition in combination with the [expectRevert](https://book.getfoundry.sh/cheatcodes/expect-revert.html) cheatcode (cheatcodes are explained in greater detail in the following [section](https://book.getfoundry.sh/forge/cheatcodes.html)). Also, other testing practices can be found in the [Tutorials section](https://book.getfoundry.sh/tutorials/best-practices.html). Now, instead of using testFail, you know exactly what reverted and with which error:

    function test_CannotSubtract43() public {
        vm.expectRevert(stdError.arithmeticError);
        testNumber -= 43;
    }

Impact

Beginners get confused when stdError is not found upon compilation of smart contracts because foundry demands explicit imports

Fix

Add following lines in the docs to import used components

import {Vm} from "forge-std/Vm.sol";
import {stdError} from "forge-std/StdError.sol";

Metadata

Metadata

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions