I'm using running hardhat tests. When I add console logs to the test cases, nothing actually gets outputted to console. Why's that?
4 Answers
When people report this, 99% the time it means that they are running npx hardhat node in one terminal and npx hardhat test --network localhost in the other, and they expect to see the logs in the second terminal, but it's actually shown in the first one. This happens because the output of console.log is shown in the process where the Hardhat Network is running.
This doesn't happen when you run npx hardhat test (without a --network param), because that uses the in-process hardhat network. That is, the test task and the network run in the same process, and therefore the output is shown in the same terminal.
I have an explainer about this that goes more in-depth about what happens in these different scenarios, and others, under the hood.
- 2,862
- 17
- 27
In case anyone else happens to come across this, I happened to run across this because I was using ganache for my network instead of hardhat node. Switching my localhost network to hardhat node instead of using ganache-cli revealed the console.logs
- 41
- 2
There's limitations to what you can console.log out..
e.g, string, boolean, address.
but for example, you would not be able to console.log a UniswapV3Pool object
if you take a look at hardhat/console.sol file, you can verify data objects are accepted. I think you are limited to a max of 4 data objects per console.log.. e.,g console.log(1, "a", address(this), 4)
If not, the solidity compiler will throw a fit.
- 11
- 1
I had the same issue recently and found a solution not listed here. Change your test command to npx hardhat test --verbose.
console.logshould be outputting text to the console the tests are running in. Could you edit the question to post the output of your tests, and maybe a code block from the tests with aconsole.login it? – Linum Labs Apr 25 '21 at 06:46