This is the 2nd post in a series of technical updates, focusing on the new testing suite for ARK Core v2, the challenges encountered, and a glimpse into the developmental process.
Test suites are an important part of any serious development environment, more so with blockchain technologies where each mistake can end up being very costly. Some would argue that test suites usurp unnecessary time, but in the long run it alleviates the headaches of small potential problems turning into big ones, and provides a means to easily spot mistakes and test your code. It is very important when writing code to focus on the long term vision and not be shortsighted. Spending extra time creating the necessary tools to help new developers quickly focus on innovation is far better time spent than finding and patching bugs. Good test cases can be much more valuable than final code as main code can be safely replaced/refactored when new technologies arise.
Development of Core v2 started with Mocha, the first testing suite (same as v1), however better options for our specific needs arose. The pros and cons of differing solutions were weighed, and in the end Jest was chosen over Mocha. There are many reasons why the dev team decided to adopt Jest.
With the growth of our team we are establishing a common base for developers, delivering the best possible tools (powerful mocking, snapshot testing, built-in code coverage, zero configuration) and making cross team collaboration smooth with testing appearing uniform across different sections of code.
One of Jest’s philosophies is to provide an integrated “zero-configuration” experience. Because ARK has access to use these tools, we were able to focus more on writing tests, resulting in a much more stable and healthy code base.
Migration from Mocha to Jest was a breeze because the Jest API and Mocha API are quite similar with only slight differences in function names. The major improvement being that Jest provides increased performance.
As stated by Airbnb:
“Airbnb migrated from Mocha to Jest. Running our test suite with Mocha took 12+ minutes. In CI with our beefy build machines (32 cores) we’re able to run the entire Jest suite in 4 minutes 30 seconds.”
Jest runs tests in parallel across workers for maximum performance. Giving you all the power of parallelization without any of the complexities that come with it. Console messages are buffered and printed together with test results. Sand boxed test files and automatic global states reset for every test, preventing two tests from conflicting with each other.
Starting tests for new Core API v2. After digging around with custom script for starting/stopping a node.
Our global config structure — the globalSetup flag can be seen.
The Jest test framework provides the globalSetup parameter — where you can define test prerequisites to be run before actual testing starts. In our case we must start and sync the node for testing to continue.
Simple API Test
Example of a simple API test done in Jest.
Jest provided us with an extraordinary performance increase over Mocha. Test runs are on average at least 2 to 3 times quicker than with Mocha!
Sample run of tests on all new ‘statistics’ endpoint in API v2
Core v2 will have new endpoints — statistics; which will be useful for all developers who want to perform analysis of the blockchain (number of total votes, number of transactions, blocks, …) — all this with a simple API call!
The ARK dev team is thrilled with our new testing suite setup! The switch to Jest enables us to develop in a manner that promotes a better, healthier codebase, larger performance increases, and makes for a solid foundation for all future work.
Our next blog post will focus on Core v2 WebHooks — a new API concept not present in v1.