Skip to main content

Scripts Pro / Enterprise

Kreya scripts (not to be confused with operation scripts, which define scripts and tests in operations) offer a way to arbitrarily control the execution flow of operations. Among other things, it allows you to create tests and view the results of them.

This can be useful in a range of scenarios. For example, if you want to start a long-running job and wait until it is finished:

// Invoke an operation that starts a long running job on the server
await kreya.invokeOperation('start-long-running-job');

while (true) {
const result = await kreya.invokeOperation('fetch-job-status');
if (result.success) {
break;
}

await kreya.wait(500);
}

// Now invoke an operation that fetches the job result and performs tests on it
await kreya.invokeOperation('fetch-job-result');

Another use case could be to run multiple operations concurrently:

// Start an operation, but do not wait for its completion (no await keyword)
const runningOperation = kreya.invokeOperation('long-running');

// Invoke some other operations in the meantime
await kreya.invokeOperation('operation1');
await kreya.invokeOperation('operation2');

// Now we wait for the long-running operation to complete
await runningOperation;

View all available properties and methods in the API reference.

To create a new script, select Script in the new menu:

Require successful operations​

Call ensureSuccess() on an operation result to throw an error if the operation was unsuccessful. This stops the script unless the error is caught:

const result = await kreya.invokeOperation('./create-item');
result.ensureSuccess(); // Throws an error if the operation was unsuccessful

An operation is successful if it completes and all its tests pass. If it has no tests, the response status must indicate success. Tests can therefore assert an expected error response and still make the operation successful.

To require success for all subsequent operation invocations in the current script run, enable kreya.script.current.ensureSuccess:

kreya.script.current.ensureSuccess = true;
await kreya.invokeOperation('./create-item');
await kreya.invokeOperation('./fetch-item');

The setting defaults to false for each script run. Set it back to false to handle unsuccessful results yourself. Each invocation uses the setting in effect when it starts; changing it does not affect operations that are already running.

You can also use ensureSuccess() inside a test. A thrown error fails the test, and the script continues after the test:

await kreya.test('Item can be fetched', async () => {
const result = await kreya.invokeOperation('./fetch-item');
result.ensureSuccess();
});