Mocha tests in Collabora Online

We have many data structure classes in Collabora Online. Most of them are complex enough to warrant unit tests for their correctness. We already have cypress tests in Online which does integration testing, but we need unit tests for the internal data structure classes too. Mocha testing framework is a good match for this. With this we can do asserts via its BDD interface. In fact we could use any assertion library like Chai or Expect.js. You can now add Mocha tests in Online to test its any existing typescript classes or functions.

Here is a sample mocha test written for CPointSet typescript class:

/// <reference path="../src/layer/vector/CPoint.ts" />
/// <reference path="../src/layer/vector/CPointSet.ts" />

var assert = require('assert');

describe('CPointSet empty() tests', function () {

	describe('new CPointSet()', function () {
		it('should be empty', function () {
			assert.ok((new
                           CPointSet()).empty());
		});
	});
});

How to add a new Mocha test to Online?

Lets write a new test for the function CBounds.parse() as an example:

  • Create a new test file for tests related to CBounds class under loleaflet/mocha_tests/. Lets name it CBounds.test.ts
  • Reference the needed types from CBounds.ts by adding these lines.
/// <reference path="../src/layer/vector/CPoint.ts" />
/// <reference path="../src/layer/vector/CBounds.ts" />
  • Import Nodejs assert.
var assert = require('assert').strict;
  • Add a BDD style description of the set of tests we are going to write for CBounds.parse().
describe('CBounds parse() tests', function () {
    // Write unit tests here.
});
  • Inside that add a few test cases with descriptions like below:
describe('CBounds.parse() call with an empty string argument',
    function () {
	it('should return undefined', function () {
		assert.equal(CBounds.parse(''), undefined);
	});
    });
  • Now run ‘make check’ inside the loleaflet/ dir to see these tests in action. In current master branch you should see something like below along with the results for the new test you have added.

If interested, do take a look at the commit 8b2eae423cf77e7c725843061898c3968a72c547 that integrated Mocha framework to Online.