Coding and Testing Tips

This section links to some useful information for both new and experienced programmers and testers.

The developers who build this SDK have compiled some simple resources to help you plan, build and test your application.

Planning your application

The KeyLines blog contains lots of useful information to get you up and running. We recommend the following posts:

Testing your application

Over time we’ve experimented with a wide range of different testing, benchmarking and analysis modules. As you will likely have specific requirements for testing, we cannot recommend any modules. However, here are a number of test modules that we find useful:

Mocha: NodeJS Test Framework

Mocha is one of the most popular test frameworks available in NodeJS. While the framework itself is very complete it’s easy to use even when testing complex asynchronous code.

Using a self-contained JS file loading in a webpage, Mocha can be used for both NodeJS testing and front-end JavaScript testing. The module also includes pluggable assertion and reporting libraries, making it a useful testing tool for a Javascript developer.

Writing your first test with Mocha is usually the first step to get in touch with TDD or BDD in Javascript. Find out more on the Mocha website.

Chai: Assertion Library

Every assertion library has pros and cons: the important thing when making a decision is to select one that best fits your needs.

For KeyLines, we experimented with various options before settling on Chai. For us its main strengths are its seamless integration with Mocha and its versatility.

Find out more on the Chai website.

Karma: Testing environment

Karma is a test runner environment that leverages the power of Mocha to run within a web page in order to test your code cross-browser. With Karma you can take your Mocha test suites and spawn 10 different browsers simultaneously, viewing results in real-time reports.

Karma is also built to be extended by plugins: preprocessors, reporters, browsers, frameworks… hundreds of plugins have been published, with new ones being added regularly.

To understand more about this tool and its plugin ecosystem visit the Karma website.

Istanbul: Coverage Tool

Istanbul is a coverage tool for Javascript that lets you know which bits of code are executed during the test run and which other bits need to be covered. Once the test run is completed, an HTML report is produced by the Istanbul Karma plugin that shows current coverage by browser, line by line.

Find out more on the Istanbul Karma plugin website.

Automated UI Testing

If you’re thinking of setting up automated UI testing, our blog post 'How to test a JavaScript UI' has some useful tips to get you started.

The KeyLines team mostly test chart behaviours using the API directly, but when you build KeyLines into your own applications you may be interested in automated UI testing.

To check which items are present at a given location, you can use the getItemInfoAt method.

You can also use the view coordinates and world coordinates API calls to convert between the mouse's location relative to the canvas element, and an item's position in the chart's internal co-ordinate system. This should help you set and check positions.

As an example, to convert from world co-ordinates to view co-ordinates:

var item = chart.getItem(chart.selection()[0]);
var viewCoords = chart.viewCoordinates(item.x, item.y);