I'm using Truffle and I want to increase the blocktime to mimic that of the mainnet when i run my tests.

As i understand it, Truffle has already instantiated a web3 instance that's ready to use so i tried to modify it with the following in my test file:

const ganache = require('ganache-core')
web3.setProvider(ganache.provider({ blockTime: 15 }))

However each test finishes in less than 200ms.

How would i go about changing the blocktime?


You can simply start Ganache with -b 15.

See here for more details.


The above solution is for when you start Ganache as a separate process.

In order to simulate a 15-second elpase in your tests, you can simply use this:

web3.currentProvider.send({method: "evm_increaseTime", params: [15]});
  • Firing up Ganache in a separate process with the -b 15 flag worked, now each test takes just over 15000ms. However the second solution gave me before each hook: setup contract for each test: Uncaught TypeError: callback is not a function
    – Conviley
    Mar 27 '20 at 13:19
  • @Conviley: The above works in Truffle 4.x, which relies on web3.js v0.x. It might require a different syntax if you're on Truffle 5.x, which relies on web3.js v1.x. Mar 27 '20 at 13:30
  • Okay i'm using truffle v5.1.13 so that might be the reason then.
    – Conviley
    Mar 27 '20 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.