Truffle offers two ways to test smart contracts: either using test files written as Solidity contracts, or Javascript/Mocha.



Are there recommendations on when to choose between the two? Do you stick to one or do you use both but for different purposes?


I use exclusively javascript (for now), because:

  • I am more comfortable with the nodejs dev environment and tools than with solidity
  • I like the debugging messages I can put in js (you could probably fire some events that would act as logging things)
  • I haven't written complex smart contracts that need to interact with one another, so that I should test their interactions
  • I can take nodejs snippets from the tests, and use them in the web UI almost without any modification.

I'm not saying my approach is the best and everybody should use it and testing in solidity is useless. I rather say I'm lazy, didn't had enough time to learn and get to feel comfortable using solidity for testing and nodejs testing is good enough for me for now.

If you have the time, I'd recommend you to get comfortable using both ways.

  • Hmm, based on what you just said, I suppose we would have to use Solidity when testing interactions between contracts?
    – Bastien
    Nov 12 '18 at 14:38
  • I think that's the best use case for it. Of course, it can be used to test single contracts too, but web3js did a very good job until now for me. Nov 12 '18 at 14:43

I agree with Tudor, I use javascript as well. Would like to add:

  • tests in solidity truffle does some extra transactions which is quite difficult to track
  • tests in javascript much comfortable to debug

Testing your smart contracts with JavaScript

And the result:

Contract: FundRaise ✓ has an owner (38ms ) ✓ is able to accept funds (234ms )
2 passing (473ms )

Handsomely. This is a process that is systematically carried out to cover smart contracts with a test written in JavaScript. Just keep doing it until all the conditions of a smart contract are fulfilled.

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.