如何用硬帽测试require语句?



我正在测试一个智能合约,其中一个函数中有一个require语句。

在过去,我只会写。

await expect(token.transfer(user.address, ethers.utils.parseEther("10")).to.be.reverted;

但是最近它停止工作了,它给了我这个错误:

Property 'reverted' does not exist on type 'Assertion'.ts(2339)

知道为什么它不能正常工作,或者我做错了什么吗?

谢谢你。

原来我没有正确导入chai。

因此,为了解决这个问题,我创建了另一个名为chai-setup.ts的文件,并在其中添加了以下代码:
import chaiModule from "chai";
import { chaiEthers } from "chai-ethers";
chaiModule.use(chaiEthers);
export = chaiModule;
然后在我的主测试文件中,我添加了这个import语句:
import { expect } from "./chai-setup"

这就解决了我的问题。

回答这个问题有点晚了,但是您可以使用hardhat chai匹配器来解决这个问题。

安装硬帽chai匹配器

yarn add --dev @nomicfoundation/hardhat-chai-matchers

现在你所需要做的就是将它导入到你的硬帽配置中:

import "@nomicfoundation/hardhat-chai-matchers";

require("@nomicfoundation/hardhat-chai-matchers")

现在你将能够使用reverted和其他东西。

最新更新