Error: Invalid JSON RPC response: "" with Jest and OpenZeppelin Test Environment

I’m testing the following erc20 token based on the OpenZeppelin ERC20 preset:

I’m using the OpenZeppelin Test Environment, Test Helpers and Jest as test runner, following the docs:
https://docs.openzeppelin.com/test-environment/0.1/

I wrote some tests based on the erc20 preset tests:

When I try to test the contract with the following command:

npm test

I get the following Error:

    Error: Error: socket hang up
    ...
    Invalid JSON RPC response: ""

When trying to comment the specific test, I get the same error with the following error and so on.
I’ve tried also to remove describe and set a time out for jest:

jest.setTimeout(60 * 5 * 1000);

I get the same error.

I think it could be something related to a specific dependency, maybe inside the open zeppelin test environment?

Could you help me with that?

Thanks!
:computer: Environment

openzeppelin/test-environment = v0.1.4
openzeppelin/test-helpers = v0.5.6
jest = v26.1.0
jest-matcher-utils = v26.1.0
openzeppelin/contracts v3.1.0
truffle = v5.1.35

:memo:Details

:1234: Code to reproduce

1 Like

Hi @naszam,

I’m sorry that you are having this issue.

I had exactly the same experience using your repository with Jest with Invalid JSON RPC response: "".

I didn’t see anything obvious with your tests (though I am not familiar with Jest).

I converted the tests to use Mocha as the test runner and didn’t have any issues.

I even ran in parallel.

$ npx mocha --exit --recursive --parallel


  WFIL
    ✓ the deployer is the owner
    ✓ owner has the default admin role (68ms)
    ✓ owner has the minter role (42ms)
    ✓ owner has the pauser role (43ms)
    ✓ minter and pauser role admin is the default admin
    minting
      ✓ owner can mint tokens (57ms)
      ✓ other accounts cannot mint tokens (93ms)
    pausing
      ✓ owner can pause (65ms)
      ✓ owner can unpause (105ms)
      ✓ cannot mint while paused (82ms)
      ✓ other accounts cannot pause (43ms)
    burning
      ✓ holders can burn their tokens (112ms)


  12 passing (3s)

Test converted to be used with mocha

// based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/test/presets/ERC20PresetMinterPauser.test.js

// test/WFIL.test.js

const { accounts, contract, web3 } = require('@openzeppelin/test-environment');

const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { ZERO_ADDRESS } = constants;

const { expect } = require('chai');

const WFIL = contract.fromArtifact('WFIL');

let wfil;

describe('WFIL', function () {
const [ owner, other ] = accounts;

const name = 'Wrapped Filecoin';
const symbol = 'WFIL';

const amount = new BN('5000');

const DEFAULT_ADMIN_ROLE = '0x0000000000000000000000000000000000000000000000000000000000000000';
const MINTER_ROLE = web3.utils.soliditySha3('MINTER_ROLE');
const PAUSER_ROLE = web3.utils.soliditySha3('PAUSER_ROLE');

  beforeEach(async function () {
    wfil = await WFIL.new({ from: owner })
  });

  it('the deployer is the owner', async function () {
    expect(await wfil.owner()).to.equal(owner);
  });

  it('owner has the default admin role', async function () {
    expect(await wfil.getRoleMemberCount(DEFAULT_ADMIN_ROLE)).to.be.bignumber.equal(new BN(1));
    expect(await wfil.getRoleMember(DEFAULT_ADMIN_ROLE, 0)).to.equal(owner);
  });

  it('owner has the minter role', async function () {
    expect(await wfil.getRoleMemberCount(MINTER_ROLE)).to.be.bignumber.equal(new BN(1));
    expect(await wfil.getRoleMember(MINTER_ROLE, 0)).to.equal(owner);
  });

  it('owner has the pauser role', async function () {
    expect(await wfil.getRoleMemberCount(PAUSER_ROLE)).to.be.bignumber.equal(new BN(1));
    expect(await wfil.getRoleMember(PAUSER_ROLE, 0)).to.equal(owner);
  });

  it('minter and pauser role admin is the default admin', async function () {
    expect(await wfil.getRoleAdmin(MINTER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
    expect(await wfil.getRoleAdmin(PAUSER_ROLE)).to.equal(DEFAULT_ADMIN_ROLE);
  });

  describe('minting', function () {
    it('owner can mint tokens', async function () {
      const receipt = await wfil.mint(other, amount, { from: owner });
      expectEvent(receipt, 'Transfer', { from: ZERO_ADDRESS, to: other, value: amount });

      expect(await wfil.balanceOf(other)).to.be.bignumber.equal(amount);
    });

    it('other accounts cannot mint tokens', async function () {
      await expectRevert(wfil.mint(other, amount, { from: other }),'WFIL: must have minter role to mint');
    });
  });

  describe('pausing', function () {
      it('owner can pause', async function () {
        const receipt = await wfil.pause({ from: owner });
        expectEvent(receipt, 'Paused', { account: owner });

        expect(await wfil.paused()).to.equal(true);
      });

      it('owner can unpause', async function () {
        await wfil.pause({ from: owner });

        const receipt = await wfil.unpause({ from: owner });
        expectEvent(receipt, 'Unpaused', { account: owner });

        expect(await wfil.paused()).to.equal(false);
      });

      it('cannot mint while paused', async function () {
        await wfil.pause({ from: owner });

        await expectRevert(
          wfil.mint(other, amount, { from: owner }),
          'ERC20Pausable: token transfer while paused'
        );
      });

      it('other accounts cannot pause', async function () {
        await expectRevert(wfil.pause({ from: other }), 'WFIL: must have pauser role to pause');
      });
  });

    describe('burning', function () {
      it('holders can burn their tokens', async function () {
        await wfil.mint(other, amount, { from: owner });

        const receipt = await wfil.burn(amount.subn(1), { from: other });
        expectEvent(receipt, 'Transfer', { from: other, to: ZERO_ADDRESS, value: amount.subn(1) });

        expect(await wfil.balanceOf(other)).to.be.bignumber.equal(new BN(1));
      });
    });

});

I reproduced using a test cloned from OpenZeppelin Contracts and created an Issue: https://github.com/OpenZeppelin/openzeppelin-test-environment/issues/130

As a work around I suggest using Mocha as your test runner.

1 Like

I will migrate to mocha for now.

Anyway, I’m curious to see what is causing the issue.

Thanks!

1 Like

Hi @naszam,

I tested using an integration test and jest@24.9.0 works but jest@25.0.0 fails

As a work around you could try using jest@24.9.0 otherwise use mocha.

1 Like

Oh! Ok. Thanks!

I’ll stick with mocha (in parallel) for now.

Thanks!

1 Like

Got bumped into the same error. I am using Mocha though


  it("General user can not pause the contract", async function () {
    await expectRevert(
      contract.connect(addr1).pause(),
      "Ownable: caller is not the owner"
    );
    expect(await contract.paused()).to.equal(false);
  });

I don't see anything obvious here.