当我运行以下使用 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/