Truffle: `truffle test` hangs if no `networks` are specified and Ganache is running on port `7545`

Created on 2 May 2019  Â·  17Comments  Â·  Source: trufflesuite/truffle

From a quick look, I believe https://github.com/trufflesuite/truffle/blob/cbd741b40696d6f7f6053ae007e1fdfb22483237/packages/truffle-core/lib/develop.js#L23-L26 is part of the issue.

If the child process can't start ganache then when it rejects (here: https://github.com/trufflesuite/truffle/blob/cbd741b40696d6f7f6053ae007e1fdfb22483237/packages/truffle-core/chain.js#L237) truffle never gets notified of the truffle.ready event (here: https://github.com/trufflesuite/truffle/blob/cbd741b40696d6f7f6053ae007e1fdfb22483237/packages/truffle-core/chain.js#L249)

Also, if the chain.js process crashes truffle doesn't seem to be listening. Might need to listen to the close event: https://nodejs.org/api/child_process.html#child_process_event_close

I don't seen any harm in using a different port for truffle's internal ganache server instance, but I don't have much experience with using truffle myself. For reliability you could (should?) try to start the ganache-core server on a random port, if it fails due to the port already being bound, try again on another random port, and maybe a few more tries before failing.

Alternatively, you could do what truffle migrate does and just (try to) use the already bound 7545 port.

To get a big speed boost you could experiement with running ganache-core as a provider (Ganache.provider(options) instead of Ganache.server(options).

Let me know if you have ganache-related questions!

error better priority3 🔧 truffle test

Most helpful comment

I face a similar issue when I download a fresh instance of MetaCoin box. Truffle compile, truffle migrate works (looks like if Ganache is running, it detects Ganache anyway and deploys successfully), but truffle test just hangs. I was unable to understand why test was hanging, then I realized that in truffle-config.js, there was no network specified as by default all lines are commented out. Once I uncommented the "development" section, truffle test succeeded!

It would be good if truffle test command could check and warn that there is no network specified.

All 17 comments

@davidmurdoch Is this still a relevant issue?

Yes. I just tested with truffle v5.0.18

@davidmurdoch I don't seem able to reproduce your specific error. If I fire up Ganache (Version 2.0.1 (2.0.1.631)) using the default settings and I use latest truffle to run truffle test on a fresh metacoin box (completely commented out truffle config), I see:

Error: The network id specified in the truffle config (4447) does not match the one returned by the network (5777).  Ensure that both the network and the provider are properly configured.
    at Object.detectAndSetNetworkId (/Users/cruzmolina/.npm/_npx/29227/lib/node_modules/truffle/build/webpack:/packages/truffle-core/lib/environment.js:100:1)
    at process.internalTickCallback (internal/process/next_tick.js:77:7)
Truffle v5.0.18 (core: 5.0.18)
Node v11.4.0

If I change the Ganache network id to 4447, I get no errors, the tests pass, and tx's populate in the Ganache dashboard.

Start up Ganache UI, make sure it is running on port 7545 then run:

mkdir test && cd test && truffle init && truffle test

The init box doesn't have any tests, but otherwise the behavior locally appears to be the same as my above comment when running those commands.

If you don't have Ganache UI running on port 7545 and you run truffle test on an the init box it completes as expected:

david @ ~/work/teams/test (develop)
└─ $ ▶ truffle test

Compiling your contracts...
===========================
> Compiling ./contracts/Migrations.sol
> Artifacts written to /tmp/test-119416-20248-4z8y8d.rzlz9
> Compiled successfully using:
   - solc: 0.5.0+commit.1d4f565a.Emscripten.clang



  0 passing (0ms)

Run Ganache UI on port 7545 then run mkdir test && cd test && truffle unbox metacoin && truffle test and it fails to complete (at least in a reasonable amount of time ... I've only ever waited up to a minute or so).

Huh, what OS are you using?

Ubuntu 18.04

Testing on Windows 10 and it returns the The network id specified in the truffle config (4447) error message.

The error message The network id specified in the truffle config (4447) is inaccurate though, as there is no network id specified in the truffle config (since there is no truffle config).

4447 is the default network_id truffle test uses to connectOrStart a ganache-core chain in the background (https://github.com/trufflesuite/truffle/blob/develop/packages/truffle-core/lib/commands/test.js#L153-L174).

1577 is an outstanding PR to change the default truffle test network_id to match GanacheUI's defaults.

Even if #1577 is fixed the error message would still be inaccurate in cases where the ganache network id was changed.

Good point! The default truffle test flow could probably be rewritten to detect Ganache's network_id and then start up a chain in the background if undetected. That wouldn't handle cases where Ganache's port is changed, but it's a step forward.

I face a similar issue when I download a fresh instance of MetaCoin box. Truffle compile, truffle migrate works (looks like if Ganache is running, it detects Ganache anyway and deploys successfully), but truffle test just hangs. I was unable to understand why test was hanging, then I realized that in truffle-config.js, there was no network specified as by default all lines are commented out. Once I uncommented the "development" section, truffle test succeeded!

It would be good if truffle test command could check and warn that there is no network specified.

Same issue with truffle v5.1.0

When Ganache is running on port 7545, somehow the network id is set by Truffle to 4447 yet it somehow knows that Ganache is running at 7545 and the network ids conflict. It would be a good idea to test other ports for these same symptoms.

If no network options are set when running truffle test, then Truffle attempts to start Ganache at port 7545. I think then Truffle will find the already running instance and there will be a network id clash. Here are the default settings.

I think a solution would be to by default choose a different port to lessen the chance that this clash will occur. This will lessen the chance of a clash since common (past and present) port defaults for Ganache are 7545, 8545, and 9545. If we avoid these for defaults during truffle test, it should achieve this aim.

Was this page helpful?
0 / 5 - 0 ratings