This is a demonstration of Ethereum smart-contract exploration environment combining Ganache Core (formerly known as TestRPC), ethjs, Mocha, Wallaby.js (non-free software).
Note: A macOS environment is assumed
Install with yarn install
.
If you want to see external contract loading, then dapp build
.
For extra convenience you can install entr — a generic file watcher — with brew install entr
and use yarn run build
to continuously compile contracts as their source changes.
Since tests are not automatically re-run on contract recompilation, there is extra auditory feedback reflecting
both the success or failure of the contract recompilation,
with different sounds.
(After contract recompilation we must use IntelliJ's Rerun
command when using Wallaby.js to avoid false test feedback!)
Start the test watcher and runner with yarn test --watch
.
Change something in test/chain_test.js
and save the file.
You shall enjoy the feedback within 200 milliseconds!
Finally, to push joy to its limits, configure Wallaby.js for your editor / IDE and save the effort of moving your eyes away from the source code, since the test failures appear right next to the source lines ;)
While Truffle made quite a progress on making the smart-contract creation process more accessible, it does quite a lot of magic in the background. Such magic gets in the way of utilizing such development conveniences as Wallaby.js for example, which can provide almost instantaneous feedback, directly inlined into the code editor, while working on a Mocha test suite.
web3.js
is a very versatile, configurable, feature rich library, but it has
accumulated quite some legacy code already...
Truffle builds on the (as of 2017-12-15) latest stable version of the
web3.js Ethereum RPC client,
which lacks some nice feature already present its upcoming 1.x versions,
like calling methods with the same name but different parameter lists, eg. myToken.methods['mint(uint256)'](123)
.
ethjs however is a leaner alternative to
web3.js
. After seeing even a
Metamask article
was using ethjs
in their examples, I thought I would give it a try too.
Ganache Core is an
in-memory Ethereum blockchain simulator, so it can be used as a
test double
in automated tests.
While using such a fake version of the chain can speed up tests significantly,
Ganache comes with the wonderful ability to branch its state, by providing
two, non-standard RPC operations evm_snapshot
and evm_revert
.
By doing a snapshot before each test and a revert after each test, we can
achieve cheap isolation of unit-test cases.
Truffle already does utilize
this feature of Ganache.
It is however
buried a bit deep
under its hood and it's also coupled to running the test suite through their
command-line interface or interactive console.
I haven't found any clear documentation on how to configure the
automatically managed, built-in develop
and test
chains either,
which I needed when I wanted to set different gas
settings for debugging
purposes.
I've learnt to love the instant feedback loop during development already decades ago. And by instant, I mean sub-second responses!
Back in the nineties, when I was developing for Microchip PIC microcontrollers in FlashForth, I could directly type code over a serial line to the controller for either execution or compilation and I saw its effect immediately. It was a 4 Mega, not Giga Hertz CPU with 4 kilo, not Mega word ROM and 1024 bytes of RAM!
Then in the past few years I was doing Clojure/ClojureScript development using the Cursive plugin for the IntelliJ IDE. Cursive also has a tight integration with Clojure's built-in test framework, providing instant feedback when running tests.
The Wallaby.js plugin promises to provide a similar, instantaneous testing experience as Cursive. They support other IDEs too and automatically integrate with all the major Javascript test frameworks, including Mocha.
But Truffle tests are not pure Mocha tests, because they run in an implicit
environment and defined by a magical contract
function — instead of
describe
— as a convenience, which is more of a nuisance,
since Wallaby.js can not recognize and run them...
So I set out to build a minimal example to showcase and also understand what would it take to tie all these great tools into a smooth development experience.
You can find the results in this repository.
(Testing YubiKey)