Writing Truffle tests with async/await

The documentation and sample projects with Truffle use promise chaining to write the test code.  As someone from the C# world, coming into the JS world it was getting really confusing to keep track (as well as all the variables I’d have to declare outside of the promise chain to use later).

I did a bit of research and found a way to do async/await in Truffle. I have put up a small simple repo by taking the truffle init project sample, and then doing a straight 1:1 conversion of their promise chaining tests over to the async/await format to help illustrate the difference. I really like the async/await version, as it is cleaner to read and much more compact (as you can see in the before/after screenshots below).

You can find the repo at https://github.com/DavidBurela/TruffleAsyncTests


before – promise chaining https://github.com/DavidBurela/TruffleAsyncTests/blob/master/test/metacoin.js



after – with async/await https://github.com/DavidBurela/TruffleAsyncTests/blob/master/test/asyncmetacoin.js



A shout out to Jim McDonald for pointing me to his GitHub repo where he had implemented async tests to get me started.


Ethereum DevOps with VSTS – easier now with new Truffle installer + npx

Last year I blogged about my first attempt to wrap a DevOps flow around Ethereum development. Since then there have been improvements in the way that Truffle is deployed with npm, the npx command that comes with npm 5.3.0 onwards, VSTS now supporting hosted Linux build agents, but also my familiarity with Linux, npm & Truffle have increased. I have spent a few weeks in my spare time trying to vastly streamline and simplify getting a basic pipeline going that provides: compilation, testing, migrating of solidity contracts using Truffle.

The steps I outline below will work on any build system, such as Jenkins or Team city that can run bash commands. But I have shown VSTS as that is what I am most familiar with.

Screenshot of the final result, with VTSTS showing Truffle test results for each build.

A video where I walk through the entire process end to end.

Example configured project

I have created a repository in GitHub where I have done the below steps. So if you want to just clone it and try it out on your own build server, that will make it easier for you


1. Configure npm development packages for your Truffle project

Add Truffle, TestRPC, Mocha & Mocha JUnit plugin as DevDependencies in your packages.json. This will allow the build server to later install these packages and then execute Truffle commands.

# don't run npm init if your project already has a packages.json
npm init -y
npm install truffle ethereumjs-testrpc mocha mocha-junit-reporter --save-dev

2. Configure truffle.js

There are 2 main things that are being added here:

a) TestRPC provider as a new network, with the require at the top of the file to import it. This will allow Truffle to just spin up TestRPC just for the context of the command, and then get thrown away once the command finishes. Great for testing migrations, or running tests, and then throwing it all away.

b) Mocha test reporter definition. We leave it as “spec” so that it will display the results in the console window while developing locally. Later on the build server we can change the reporter to JUnit, and the output file is already specified here ready for that.

var TestRPC = require("ethereumjs-testrpc");

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id

// add a new network definition that will self host TestRPC
localtest: {
provider: TestRPC.provider(),

// add a section for mocha defaults
mocha: {
reporter: "spec",
reporterOptions: {
mochaFile: 'TEST-truffle.xml'


3. Build server settings

This screenshot shows what the end result looks like. It can be summarised as: get the tests, install the npm packages, configure any environment variables, run the Truffle commands, and collect the test results.


a) Get sources

Define where your code is sitting (e.g. VSTS, Github, Bitbucket, etc.)

b) npm install

The default npm install task. Will look in packages.json and install our dev dependencies so later we can run Truffle, TestRPC, etc.

c) Shell script – environment details and config

An inline script that spits out useful environment information to help debug if things go wrong.

It also importantly replaces the line in our truffle.js to change the test reporter to the console UI, to output the results into a .xml file in JUnit format

# output version details for debugging
node -v
npm -v
npx truffle version

# string replace the mocha reporter to junit output
sed -i -e 's/reporter: "spec"/reporter: "mocha-junit-reporter"/g' truffle.js


d) Shell script – truffle commands

Inline script that executes the local version of Truffle with npx. I like have all 3 lines, so if something breaks we can see where it happened.

# check the contracts compile
npx truffle compile

# check migrations execute without issue
npx truffle migrate --network localtest

# run unit tests
npx truffle test --network localtest


e) Publish test results

Default task. I have left the defaults

New releases of Truffle & TestRPC. Now easier to install.

I have mentioned previously that Truffle & TestRPC are my development tools of choice for Ethereum based development.

truffle logo

It is exciting to see that there have been new releases of both Truffle & TestRPC. I have been hanging out in the Truffle Gitter channel, and been fielding lots of questions about installation issues on Windows. As Tim has previously blogged, there have been a number of issues with installation. The installation issues were mostly due to compiling C based libraries, and requiring a number of build tools on the local machine. These issues have now been removed as Truffle & TestRPC are both now built, and then released as a completely pre-packaged release. This also means that installation times have dropped from 10 minutes, to 10 seconds! (Which means I’ll need to go back and update some of my old tutorials )



Highlights: More reliable installation. Makes it easier for everyone to jump in and start playing!



Highlights: Installs quicker. Reduced memory consumption. Ability to persist the blockchain to disk, meaning you can suspend and then resume if you like  (I’ll still be using it mostly as a throw away in memory style dev environment).


Whats next?

I’ve been waiting in suspense for these updated installers to drop for many months now. There have been a couple of projects on my backlog that had been blocked until this came out. A short list of what I’ll be working on in the near term are:

Reghack “Down Under” event roundup

Reghack was a 3 day hackfest that ran in Melbourne, Australia https://reghack.org/

The problem statement asked:
Do you have an interest in helping solve regulatory issues in the Financial Services and the Energy Sector in Australia?
How do we use RegTech to make regulatory compliance a strategic advantage that’s a win for the regulator, market participants and the consumer?

The focus of the event was to try and invigorate local innovation in the energy & financial services sectors, by allowing people to come together and explore how Blockchain could be utilised in these areas. The event was the brainchild of Chami Akmeemana https://www.linkedin.com/in/chami1/ who is a director of ConsenSys, the largest Blockchain focused consulting company in the world. Chami came to Melbourne and asked for local community support to help him organise and run the event. I was lucky enough to be tapped by Chami and invited to assist. I helped out by providing sponsorship for the meals via Microsoft, and delivered training to help upskill the community beforehand (more about that below).

The event had around 90 participants, with many more volunteers on the day. In the end 14 teams pitched their ideas which ranged from energy trading systems, ways to authenticate documents, to ways to eliminate GST during B2B transactions.
A big thanks to Chami for organising it all, and to all the volunteers that helped make the event a resounding success.


Note: The roundup of the teams pitches are at the bottom of this post.

Continue reading

How to install Truffle & TestRPC on Ubuntu or Windows 10 with “Windows subsystem for Linux”

I previously wrote how you can install Truffle & TestRPC on a windows machine, by using the Windows installers for Node & npm.
However I have found it is a much nicer experience to use the Windows Subsystem for Linux which provides a native Ubuntu shell with bash, which allows me to run all the Linux tools natively without issues. These install steps will work on an Ubuntu machine as well.

Prerequisite if running on Windows

  1. Enable Windows subsystem for Linux.

    Follow this short guide on how to enable it https://msdn.microsoft.com/en-us/commandline/wsl/install_guide


  2. After following the steps in the guide above. Simply start the Ubuntu bash shell



Install steps


# Takes a clean Ubuntu image, up to being Ethereum dev ready.
# installing npm from official repo
https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions as Ubuntu default apt-get has a very old version (NodeJS v4.1)
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash –
sudo apt-get update -y && sudo apt-get upgrade -y

# install system basics
sudo apt-get install -y build-essential nodejs

# upgrade node/npm/npx and then install dev tools
sudo npx dist-upgrade
sudo npm install -g ethereumjs-testrpc truffle

# fix up permission issues. npm5 creates these as root, so user can’t modify by default
sudo chown -R $(whoami) ~/.npm
sudo chown -R $USER:$(id -gn $USER) ~/.config


now you should be able to create a folder, go into it and run `truffle init` to create a new project template.



Additional things to do

Now that your machine has it installed, I recommend you follow my other guide to get Visual Studio code configured for Truffle development.

Ethereum DevOps with Truffle, TestRPC & Visual Studio Team Services

UPDATE 2017/09/07:
Consider this post deprecated. I have written a new much simpler guide https://davidburela.wordpress.com/2017/09/06/ethereum-devops-with-vsts-easier-now-with-new-truffle-installer-npx/

I have been working on automating the compilation and testing of Ethereum solidity contracts, via the use of Truffle. I’ve got the test results being published back into the portal, allowing me to see on each commit if my code still compiles and passes my tests.


I’m assuming you already have a Truffle project locally that you want to automate the continuous builds & testing on. Follow my tutorial on installing Truffle & TestRPC on Windows.

My final system will allow you to run “truffle test” locally to see standard test output, but will modify the test runner on the server to output it as JUnit format.

The Build system

The system uses the Visual Studio Team Services (VSTS) build engine to automate this. You can sign up for free, and get unlimited private Git repos.
You can have the code hosted on any Git provider. So either within VSTS itself, or GitHub, BitBucket, etc.

Prepare truffle.js

A pre-step is to define the  test section in the truffle.js file

mocha: {
reporter: “spec”,
reporterOptions: {
mochaFile: ‘junitresults.xml’


Create a build agent

VSTS does provide hosted build agents, which are generic and can build standard .Net projects, Xamarin, etc. But because we are going to use npm packages installed globally on the box to handle the Truffle builds

  • Create a new Windows VM (Can be your own hosted server, or Azure).
    e.g. Windows Server 2016 Datacentre edition on Azure
  • Install the VSTS build agent. Instructions at https://www.visualstudio.com/en-us/docs/build/admin/agents/v2-windows
    Note: DON’T select to run service as NT AUTHORITY\NETWORK, this will not work with TestRPC (needs to open ports).
    Run the service as another user, or NT AUTHORITY\SYSTEM
  • Install chocolatey
  • Install these chocolatey packages
    • choco install git -y
    • choco install nodejs.install –
  • Install npm packages (make sure you open a new PowerShell window so that node is in your path)
    • npm install -g npm
    • npm install -g –production windows-build-tools
    • npm install -g ethereumjs-testrpc
    • npm install -g truffle
    • npm install -g mocha
    • npm install -g mocha-junit-reporter
  • Restart the build agent so that all new paths are working

Configure VSTS build

    • Create a new variable with the path to where the npm global path is, for the user you installed the npm packages on above
      variable name: npm.path
      variable value: path to npm packages e.g. C:\Users\<user>\AppData\Roaming\npm
    • Add 7 PowerShell tasks, and configure them like this
      • Name: System version information
        #Setting environment paths
        $ENV:Path = $ENV:Path + “;” + $env:npm_path
        npm config set prefix $env:npm_path    #only needs to be set once, will update for user
        #npm list -g –depth=0
        #Display system information
        Write-Host “System version information”
        Write-Host -nonewline    “node version: ” ; node -v
        Write-Host -nonewline    “npm version: “; npm -v
        Write-Host -nonewline    “npm prefix: “;  npm prefix -g
        Write-Host -nonewline    “truffle: ” ;    truffle version
    • Name: Config transform & test clean
      # remove old test results
      rm .\junitresults.xml -ea SilentlyContinue
      # Modify the Truffle test runner to use the JUnit reporter
      Rename-Item .\truffle.js .\truffle_temp.js
      cat .\truffle_temp.js | % { $_ -replace ‘reporter: “spec”‘, ‘reporter: “mocha-junit-reporter”‘ } | Out-File -Encoding ASCII .\truffle.js
      rm .\truffle_temp.js
    • Name: Truffle build
      #Setting environment paths
      $ENV:Path = $ENV:Path + “;” + $env:npm_path
      #Truffle build
      truffle compile
    • Name: Launch TestRPC
      #Setting environment paths
      $ENV:Path = $ENV:Path + “;” + $env:npm_path
      # launch the process
      echo “launching TestRPC”
      $testrpcProcess = Start-Process testrpc -passthru
      # persist the PID to disk and display in logs
      $testrpcProcess.Id | Export-CliXml testrpcPID.xml
      cat testrpcPID.xmlimage
    • Name: Run Truffle tests
      #Setting environment paths
      $ENV:Path = $ENV:Path + “;” + $env:npm_path
      # Run the tests
      truffle test
    • Name: Shutdown TestRPC
      Other Settings: Enable “Always Run” (to make sure it is shutdown if there is an error)
      #Setting environment paths
      $ENV:Path = $ENV:Path + “;” + $env:npm_path
      # retrieve the PID and kill the entire processs tree
      cat testrpcPID.xml
      $testrpcPID = Import-CliXml testrpcPID.xml
      taskkill /pid $testrpcPID /F /T
  • Add a new Publish test result
    • Test Result Format: JUnit
      Test Result Files: junitresults.xml


Future work

Things that I would like to add in the future:

  • Figure out how to automate this on a Linux build agent (VSTS supports both Windows & Linux based build agents)
  • Automate Release Management to run “truffle migrate” to push to a Bletchley test environment

Configuring Visual Studio code for Ethereum Blockchain development

Visual Studio code is a great tool for editing Solidity smart contracts, and is available on Windows, Mac & Linux. There is a great plugin that enables Syntax highlighting, snippets, and compiling of the current contract (if you aren’t using an external tool) https://github.com/juanfranblanco/vscode-solidity/

This configuration works really well with Truffle (as shown in the final screenshot). You can read how to install Truffle on Windows in my previous post https://davidburela.wordpress.com/2016/11/18/how-to-install-truffle-testrpc-on-windows-for-blockchain-development/

Step 1: Install Visual Studio code

Easy option on Windows: Just install via https://chocolatey.org/ by using the command
choco install VisualstudioCode –y

Step 2: Install Visual Studio extensions
Go into the extensions section, then install these plugins:

  • solidity
  • Material Icon Theme


Step 3: Enable icon theme
File –> Preferences –> File Icon Themeimage

Final result: Sexy workspace