Skip to content
This repository was archived by the owner on Nov 15, 2021. It is now read-only.
This repository was archived by the owner on Nov 15, 2021. It is now read-only.

Jest: Error: socket hang up #130

@abcoathup

Description

@abcoathup

Using Jest as the test runner, test fails with Error: socket hang up

Reported in the community forum: https://forum.openzeppelin.com/t/error-invalid-json-rpc-response-when-testing-erc20-token-based-on-preset-with-jest-and-openzeppelin-test-environment/3373

I can reproduce with the following steps:

Using a contract based on mock ownable:

// contracts/MyContract.sol

pragma solidity ^0.6.0;

import "@openzeppelin/contracts/access/Ownable.sol";

contract MyContract is Ownable { }

Using Ownable.test.js from OpenZeppelin Contracts modified for Jest

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

const Ownable = contract.fromArtifact('MyContract');

let ownable;

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

  beforeEach(async function () {
    ownable = await Ownable.new({ from: owner });
  });

  it('should have an owner', async function () {
    expect(await ownable.owner()).toEqual(owner);
  });

  it('changes owner after transfer', async function () {
    const receipt = await ownable.transferOwnership(other, { from: owner });
    expectEvent(receipt, 'OwnershipTransferred');

    expect(await ownable.owner()).toEqual(other);
  });

  it('should prevent non-owners from transferring', async function () {
    await expectRevert(
      ownable.transferOwnership(other, { from: other }),
      'Ownable: caller is not the owner'
    );
  });

  it('should guard ownership against stuck state', async function () {
    await expectRevert(
      ownable.transferOwnership(ZERO_ADDRESS, { from: owner }),
      'Ownable: new owner is the zero address'
    );
  });

  it('loses owner after renouncement', async function () {
    const receipt = await ownable.renounceOwnership({ from: owner });
    expectEvent(receipt, 'OwnershipTransferred');

    expect(await ownable.owner()).toEqual(ZERO_ADDRESS);
  });

  it('should prevent non-owners from renouncement', async function () {
    await expectRevert(
      ownable.renounceOwnership({ from: other }),
      'Ownable: caller is not the owner'
    );
  });
});

When run with Jest gives the following error:

$ npx jest ./test
  console.error
    Error: Error: socket hang up
        at Object.dispatchError (/home/abcoathup/projects/forum/testjest/node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
        at Request.client.on.err (/home/abcoathup/projects/forum/testjest/node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:654:18)
        at Request.emit (events.js:203:15)
        at Request.onRequestError (/home/abcoathup/projects/forum/testjest/node_modules/request/request.js:877:8)
        at ClientRequest.emit (events.js:198:13)
        at Socket.socketOnEnd (_http_client.js:435:9)
        at Socket.emit (events.js:203:15)
        at endReadableNT (_stream_readable.js:1145:12)
        at process._tickCallback (internal/process/next_tick.js:63:19) undefined

      at VirtualConsole.on.e (node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)

 FAIL  test/Ownable.test.js (23.375 s)
  Ownable
    ✓ should have an owner (4381 ms)
    ✓ changes owner after transfer (195 ms)
    ✓ should prevent non-owners from transferring (8396 ms)
    ✕ should guard ownership against stuck state (2550 ms)
    ✓ loses owner after renouncement (189 ms)
    ✓ should prevent non-owners from renouncement (154 ms)

  ● Ownable › should guard ownership against stuck state

    Invalid JSON RPC response: ""

      18 |   });
      19 |
    > 20 |   it('changes owner after transfer', async function () {
         |                                ^
      21 |     const receipt = await ownable.transferOwnership(other, { from: owner });
      22 |     expectEvent(receipt, 'OwnershipTransferred');
      23 |

      at Object.<anonymous> (test/Ownable.test.js:20:32)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 5 passed, 6 total
Snapshots:   0 total
Time:        23.845 s, estimated 25 s
Ran all test suites matching /.\/test/i.

Environment:
Windows 10 WSL2

$ npx truffle version
Truffle v5.1.35 (core: 5.1.35)
Solidity - 0.6.12 (solc-js)
Node v10.21.0
Web3.js v1.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions