繁体   English   中英

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

[英]Error when using web3.js with Jest

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

作为解决方法,将此行require(“./ build / release / scrypt”)更改为'/node_modupt/scrypt/index.js'中的require(“scrypt”)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM