Skip to main content

Test

test

Defines a test case for TDD test packages. Each test can be "SUCCESSFUL" or "FAILED" depending on the asserts in the test. There can be more than one assert in a test - if there is no assert the test is always "SUCCESSFUL". Test parameters are lists, used to execute the test multiple times - once for each parameter list entry.

test("test_name", [test_parameters], test_function);

Example 1:

test("Test equal 1", () => {
x = 4;
y = 4;
assert_equal(x, y, "Values are not equal");
});

test("Test equal 2", () => {
x = 4;
y = 4;
assert_ok(x === y, "Values are not equal");
});

Example 2: using parameter list

let params = [
{ x: 1, y: 1 }, // first execution will succeed
{ x: 4, y: 4 }, // second execution will succeed
{ x: 5, y: 6 }, // third execution will fail
];
test("Test equal 1", params, (p) => {
assert_equal(p.x, p.y, "Values are not equal");
});

assert_equal

Checks if a test value equals an expected value. If the check fails the failed_text is written to the log. Commonly used in tdd tests.

For each assert_equal a log line with the test name is written in the case of a Failure.

assert_equal(test_value, expected_value, failed_text, [log_on_success]);

See also: test

Example :

test("Test equal", () => {
x = 4;
y = 4;
assert_equal(x, y, "Values are not equal");
});

assert_ok

Checks if a test value is truthy. If the check fails the failed_text is written to the log. Commonly used in tdd tests.

For each assert_ok a log line with the test name is written in the case of a Failure.

assert_ok(test_value, failed_text, [log_on_success]);

See also: test

Example :

test("Test equal", () => {
x = 4;
y = 4;
assert_ok(x === y, "Values are not equal");
});

setIgnore

Toggles a function on the ignore list. When ignored, calls return 'ignored' instead of executing.

Can be used with own functions and with most api system functions as described in this document.

setIgnore("function name");

Example :

function x(p) {
return p;
}

(function () {
let res_1 = x(5);
setIgnore("x");
let res_2 = x(5);
setIgnore("x");
let res_3 = x(5);

log("res_1", res_1); // 5
log("res_2", res_2); // ignored
log("res_3", res_3); // 5
})();

After the first call of setIgnore the function x is ignored - each call results with the value "ignored". With the second call of setIgnore the function works as implemented.

setMock

Sets a mock result or mock function for testing, or resets to the original when called with only the function name.

Returns true when a mock is set, false when removed.

Can be used with own functions and with most api system functions as described in this document.

[let result =]
setMock('function name'[, mock
data
]
[, (p1, p2, ...px) => {
}]
)

Example :

function x(p) {
return p;
}

(function () {
let res_1 = x(5);

setMock("x", "fixed mocked result");

let res_2 = x(5);

setMock("x", (p) => {
return "mocked function";
});

let res_3 = x(5);

setMock("x");

let res_4 = x(5);

log("res_1", res_1); // 5
log("res_2", res_2); // 'fixed mocked result'
log("res_3", res_3); // 'mocked function'
log("res_4", res_4); // 5
})();

execute_package

Executes the package with the given name and returns an object with all global variables of the executed package.

Optionally, a data object can be passed to the package to override the standard variables injected into the package.

Unless explicitly specified in the data object, each standard variable is set to the value of the current package.

const result = execute_package(package_name);
const result = execute_package(package_name, data);
warning

Nesting package executions is not allowed: calling execute_package inside another execute_package will result in an error.

Example

Suppose you have a package called "pkg to run" that contains the following code:

const x = 42;
const y = `The event is: ${event}`;

Then you can execute the package like this:

const data = { event: "my_event" };
const result = execute_package("pkg to run", data);
log("result", result); // { x: 42, y: "The event is: my_event" }