While developing a smart contract, it’s wise to avoid time-wasting tasks. One is the need to deploy in a heavy to setup environment just to test your changes.
With Convector, you can run local unit tests with a Mock ledger that you can also use to debug your code. With the latest versions of Hurley and Convector Smart Contracts it’s now even possible to debug straight from the blockchain.
What are Unit Tests
In case you haven’t heard of them, it’s a way to create some code that automatically tests your code (yeah, trust the machines 😁).
In general, you grab a unit of code that makes sense to test out (a module, a function, a procedure, a group of functions) and code a function that calls that and checks results. These tests can be run while compiling, integrating, or deploying your code, so that you can be sure that what you expected to happen still happens.
Some advantages of implementing Unit Tests
- Fast and agile development. Making refactors or even small changes to parts of your code that already worked can definitely be worrisome. With Unite Tests if you change something, you can run the unit tests to see if the software still behaves the way you want it to.
- Thinking on tests before coding can make you build better-thought code, you can think of the inputs and behavior before jumping into code. Unit tests are basic units on BDD (and TDD). Your blockchain components should not break a good engineering process or feel alien to your day-to-day.
- Find bugs early and quickly! Even before going to integration.
- Easier integration of multiple developers. Since the “contract” of the functions are reflected in the tests, if somebody breaks it, it’s easier to identify dependencies changing, hence maintaining and changing the code becomes easier with multiple individuals turning coffee into code.
- Easier documentation. Imagine you are new to the team, you want to better understand what a function is expecting and returning. With a unit test, you can pick up an idea of what is happening faster than reading the code and going through dependencies on multiple layers.
- Debug, debug, debug. Instead of having to run that new piece of code in a complex and slow to start full environment, you can test if your business logic works right where you are building it.
Overall Unit Tests can save you a lot of time and money, while will also help you maintain a healthy code base.
How to do it
Make sure you have Convector-CLI version 1.1.0
$ npm i -g @worldsibu/convector-cli
Now, let’s start by creating a new Convector project:
$ conv new bughunting -c mybuggychaincode$ cd bughunting$ npm install
By default, a test file was created for your chaincode inside of
We use mocha for tests, which is a renown lib for that. That file is just bringing the plumbing and testing the default
create function that the controller has.
Go ahead and run it! In your terminal run
npm run test
That’s it! You just ran your first unit test for a Convector smart contract. That is also what would be run automatically in your CI/CD process.
Mocha is quite extensive, you can check all the options here. For now, let’s create an extra function and change the smart contract a little bit.
Update the following files
mybuggychaincode.model.ts , and
Go ahead and run the test:
$ npm test
The 4 testing functions should have run successfully!
Debugging is another useful task, with the Convector Suite you can debug your unit tests but you can also debug your code running in the blockchain (😲 yeah!).
Debugging Unit Test
./packages/mybuggychaincode-cc/package.json and replace the contents of the
test task with the following.
"test": "npm run build && mocha --inspect --debug=8888 -r ts-node/register tests/*.spec.ts --reporter spec"
--inspectparam will let you connect a debugger and walk through your code. I also changed the port to avoid collisions with other processes.
You can use your preferred debugger. I like to use Chrome DevTools for Node, so I just go to
chrome://inspect in my browser and click on
Open dedicated DevTools for Node .
Add connection and enter
localhost:8888 to listen for that port.
Go back to your code and in the test file located in
./packages/mybuggychaincode/tests/mybuggychaincode.spec.ts put a
npm test and voilá! You should now be able to debug nicely your code.
Debugging code in the blockchain
We use Hurley and since 1.0.0 it now supports a
You will need two new tasks in your CLI (if you don’t have them already). Go to your root
package.json and look for
$ npm run cc:start:debug — mybuggychaincode
If you see this, it’s running ready for debugging.
Listen for the debugging port in
All you need to do now is make a dummy call for the containers to be provisioned and the code loaded.
# It will throw an expected error - This will take some seconds while downloading dependencies again in the rest of the organizations
$ hurl invoke mybuggychaincode init
Now go to your DevTools and explore the source code.
You will see the native code running: