The Mocha Test Framework
The Mocha test framework (https://mochajs.org/) is the default test framework for map.apps. It replaces the deprecated Intern test library.
Installation
To integrate the Mocha framework in map.apps, the following dependencies have to be added to the package.json
file:
"@conterra/mapapps-mocha-runner": "^1.0.0",
"chai": "^4.3.4",
"mocha": "^9.0.0"
Afterwards, npm install
has to be performed.
Check the Installation
The available version of Mocha can be retrieved by: http://localhost:9090/resources/jsregistry/root/mocha
A self test can be started by: http://localhost:9090/resources/jsregistry/root/@conterra/mapapps-mocha-runner/latest/mocha.html?boot=/js/tests/test-init.js
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:
{
"name": "helloworld",
"version": "1.0.0-SNAPSHOT"
}
The hello.js
can be as simple as:
export function sayHello(msg){
if (!msg) {
return "Hello!";
}
return "Hello " + msg + "!";
}
The test file in tests/hello.js
can look like:
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!");
})
});
Run Tests
The preceding sample can be started using this URL: http://localhost:9090/resources/jsregistry/root/@conterra/mapapps-mocha-runner/latest/mocha.html?boot=/js/tests/test-init.js&timeout=5000&test=helloworld/tests/hello
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 .
|
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:
{
"name": "all-tests"
}
The all.js
has following structure:
import "helloworld/tests/hello",
...
import "otherpackage/tests/nameoftest"
The execution works exactly as described in the "Run Tests" section: