Search code examples
javascriptnode.jsjestjsweb3js

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


Solution

  • 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