javascript - 将 web3.js 与 Jest 一起使用时出错

标签 javascript node.js jestjs web3js

当我运行以下使用 web3.js 的 Jest 测试时包裹

const Web3 = require("web3");

test("Web3 version", function()
{
    expect(Web3.version).toEqual("1.0.0-beta.23");
});

出现以下错误

Cannot find module './build/Release/scrypt' from 'index.js'

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)

web3 似乎已正确安装,因为以下内容正确输出 Web3 version = 1.0.0-beta.23

const Web3 = require("web3");

console.log("Web3 version = " + Web3.version);

我是 Jest 的新手(今天第一次使用它)所以我不确定问题是我的 Jest 安装/设置还是 web3.js。

我在 GitHub 上创建了一个简单的项目来重现这个问题 https://github.com/naddison36/web3-jest

我的机器运行的是 Mac OS X 10.12.6、node v6.10.3 和 npm 3.10.10。 测试项目使用的是 web3.js 版本 1.0.0-beta.23 和 Jest 21.2.1

最佳答案

在您的 package.json 中,将“node”添加到 jest -> moduleFileExtensions 应该可以修复它。

{... "jest": { "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", "moduleFileExtensions": [ "ts", "tsx", "js", "jsx", "json", "node" ] } }

引用:https://facebook.github.io/jest/docs/en/configuration.html#modulefileextensions-array-string

关于javascript - 将 web3.js 与 Jest 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46672532/

相关文章:

javascript - 您如何编写一个按钮,使其在单击时可以使 iframe 变为全屏?

node.js - '#' 在 Mocha 中有特殊含义吗?

javascript - 用 TypeScript 开 Jest

node.js - 如何在 Mongoose 中为属性设置自定义 setter ?

javascript - HTTP put请求在node和mongodb中

reactjs - 开 Jest : Test useNavigate hook in function component

javascript - enzyme "containsMatchingElement"和测试代码中缺少 PropTypes

javascript - 为什么两个不同的键在一个对象中相互覆盖?

javascript - 从对象将值设置为变量后无法获取值

javascript - onchange 输入和设置状态不起作用?