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)
// 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)
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’); ).
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)
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.