Skip to main content

Scripting and tests Pro / Enterprise

The scripting feature allows you to run custom JavaScript inside Kreya. Among other things, it allows you to create tests and view the results of them.

Kreya operation scripts

Kreya operation scripts are attached directly to a specific Kreya operation and run automatically when the operation is invoked. These can mainly be used to test operation responses.

Find more information about Kreya operation scripts in the Operation scripts documentation.

Kreya scripts

Kreya scripts are scripts within Kreya that provide more advanced control, allowing you to programmatically invoke one or more operations using logic like loops and conditional statements for complex workflows or test scenarios.

Find more information about Kreya scripts in the Scripts documentation.

Script APIs

Kreya offers a set of useful APIs. Checkout the operation script API reference and scripts API reference documentation as well as the general script API reference. You can also share code or import external NPM modules.

User variables

Sometimes, you may need to share data between different operations. For example, if you have a process where you create an entity → update the entity → delete the entity, changing the entity ID manually each time becomes annoying. It would be much easier if you could temporarily store the ID of the created entity and reference it from the other two operations. For such use cases, user variables are the way to go.

The feature is available through the kreya.variables object. User variables can also be referenced via templating. A common use case is to set a variable in an operation script and then referencing it from somewhere else:

// In some script
kreya.variables.set('my_var', 123);

// In some other script
const myVar = kreya.variables.get('my_var');

// Or via templating in an operation
{{ vars.my_var }}

Faker

Similar to using faker in templating, it can be used with kreya.faker. It uses Bogus behind the scenes. As an example, you could use

const name = kreya.faker.name.fullName();

to generate a fake, but realistic full name.

Writing tests

Tests can be defined in any script. Simply use the kreya.test function, for example:

import { expect } from 'chai'; // chai is bundled with Kreya

kreya.test('test that will fail', () => expect(2).to.eql(3));
kreya.test('test that will succeed', () => expect(6).to.eql(6));

Of course these tests do not make much sense. Take a look at the gRPC or the REST example to see more realistic tests.

Viewing test results

The test results show up in the Tests tab. It will automatically show how many of the tests passed in the tab header. Should a test fail, Kreya will display the reason of the failure.