You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行npx hardhat test始终返回'0 passing'问题求助

解决Hardhat测试显示'0 passing'的问题

针对执行npx hardhat test时无测试用例执行的问题,按以下步骤排查:

1. 确认测试文件位置与命名

确保Testing.js文件放在项目根目录的test/文件夹下,Hardhat默认仅扫描该目录下的测试文件。若文件在其他目录,执行时需显式指定路径:

npx hardhat test 你的文件路径/Testing.js

2. 修正Ethers导入方式

在Hardhat环境中,必须从hardhat包导入ethers和waffle,而非直接导入ethers包。修改导入代码:

const { expect } = require("chai");
// 替换原ethers导入方式
const { ethers, waffle } = require("hardhat");
// 删除无用的内部模块导入
// const { isCallTrace } = require("hardhat/internal/hardhat-network/stack-traces/message-trace");

3. 移除无关内部模块导入

你导入的isCallTrace是Hardhat私有内部模块,测试代码无需使用,删除该行可避免干扰测试框架执行流程。

4. 修复测试代码中的错误

变量拼写错误

beforeEach中signers2为拼写错误,修正为signer2:

[signer1, signer2] = await ethers.getSigners();

Tiers索引错误

deploy测试块中所有tiers索引均写为3,不符合逻辑,修正为对应索引:

expect(await staking.tiers(0)).to.equal(700);
expect(await staking.tiers(1)).to.equal(1000);
expect(await staking.tiers(2)).to.equal(1200);

5. 检查Hardhat配置

确保hardhat.config.js已正确配置waffle插件,且编译器版本与合约一致:

require("@nomiclabs/hardhat-waffle");

module.exports = {
  solidity: "0.8.17", // 替换为你的合约使用的Solidity版本
};

6. 重新安装依赖(可选)

若以上步骤无效,尝试删除依赖文件后重新安装:

rm -rf node_modules package-lock.json
npm install

完成以上操作后,再次执行npx hardhat test,测试用例应能正常执行。

内容的提问来源于stack exchange,提问作者Kenny

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 04:15:08