Testing and debugging a Convector Smart Contract

Walter Montes
Mar 21 · 5 min read

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.

Unit tests

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

Do you have Convector-CLI installed yet? Be sure to have Node 8.11.0 and Docker Community Edition.

Make sure you have Convector-CLI version 1.1.0

Now, let’s start by creating a new Convector project:


By default, a test file was created for your chaincode inside of ./packages/mybuggychaincode/tests/mybuggychaincode.spec.ts

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.controller.ts , mybuggychaincode.model.ts , and mybuggychaincode.spec.ts accordingly:

Go ahead and run the 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

Go to ./packages/mybuggychaincode-cc/package.json and replace the contents of the test task with the following.

The --inspect param 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 .

Click on 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 debugger; somewhere.

Run 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 debug param.

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 cc:start:debug and cc:install:debug .

If you see this, it’s running ready for debugging.

Listen for the debugging port in 9990 .

All you need to do now is make a dummy call for the containers to be provisioned and the code loaded.

Now go to your DevTools and explore the source code.

You will see the native code running:


A unified development platform to create and deploy enterprise smart contract systems.

Thanks to Diego Barahona

Walter Montes

Written by

CEO @ Covalent — https://covalentx.com



A unified development platform to create and deploy enterprise smart contract systems.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade