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:

I ran the following test on the Ownable Box contract:


// 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 = await{ from: owner });

  it('non owner cannot store a value', async function () {
    // Test a transaction reverts
    await expectRevert( 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

    ✓ 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.