如何正确导入@nomicfoundation/hardhat-chai-matchers 到 hardhat 项目中?

问题描述 投票:0回答:0

按照说明, 我把这个放在

hardhat.config.ts
:

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

在我的测试中我有:

expect(maxCount).to.equal(64);

其中

maxCount
是来自 ethers.js 的 BigNumber,而
64
是常规的 Javascript/Typescript 数字....但我收到此错误:

AssertionError: expected [ BigNumber { value: "64" } ] to equal 64

...这意味着 hardhat chai matcher 没有启动, 因为它应该通过执行来处理 BigNumber 转换自动作为断言的一部分。

这让我觉得我导入这个模块的方式可能不正确。 什么是正确的方法?

技术上,“使用”部分 的文档将其声明为 CommonJS

require

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

因为我的安全帽配置(和项目的其余部分)是在 Typescript 中, 我用这个代替:

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

更多详情:

这是我来自

package.json
的开发依赖:

  "devDependencies": {
    "@nomicfoundation/hardhat-chai-matchers": "1.0.6",
    "@nomicfoundation/hardhat-toolbox": "2.0.2",
    "@types/chai": "4.3.4",
    "@types/fs-extra": "11.0.1",
    "@types/mocha": "10.0.1",
    "@types/node": "18.14.6",
    "fs-extra": "11.1.0",
    "hardhat": "2.13.0",
    "ts-node": "10.9.1",
    "typescript": "4.9.5"
  }

这是我的运行时间:

$ node -v
v16.13.1
$ npm -v
8.1.2

编辑

为了调试,我手动编辑了

node_modules/@nomicfoundation/hardhat-chai-matchers/src/internal/bigNumber.ts
在几个地方添加
console.log
语句。

function overwriteBigNumberFunction(
  functionName: Methods,
  readableName: string,
  readableNegativeName: string,
  _super: (...args: any[]) => any,
  chaiUtils: Chai.ChaiUtils
) {
  console.log('hh chai matcher overwriteBigNumberFunction');
  /* ... */
    } else if (isBigNumber(expectedFlag) || isBigNumber(actualArg)) {
      console.log('hh chai matcher bignit');
  /* ... */

然后运行

npx hardhat console
。 以上日志语句均未输出, 让我进一步相信我导入的方式是不正确的。

typescript chai ethers.js hardhat
© www.soinside.com 2019 - 2024. All rights reserved.