简体   繁体   中英

Error when using web3.js with Jest

When I run the following Jest test which uses the web3.js package

const Web3 = require("web3");

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

I get the following error

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

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

web3 appears to be installed correctly as the following correctly outputs Web3 version = 1.0.0-beta.23

const Web3 = require("web3");

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

I'm new to Jest (used it for the first time today) so I'm not sure if the problem is my Jest installation/setup or web3.js.

I've created a simple project on GitHub to replicate the problem https://github.com/naddison36/web3-jest

My machine is running Mac OS X 10.12.6 , node v6.10.3 and npm 3.10.10 . The test project is using web3.js version 1.0.0-beta.23 and Jest 21.2.1

In your package.json add "node" to jest -> moduleFileExtensions should fix it.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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