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

nad*_*son 2 javascript node.js jest web3js

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

const Web3 = require("web3");

test("Web3 version", function()
{
    expect(Web3.version).toEqual("1.0.0-beta.23");
});
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

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

  at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:191:17)
Run Code Online (Sandbox Code Playgroud)

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

const Web3 = require("web3");

console.log("Web3 version = " + Web3.version);
Run Code Online (Sandbox Code Playgroud)

我是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和Jest21.2.1

exi*_*dbq 8

在你的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