# Hardhat 3 本地测试怎么隔离？验证边界输入与失败回滚

整理：链小知（AI 辅助）
资料核对日期：2026-10-11
引用地址：https://lianxiaozhi.com/tools/guides/hardhat3-isolated-local-solidity-test
难度：进阶
核验方式：官方文档整理，未执行真实账户操作

在独立 Hardhat 3 项目里使用内建 Solidity 测试，检查合法输入、拒绝分支与状态保留，并区分 EVM isolate 和外部网络隔离。

## 使用工具
- [Hardhat 3](https://lianxiaozhi.com/tools/hardhat) · [官方入口](https://hardhat.org/)

## 前置条件
- 已有可用 Hardhat 3 项目和兼容运行环境；初始化与安装按当前官方 Getting Started 完成。
- 使用新练习目录，检查配置和脚本不含公共 RPC、生产账户或 fork URL。此教程为文档核验，未替读者执行编译测试。

## 1. 核对版本与独立测试范围

在练习目录查看项目 Hardhat 版本。下文使用 v3 内建 Solidity 测试，不把 v2 的插件配置和网络写法直接混入。

查看 hardhat.config.ts、测试文件和自定义任务；仅保留本地练习需要的设置。若有 http 网络、forking、读入私钥或测试里发外部请求，先确定隔离方式，不能仅凭命令名含 test 判定无外部访问。

核对结果：项目版本为 v3，练习范围与网络依赖已核对。
依据：[Hardhat 3：初始化与项目结构](https://hardhat.org/docs/getting-started)
依据：[Hardhat 3：本地网络、isolate 与 FFI 配置](https://hardhat.org/docs/reference/configuration)

## 2. 设置本地测试配置

在独立练习项目使用最小配置示例：
import { defineConfig } from "hardhat/config";
export default defineConfig({
  solidity: "0.8.28",
  test: { solidity: { isolate: true, ffi: false } }
});

isolate 让顶层调用在单独的交易/EVM 上下文执行，帮助观察交易行为；它不是网络防火墙。Solidity 测试默认环境模拟 Ethereum，仍需保证项目没有外部请求或 fork 依赖。编译器下载也不等于连接生产链。

核对结果：配置含本地测试选项，没有公共网络、账号或 fork 设置。
依据：[Hardhat 3：本地网络、isolate 与 FFI 配置](https://hardhat.org/docs/reference/configuration)
依据：[Hardhat 3：Solidity 测试发现、运行与过滤](https://hardhat.org/docs/guides/testing/using-solidity)

## 3. 保存完整的自编测试例子

在 test/BoundedValue.t.sol 保存下列例子，不依赖外部测试库：
pragma solidity ^0.8.28;
contract BoundedValue {
    uint256 public value;
    function set(uint256 next) external {
        require(next < 10, "limit");
        value = next;
    }
}
contract BoundedValueTest {
    function testBoundaryAndRollback() public {
        BoundedValue target = new BoundedValue();
        target.set(9);
        require(target.value() == 9, "valid input");
        bool rejected;
        try target.set(10) { rejected = false; }
        catch { rejected = true; }
        require(rejected, "must reject 10");
        require(target.value() == 9, "rollback state");
    }
}

测试以函数名 test 开头，放在默认 test 目录，验证边界内 9 与边界外 10。代码为本站原创，仅覆盖这段简单逻辑。

核对结果：文件位置与测试命名匹配 Hardhat 3 默认发现规则。
依据：[Hardhat 3：Solidity 测试发现、运行与过滤](https://hardhat.org/docs/guides/testing/using-solidity)

## 4. 运行本例并核对发现数量

运行 npx hardhat test solidity test/BoundedValue.t.sol。项目尚未安装依赖时先按前置步骤准备，避免误在网站仓库临时安装框架。

确认输出列出了 BoundedValueTest 和 testBoundaryAndRollback。零测试、只跑默认 Counter 示例或只有编译成功，都不能标记本例行为通过。

核对结果：本例被实际执行，合法输入、拒绝和保留状态三项断言通过。
依据：[Hardhat 3：Solidity 测试发现、运行与过滤](https://hardhat.org/docs/guides/testing/using-solidity)

## 5. 制造一次可预期的断言失败

临时把最后一项 target.value() == 9 改成 == 8，再运行同一文件。应看到 rollback state 的失败信息。

如果仍通过，检查保存位置、实际筛选文件和缓存输出。随后恢复 9 并重新运行，保留失败与恢复后成功的结果，验证测试会发现错误。

核对结果：受控错误会失败，恢复正确预期后通过。
依据：[Hardhat 3：Solidity 测试发现、运行与过滤](https://hardhat.org/docs/guides/testing/using-solidity)

## 6. 说明通过结果能证明什么

记录版本、编译器、配置和测试名称。isolate 解决调用上下文问题，本例不覆盖公共链共识、资产兼容、预言机或真实账户权限。

如果以后需要 fork 或 TypeScript 集成测试，重新列出网络、数据与脚本依赖。不能将本地例子通过写成已部署合约审计通过，也不需要添加真实钱包来补这个示例。

核对结果：验证报告保留具体输入与覆盖范围，没有扩大成主网保证。
依据：[Hardhat 3：本地网络、isolate 与 FFI 配置](https://hardhat.org/docs/reference/configuration)
依据：[Hardhat 3：Solidity 测试发现、运行与过滤](https://hardhat.org/docs/guides/testing/using-solidity)

## 常见错误
- 混用 Hardhat 2 与 3 配置。
- 将 isolate 当成自动阻断所有网络访问。
- 只有编译通过或零测试就报告成功。
- 为通过删除拒绝断言，或在练习中加入生产账号。

## 完成检查
- 本例测试被发现并覆盖 9/10 两种输入。
- 故意改错预期能得到失败，恢复后通过。
- 项目外部依赖与本地验证边界明确。

## 相关知识
- [Gas 怎么计算？交易失败为什么仍可能扣费](https://lianxiaozhi.com/learn/gas-and-failed-transactions)

## 官方依据
- [Hardhat 3：初始化与项目结构](https://hardhat.org/docs/getting-started)
- [Hardhat 3：Solidity 测试发现、运行与过滤](https://hardhat.org/docs/guides/testing/using-solidity)
- [Hardhat 3：本地网络、isolate 与 FFI 配置](https://hardhat.org/docs/reference/configuration)
