Skip to main content

Test

test

Defines a test case.

When all assertions in the test are successful, the test is marked as passed and will cause a success message to be written to the log.

Otherwise, if any of the assertions fail or the code in the test throws an error, the test is marked as failed and will cause a failed message to be written to the log.

The optional testParameters argument takes an array of values or objects, and the test function will be executed for each item in the array.

Mocks or ignores set with setMock or setIgnore inside the test function will be reset after the test and between each item in the testParameters array.

test(description, testFunction);
test(description, testParameters, testFunction);

Example 1:

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

test("has equal values 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("has equal values", 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 failedText is written to the log.

assert_equal(testValue, expectedValue);
assert_equal(testValue, expectedValue, failedText);

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 given failedText is written to the log.

assert_ok(test_value);
assert_ok(test_value, failedText);

Example :

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

assert_throws

Checks that a function throws. If the check fails, the given failedText is written to the log.

With only the function argument, the assertion passes as long as the function throws anything. An optional second argument verifies the thrown error in more detail:

  • a string, compared against the error message;
  • a regex, matched against the error message;
  • a function that receives the thrown error, so you can verify it in detail using other assertions.

The error message is only used for Error objects. Any other thrown value, such as a string thrown with throw("...")), is matched against the thrown value itself.

assert_throws(fn);
assert_throws(fn, matcher);
assert_throws(fn, matcher, failedText);

Examples :

test("Test throws", () => {
// passes: the function throws
assert_throws(() => throw new Error("boom"));

// passes: the error message equals the string
assert_throws(() => throw new Error("boom"), "boom");

// fails: the error message does not equal the string
assert_throws(() => throw new Error("kaboom"), "boom");

// passes: the error message matches the regex
assert_throws(() => throw new Error("boom"), /bo+m/);

// passes: the matcher function verifies the error without throwing
assert_throws(
() => throw new Error("boom"),
(e) => assert_equal(e.message, "boom", "Unexpected error message"),
);
});

setIgnore

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

Can be used both with user-defined functions and with most AutomatorScript functions.

setIgnore("functionName");

Example :

function myFunc(p) {
return p;
}

log(myFunc(5)); // 5

setIgnore("myFunc");
log(myFunc(5)); // ignored

setIgnore("myFunc");
log(myFunc(5)); // 5

After the first call of setIgnore the function myFunc 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 both with user-defined functions and with most AutomatorScript functions.

setMock("functionName", "mockResult");
setMock("functionName", (args) => "mockFunction");
setMock("functionName"); // remove mock

Examples

function myFunc(p) {
return p;
}

log(myFunc(5)); // 5

setMock("myFunc", "fixed mocked result");
log(myFunc(5)); // 'fixed mocked result'

setMock("myFunc", (p) => `mocked myFunc(${p})`);
log(myFunc(5)); // 'mocked myFunc(5)'

setMock("myFunc"); // Remove mock
log(myFunc(5)); // 5

setMock("Array.isArray", false);
log(Array.isArray([1, 2, 3])); // false

setMock("Xurrent.graphqlQuery", [{ subject: "test" }]);
log(Xurrent.graphqlQuery("problems", { fields: ["subject"] })); // [{ subject: "test" }]