Jest: Error: socket hang up
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
The issue occurs with the latest version of [email protected] but doesn't occur with [email protected], which is the version used in OpenZeppelin Test Environment integration test:
https://github.com/OpenZeppelin/openzeppelin-test-environment/blob/master/test-integration/jest/package.json#L14
The issue also occurs with [email protected]
I experience the same issue but when I remove the cacheDirectory: "./tmp/" from my jest.config.js I don't have the problem anymore ...here is the full config
module.exports = {
rootDir: "./tests/",
testMatch: ["**/?(*.|*-)+(spec|test).+(ts|tsx|js)"],
coverageDirectory: "./coverage/",
cacheDirectory: "./tmp/",
collectCoverage: false,
testTimeout: 300000,
};