Unit testing

The Mocha test framework (https://mochajs.org/) is the default test framework for map.apps.

Installation

To integrate the Mocha framework in map.apps, the following dependencies have to be added to the package.json file:

package.json
"@conterra/mapapps-mocha-runner": "^1.0.0",
"chai": "^4.3.4",
"mocha": "^9.0.0"

Afterwards, npm install has to be performed.

Write a test

For full documentation on how to write a unit test, see the Mocha website . In the following, only a small sample is shown.

The following project structure is assumed:

src/main/js/bundles
    +-helloworld          // bundle folder
          manifest.json   // bundle descriptor
          hello.js        // file to test
        \-tests           // tests folder to put tests into
              hello.js    // test for the hello.js file

The contents of the manifest.json file can look like:

manifest.json
{
    "name": "helloworld",
    "version": "1.0.0-SNAPSHOT"
}

The hello.js can be as simple as:

hello.js
export function sayHello(msg){
    if (!msg) {
        return "Hello!";
    }
    return "Hello " + msg + "!";
}

The test file in tests/hello.js can look like:

tests/hello.js
import { assert } from "chai";
import md from "module";
import { sayHello } from "../hello.js"

describe(md.id, function() {
    it("test that sayHello without a message produces no whitespace between prefix and '!'", function() {
        assert.equal(sayHello(), "Hello!", "expected no space between 'Hello' and '!'");
    });
    it(
        "test that sayHello with a message produces whitespace between prefix and the parameter", function() {
        assert.equal(sayHello("First Test"), "Hello First Test!");
    })
});

Execute multiple tests

To execute multiple tests across multiple packages/bundles at once, a special test bundle is required, which fetches all tests to be executed.

The mapapps-4-developers project already comes with such a bundle. Its name is sample_tests.
Test package structure
 src/main/js/bundles
    +-all-tests             // package folder
           manifest.json    // package descriptor
           all.js           // file which lists the tests to execute

The contents of the manifest.json file can look like:

manifest.json
{
    "name": "all-tests"
}

The all.js has following structure:

all.js
import "helloworld/tests/hello",
...
import "otherpackage/tests/nameoftest"

The execution works exactly as described in the "Run Tests" section: