Invalid JSON RPC response when using Test Helpers expectRevert OpenZeppelin Test Environment

The problem surfaces as:

Error: Invalid JSON RPC response: "" at Object.InvalidResponse (node_modules/web3/node_modules/web3-core-helpers/src/errors.js:42:16) at XMLHttpRequest.request.onreadystatechange (node_modules/web3/node_modules/web3-providers-http/src/index.js:106:32) at XMLHttpRequestEventTarget.dispatchEvent (node_modules/xhr2-cookies/xml-http-request-event-target.ts:44:13) at XMLHttpRequest._setReadyState (node_modules/xhr2-cookies/xml-http-request.ts:219:8) at XMLHttpRequest._onHttpRequestError (node_modules/xhr2-cookies/xml-http-request.ts:379:8) at ClientRequest.<anonymous> (node_modules/xhr2-cookies/xml-http-request.ts:266:37) at Socket.socketErrorListener (_http_client.js:432:9) at emitErrorNT (internal/streams/destroy.js:84:8) at processTicksAndRejections (internal/process/task_queues.js:84:21)

It works with running ganache-cli in another process but not with the built-in ganache (… a local ganache-powered blockchain with unlocked accounts will be spun up, … as in docs -> Overview)

Thanks for helping!

1 Like

Hi @MichaelKashofer,

Welcome to the community :wave:

I am sorry that you are having this issue. Unfortunately I wasn’t able to reproduce.

I am running on WSL2 on Windows 10 with node v10:

$ npx truffle version
Truffle v5.1.41 (core: 5.1.41)
Solidity - 0.6.12 (solc-js)
Node v10.22.1
Web3.js v1.2.1

What operating system and version of node are you using?


This is what I did to try to reproduce:

Using the setup described in the Learn guides: https://docs.openzeppelin.com/learn/writing-automated-tests#test-environment

I ran the following test on the Ownable Box contract: https://docs.openzeppelin.com/learn/developing-smart-contracts#ownable-box-contract

Box.test.js

// test/Box.test.js

// Load dependencies
const { accounts, contract } = require('@openzeppelin/test-environment');
const { expectRevert, BN } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

// Load compiled artifacts
const Box = contract.fromArtifact('Box');

// Start test block
describe('Box', function () {
  const [ owner, other ] = accounts;

  beforeEach(async function () {
    // Deploy a new Box contract for each test
    this.box = await Box.new({ from: owner });
  });

  it('non owner cannot store a value', async function () {
    // Test a transaction reverts
    await expectRevert(
      this.box.store(new BN(23), { from: other }),
      'Ownable: caller is not the owner'
    );
  });
});

Run Test

$ npm test

> learnt@1.0.0 test /home/abcoathup/projects/learn/learnt
> mocha --exit --recursive



  Box
    ✓ non owner cannot store a value (98ms)


  1 passing (452ms)

Hi @MichaelKashofer,

I wanted to check if you were still having issues?

When (re)building my test from your code I had some issues at first, but as I understand now (I am new to JS), the main one was that in my code I used require() with a local folder ( require(’…/openzeppelin/test-helpers’); vs. require(’@openzeppelin/test-helpers’); ).

Thank you very much for your help!

1 Like

Hi @MichaelKashofer,

Glad to here that you were able to resolve.

Also got the same issue, OP's solution doesn't work for me.

Error: Invalid JSON RPC response: ""
 at Object.InvalidResponse (node_modules/web3/node_modules/web3-core-helpers/lib/errors.js:43:16)
      at XMLHttpRequest.request.onreadystatechange (node_modules/web3/node_modules/web3-providers-http/lib/index.js:95:32)
      at XMLHttpRequestEventTarget.dispatchEvent (node_modules/xhr2-cookies/xml-http-request-event-target.ts:44:13)
      at XMLHttpRequest._setReadyState (node_modules/xhr2-cookies/xml-http-request.ts:219:8)
      at XMLHttpRequest._onHttpRequestError (node_modules/xhr2-cookies/xml-http-request.ts:379:8)
      at ClientRequest.<anonymous> (node_modules/xhr2-cookies/xml-http-request.ts:266:37)
      at ClientRequest.emit (node:events:390:28)
      at Socket.socketErrorListener (node:_http_client:447:9)
      at Socket.emit (node:events:390:28)
      at emitErrorNT (node:internal/streams/destroy:164:8)
      at emitErrorCloseNT (node:internal/streams/destroy:129:3)
      at processTicksAndRejections (node:internal/process/task_queues:83:21)

bump, same issue here @abcoathup

i can't speak for others but it seems to be tied to hardhat and maybe not mocha

I was having this error.

I was importing const { BN } = require('@openzeppelin/test-helpers'); on the migration file and on the test .js. Once I removed the import from the migration file, everything worked. Have no idea why.

please run the command npx hardhat node before testing